{"id":33011,"date":"2024-11-01T09:13:06","date_gmt":"2024-11-01T09:13:06","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=33011"},"modified":"2024-11-01T11:29:23","modified_gmt":"2024-11-01T11:29:23","slug":"spring-boot-backend-development-course-implementing-login-and-logout-with-oauth2-testing-oauth2-execution","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/33011\/","title":{"rendered":"Spring Boot Backend Development Course, Implementing Login and Logout with OAuth2, Testing OAuth2 Execution"},"content":{"rendered":"<p><body><\/p>\n<p>In today&#8217;s web applications, authentication and authorization are crucial elements, and OAuth2 is a widely used standard for implementing them. In this course, we will explore in detail how to implement OAuth2-based login and logout functionality using Spring Boot. The goal of this course is to apply a secure authentication method through OAuth2 in a Spring Boot application and learn how to test it.<\/p>\n<h2>1. What is OAuth2?<\/h2>\n<p>OAuth2 (Open Authorization 2.0) is an authorization framework that allows internet users to use their accounts with other services without exposing their information to third-party applications. Essentially, OAuth2 operates based on access tokens, which allow users to access their data. During this process, there is no need to share the user&#8217;s password.<\/p>\n<h3>1.1 Components of OAuth2<\/h3>\n<ul>\n<li><strong>Resource Owner:<\/strong> The owner of the resource, usually the user.<\/li>\n<li><strong>Client:<\/strong> The application that requests the service on behalf of the resource owner.<\/li>\n<li><strong>Authorization Server:<\/strong> The server that authenticates the client&#8217;s request and issues an access token after consent.<\/li>\n<li><strong>Resource Server:<\/strong> The server that can access the protected resources (e.g., user data).<\/li>\n<\/ul>\n<h3>1.2 OAuth2 Flow<\/h3>\n<p>The authentication flow of OAuth2 proceeds as follows:<\/p>\n<ol>\n<li>The user sends an authentication request to the Authorization Server through the client.<\/li>\n<li>If the user successfully authenticates, the Authorization Server issues an access token to the client.<\/li>\n<li>The client sends the issued access token to the Resource Server to access the protected resources.<\/li>\n<\/ol>\n<h2>2. Setting up Spring Boot Environment<\/h2>\n<p>To set up the Spring Boot environment, we first need to add the necessary dependencies. To do this, we will use Spring Initializr to create a project.<\/p>\n<h3>2.1 Creating a Project in Spring Initializr<\/h3>\n<p>Create a Spring Boot project with the following settings:<\/p>\n<ul>\n<li><strong>Project:<\/strong> Maven Project<\/li>\n<li><strong>Language:<\/strong> Java<\/li>\n<li><strong>Spring Boot:<\/strong> 2.6.3 (select the latest version)<\/li>\n<li><strong>Dependencies:<\/strong> Spring Web, Spring Security, Spring Data JPA, H2 Database<\/li>\n<\/ul>\n<h3>2.2 Modifying the pom.xml File<\/h3>\n<p>Next, add the OAuth2-related dependencies to the <code>pom.xml<\/code> file:<\/p>\n<pre><code>&lt;dependency&gt;\n    &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\n    &lt;artifactId&gt;spring-boot-starter-oauth2-client&lt;\/artifactId&gt;\n&lt;\/dependency&gt;\n&lt;dependency&gt;\n    &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\n    &lt;artifactId&gt;spring-boot-starter-oauth2-resource-server&lt;\/artifactId&gt;\n&lt;\/dependency&gt;\n<\/code><\/pre>\n<h2>3. OAuth2 Configuration<\/h2>\n<p>To configure OAuth2, modify the <code>application.yml<\/code> file to include the Authorization Server and Resource Server information.<\/p>\n<pre><code>spring:\n  security:\n    oauth2:\n      client:\n        registration:\n          google:\n            client-id: YOUR_CLIENT_ID\n            client-secret: YOUR_CLIENT_SECRET\n            scope:\n              - profile\n              - email\n            redirect-uri: \"{baseUrl}\/login\/oauth2\/code\/{registrationId}\"\n        provider:\n          google:\n            authorization-uri: https:\/\/accounts.google.com\/o\/oauth2\/auth\n            token-uri: https:\/\/oauth2.googleapis.com\/token\n            user-info-uri: https:\/\/www.googleapis.com\/oauth2\/v3\/userinfo\n            user-name-attribute: sub\n<\/code><\/pre>\n<h2>4. Spring Security Configuration<\/h2>\n<p>Configure Spring Security to handle authentication and authorization. Below is an example of a basic security configuration class:<\/p>\n<pre><code>import org.springframework.context.annotation.Bean;\nimport org.springframework.context.annotation.Configuration;\nimport org.springframework.security.config.annotation.web.builders.HttpSecurity;\nimport org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;\nimport org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;\n\n@Configuration\n@EnableWebSecurity\npublic class SecurityConfig extends WebSecurityConfigurerAdapter {\n    @Override\n    protected void configure(HttpSecurity http) throws Exception {\n        http\n            .authorizeRequests()\n                .antMatchers(\"\/\", \"\/login\").permitAll()\n                .anyRequest().authenticated()\n                .and()\n            .oauth2Login();\n    }\n}\n<\/code><\/pre>\n<h2>5. Retrieving User Information<\/h2>\n<p>Once the user successfully authenticates, retrieve user information through the OAuth2 client. To do this, create a controller.<\/p>\n<pre><code>import org.springframework.security.core.annotation.AuthenticationPrincipal;\nimport org.springframework.security.oauth2.core.user.OAuth2User;\nimport org.springframework.web.bind.annotation.GetMapping;\nimport org.springframework.web.bind.annotation.RestController;\n\n@RestController\npublic class UserController {\n    @GetMapping(\"\/user\")\n    public OAuth2User getUser(@AuthenticationPrincipal OAuth2User principal) {\n        return principal;\n    }\n}\n<\/code><\/pre>\n<h2>6. Testing and Running<\/h2>\n<p>Run the application and access <code>http:\/\/localhost:8080<\/code> in your web browser. The user will be redirected to the Google login page by clicking the login button. After logging in, they can check their user information via the <code>\/user<\/code> endpoint.<\/p>\n<h2>Conclusion<\/h2>\n<p>In this course, we explored how to implement login and logout functionality using OAuth2 with Spring Boot. Throughout this process, we learned the basic concepts of OAuth2, setting up the required dependencies, configuring Spring Security, and retrieving user information. This allows you to add a more secure login feature to your web applications.<\/p>\n<h3>References<\/h3>\n<ul>\n<li><a href=\"https:\/\/spring.io\/projects\/spring-security\">Spring Security<\/a><\/li>\n<li><a href=\"https:\/\/oauth.net\/2\/\">OAuth 2.0 Specification<\/a><\/li>\n<li><a href=\"https:\/\/spring.io\/guides\/gs\/spring-boot\/\">Spring Boot Guides<\/a><\/li>\n<\/ul>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In today&#8217;s web applications, authentication and authorization are crucial elements, and OAuth2 is a widely used standard for implementing them. In this course, we will explore in detail how to implement OAuth2-based login and logout functionality using Spring Boot. The goal of this course is to apply a secure authentication method through OAuth2 in a &hellip; <a href=\"https:\/\/atmokpo.com\/w\/33011\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;Spring Boot Backend Development Course, Implementing Login and Logout with OAuth2, Testing OAuth2 Execution&#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-33011","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, Testing OAuth2 Execution - \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\/33011\/\" \/>\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, Testing OAuth2 Execution - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"In today&#8217;s web applications, authentication and authorization are crucial elements, and OAuth2 is a widely used standard for implementing them. In this course, we will explore in detail how to implement OAuth2-based login and logout functionality using Spring Boot. The goal of this course is to apply a secure authentication method through OAuth2 in a &hellip; \ub354 \ubcf4\uae30 &quot;Spring Boot Backend Development Course, Implementing Login and Logout with OAuth2, Testing OAuth2 Execution&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/33011\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:13:06+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:29:23+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\/33011\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33011\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"Spring Boot Backend Development Course, Implementing Login and Logout with OAuth2, Testing OAuth2 Execution\",\"datePublished\":\"2024-11-01T09:13:06+00:00\",\"dateModified\":\"2024-11-01T11:29:23+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33011\/\"},\"wordCount\":488,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Spring Boot backend development\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/33011\/\",\"url\":\"https:\/\/atmokpo.com\/w\/33011\/\",\"name\":\"Spring Boot Backend Development Course, Implementing Login and Logout with OAuth2, Testing OAuth2 Execution - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:13:06+00:00\",\"dateModified\":\"2024-11-01T11:29:23+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33011\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/33011\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/33011\/#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, Testing OAuth2 Execution\"}]},{\"@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, Testing OAuth2 Execution - \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\/33011\/","og_locale":"ko_KR","og_type":"article","og_title":"Spring Boot Backend Development Course, Implementing Login and Logout with OAuth2, Testing OAuth2 Execution - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"In today&#8217;s web applications, authentication and authorization are crucial elements, and OAuth2 is a widely used standard for implementing them. In this course, we will explore in detail how to implement OAuth2-based login and logout functionality using Spring Boot. The goal of this course is to apply a secure authentication method through OAuth2 in a &hellip; \ub354 \ubcf4\uae30 \"Spring Boot Backend Development Course, Implementing Login and Logout with OAuth2, Testing OAuth2 Execution\"","og_url":"https:\/\/atmokpo.com\/w\/33011\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:13:06+00:00","article_modified_time":"2024-11-01T11:29:23+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\/33011\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/33011\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"Spring Boot Backend Development Course, Implementing Login and Logout with OAuth2, Testing OAuth2 Execution","datePublished":"2024-11-01T09:13:06+00:00","dateModified":"2024-11-01T11:29:23+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/33011\/"},"wordCount":488,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Spring Boot backend development"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/33011\/","url":"https:\/\/atmokpo.com\/w\/33011\/","name":"Spring Boot Backend Development Course, Implementing Login and Logout with OAuth2, Testing OAuth2 Execution - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:13:06+00:00","dateModified":"2024-11-01T11:29:23+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/33011\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/33011\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/33011\/#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, Testing OAuth2 Execution"}]},{"@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\/33011","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=33011"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/33011\/revisions"}],"predecessor-version":[{"id":33012,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/33011\/revisions\/33012"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=33011"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=33011"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=33011"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}