{"id":33033,"date":"2024-11-01T09:13:17","date_gmt":"2024-11-01T09:13:17","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=33033"},"modified":"2024-11-01T11:29:15","modified_gmt":"2024-11-01T11:29:15","slug":"spring-boot-backend-development-course-implementing-login-and-logout-with-oauth2-obtaining-tokens","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/33033\/","title":{"rendered":"Spring Boot Backend Development Course, Implementing Login and Logout with OAuth2, Obtaining Tokens"},"content":{"rendered":"<p><body><\/p>\n<p>In recent years, the security and user authentication methods of web applications have changed dramatically. In particular, OAuth2 has established itself as the standard for handling user authentication in many web services. This course will detail how to implement login and logout using OAuth2 with Spring Boot, as well as the token issuance process. Through this article, you will learn the basic principles of OAuth2 and gain the skills needed to build real applications.<\/p>\n<h2>1. What is OAuth2?<\/h2>\n<p>OAuth2 is a protocol for user authentication that allows users to grant access without providing their information to third-party services. This enables apps or services to access the user&#8217;s resources. The main components of OAuth2 are as follows:<\/p>\n<ul>\n<li><strong>User (Resource Owner):<\/strong> The entity that protects and manages their information.<\/li>\n<li><strong>Client:<\/strong> An application that seeks to access resources on behalf of the user.<\/li>\n<li><strong>Resource Server:<\/strong> The server that provides protected resources.<\/li>\n<li><strong>Authorization Server:<\/strong> The server that handles user authentication and issues access tokens to the client.<\/li>\n<\/ul>\n<h2>2. What is Spring Boot?<\/h2>\n<p>Spring Boot is a project based on Java&#8217;s Spring Framework that helps in quickly developing applications. Spring Boot offers the following advantages:<\/p>\n<ul>\n<li>Simplified configuration: Thanks to various defaults, you can get started quickly without complex configurations.<\/li>\n<li>Auto-configuration: You can easily add the necessary libraries for automatic configuration.<\/li>\n<li>Starter packages: These provide starter packages that combine multiple dependencies and configurations to speed up development.<\/li>\n<\/ul>\n<h2>3. Preparing to Build an OAuth2 Login System<\/h2>\n<h3>3.1 Project Setup<\/h3>\n<p>To start a Spring Boot project, use <a href=\"https:\/\/start.spring.io\/\">Spring Initializr<\/a> to create a basic project. Add the following dependencies:<\/p>\n<ul>\n<li>Spring Web<\/li>\n<li>Spring Security<\/li>\n<li>OAuth2 Client<\/li>\n<li>Spring Data JPA<\/li>\n<li>H2 Database (for development)<\/li>\n<\/ul>\n<h3>3.2 Project Structure<\/h3>\n<pre>\n        \/src\n        \u2514\u2500\u2500 main\n            \u251c\u2500\u2500 java\n            \u2502   \u2514\u2500\u2500 com\n            \u2502       \u2514\u2500\u2500 example\n            \u2502           \u2514\u2500\u2500 oauth2demo\n            \u2502               \u251c\u2500\u2500 controller\n            \u2502               \u251c\u2500\u2500 model\n            \u2502               \u251c\u2500\u2500 repository\n            \u2502               \u251c\u2500\u2500 security\n            \u2502               \u2514\u2500\u2500 service\n            \u2514\u2500\u2500 resources\n                \u251c\u2500\u2500 application.properties\n                \u2514\u2500\u2500 static\n    <\/pre>\n<h3>3.3 Configuring application.properties<\/h3>\n<p>To use OAuth2, configure as below. I will use Google OAuth2 as an example:<\/p>\n<pre>\n        spring.security.oauth2.client.registration.google.client-id=YOUR_CLIENT_ID\n        spring.security.oauth2.client.registration.google.client-secret=YOUR_CLIENT_SECRET\n        spring.security.oauth2.client.registration.google.redirect-uri={baseUrl}\/login\/oauth2\/code\/{registrationId}\n        spring.security.oauth2.client.registration.google.scope=profile, email\n        spring.security.oauth2.client.provider.google.authorization-uri=https:\/\/accounts.google.com\/o\/oauth2\/auth\n        spring.security.oauth2.client.provider.google.token-uri=https:\/\/oauth2.googleapis.com\/token\n        spring.security.oauth2.client.provider.google.user-info-uri=https:\/\/www.googleapis.com\/oauth2\/v3\/userinfo\n    <\/pre>\n<h2>4. Spring Security Configuration<\/h2>\n<p>Spring Security is used to manage authentication and authorization. Below is a basic security configuration example:<\/p>\n<pre>\n    import org.springframework.context.annotation.Bean;\n    import org.springframework.context.annotation.Configuration;\n    import org.springframework.security.config.annotation.web.builders.HttpSecurity;\n    import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;\n    import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;\n\n    @Configuration\n    @EnableWebSecurity\n    public class SecurityConfig extends WebSecurityConfigurerAdapter {\n        @Override\n        protected void configure(HttpSecurity http) throws Exception {\n            http\n                .authorizeRequests()\n                .anyRequest().authenticated()\n                .and()\n                .oauth2Login();\n        }\n    }\n    <\/pre>\n<h2>5. Retrieving User Information<\/h2>\n<p>Once the user has completed the login, the client application can obtain the authentication information. To fetch user&#8217;s information, implement the service below:<\/p>\n<pre>\n    import org.springframework.security.core.Authentication;\n    import org.springframework.security.oauth2.core.user.OAuth2User;\n    import org.springframework.stereotype.Service;\n\n    @Service\n    public class UserService {\n        public String getCurrentUserName(Authentication authentication) {\n            OAuth2User oauth2User = (OAuth2User) authentication.getPrincipal();\n            return oauth2User.getAttribute(\"name\");\n        }\n    }\n    <\/pre>\n<h2>6. Implementing Logout<\/h2>\n<p>Logout is also a common requirement. You can implement a simple logout feature using the configuration below:<\/p>\n<pre>\n    http\n        .logout()\n        .logoutSuccessUrl(\"\/login\")\n        .invalidateHttpSession(true)\n        .clearAuthentication(true);\n    <\/pre>\n<h2>7. Running and Testing the Application<\/h2>\n<p>After completing all configurations, run the application and access <code>http:\/\/localhost:8080<\/code> in your web browser. If configured correctly, the Google login screen will appear. You will also be able to view a screen that retrieves the user&#8217;s name after logging in.<\/p>\n<h2>8. Conclusion<\/h2>\n<p>In this course, we explored how to implement login and logout functions based on OAuth2 using Spring Boot. OAuth2 is a widely used authentication method in modern web applications, and we have seen how easily it can be configured using Spring Boot. We hope to build a more secure and convenient user authentication system by adding more advanced features in the future.<\/p>\n<h2>Additional Resources<\/h2>\n<p>If you would like more information, please refer to the links below:<\/p>\n<ul>\n<li><a href=\"https:\/\/spring.io\/projects\/spring-boot\">Official Spring Boot Documentation<\/a><\/li>\n<li><a href=\"https:\/\/spring.io\/projects\/spring-security\">Official Spring Security Documentation<\/a><\/li>\n<li><a href=\"https:\/\/oauth.net\/2\/\">Official OAuth 2.0 Website<\/a><\/li>\n<\/ul>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In recent years, the security and user authentication methods of web applications have changed dramatically. In particular, OAuth2 has established itself as the standard for handling user authentication in many web services. This course will detail how to implement login and logout using OAuth2 with Spring Boot, as well as the token issuance process. Through &hellip; <a href=\"https:\/\/atmokpo.com\/w\/33033\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;Spring Boot Backend Development Course, Implementing Login and Logout with OAuth2, Obtaining Tokens&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[131],"tags":[],"class_list":["post-33033","post","type-post","status-publish","format-standard","hentry","category-spring-boot-backend-development"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Spring Boot Backend Development Course, Implementing Login and Logout with OAuth2, Obtaining Tokens - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/atmokpo.com\/w\/33033\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Spring Boot Backend Development Course, Implementing Login and Logout with OAuth2, Obtaining Tokens - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"In recent years, the security and user authentication methods of web applications have changed dramatically. In particular, OAuth2 has established itself as the standard for handling user authentication in many web services. This course will detail how to implement login and logout using OAuth2 with Spring Boot, as well as the token issuance process. Through &hellip; \ub354 \ubcf4\uae30 &quot;Spring Boot Backend Development Course, Implementing Login and Logout with OAuth2, Obtaining Tokens&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/33033\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:13:17+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:29:15+00:00\" \/>\n<meta name=\"author\" content=\"root\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:site\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:label1\" content=\"\uae00\uc4f4\uc774\" \/>\n\t<meta name=\"twitter:data1\" content=\"root\" \/>\n\t<meta name=\"twitter:label2\" content=\"\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04\" \/>\n\t<meta name=\"twitter:data2\" content=\"3\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/33033\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33033\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"Spring Boot Backend Development Course, Implementing Login and Logout with OAuth2, Obtaining Tokens\",\"datePublished\":\"2024-11-01T09:13:17+00:00\",\"dateModified\":\"2024-11-01T11:29:15+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33033\/\"},\"wordCount\":501,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Spring Boot backend development\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/33033\/\",\"url\":\"https:\/\/atmokpo.com\/w\/33033\/\",\"name\":\"Spring Boot Backend Development Course, Implementing Login and Logout with OAuth2, Obtaining Tokens - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:13:17+00:00\",\"dateModified\":\"2024-11-01T11:29:15+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33033\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/33033\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/33033\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Spring Boot Backend Development Course, Implementing Login and Logout with OAuth2, Obtaining Tokens\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/atmokpo.com\/w\/#website\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/atmokpo.com\/w\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"ko-KR\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"contentUrl\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"width\":400,\"height\":400,\"caption\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\"},\"image\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/bebubo4\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\",\"name\":\"root\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"caption\":\"root\"},\"sameAs\":[\"http:\/\/atmokpo.com\/w\"],\"url\":\"https:\/\/atmokpo.com\/w\/author\/root\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Spring Boot Backend Development Course, Implementing Login and Logout with OAuth2, Obtaining Tokens - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/atmokpo.com\/w\/33033\/","og_locale":"ko_KR","og_type":"article","og_title":"Spring Boot Backend Development Course, Implementing Login and Logout with OAuth2, Obtaining Tokens - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"In recent years, the security and user authentication methods of web applications have changed dramatically. In particular, OAuth2 has established itself as the standard for handling user authentication in many web services. This course will detail how to implement login and logout using OAuth2 with Spring Boot, as well as the token issuance process. Through &hellip; \ub354 \ubcf4\uae30 \"Spring Boot Backend Development Course, Implementing Login and Logout with OAuth2, Obtaining Tokens\"","og_url":"https:\/\/atmokpo.com\/w\/33033\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:13:17+00:00","article_modified_time":"2024-11-01T11:29:15+00:00","author":"root","twitter_card":"summary_large_image","twitter_creator":"@bebubo4","twitter_site":"@bebubo4","twitter_misc":{"\uae00\uc4f4\uc774":"root","\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04":"3\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/33033\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/33033\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"Spring Boot Backend Development Course, Implementing Login and Logout with OAuth2, Obtaining Tokens","datePublished":"2024-11-01T09:13:17+00:00","dateModified":"2024-11-01T11:29:15+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/33033\/"},"wordCount":501,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Spring Boot backend development"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/33033\/","url":"https:\/\/atmokpo.com\/w\/33033\/","name":"Spring Boot Backend Development Course, Implementing Login and Logout with OAuth2, Obtaining Tokens - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:13:17+00:00","dateModified":"2024-11-01T11:29:15+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/33033\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/33033\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/33033\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"Spring Boot Backend Development Course, Implementing Login and Logout with OAuth2, Obtaining Tokens"}]},{"@type":"WebSite","@id":"https:\/\/atmokpo.com\/w\/#website","url":"https:\/\/atmokpo.com\/w\/","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","description":"","publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/atmokpo.com\/w\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"ko-KR"},{"@type":"Organization","@id":"https:\/\/atmokpo.com\/w\/#organization","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","url":"https:\/\/atmokpo.com\/w\/","logo":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/","url":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","contentUrl":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","width":400,"height":400,"caption":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8"},"image":{"@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/bebubo4"]},{"@type":"Person","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7","name":"root","image":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","caption":"root"},"sameAs":["http:\/\/atmokpo.com\/w"],"url":"https:\/\/atmokpo.com\/w\/author\/root\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/33033","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/comments?post=33033"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/33033\/revisions"}],"predecessor-version":[{"id":33034,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/33033\/revisions\/33034"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=33033"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=33033"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=33033"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}