{"id":33009,"date":"2024-11-01T09:13:05","date_gmt":"2024-11-01T09:13:05","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=33009"},"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-writing-oauth2-configuration-files","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/33009\/","title":{"rendered":"Spring Boot Backend Development Course, Implementing Login and Logout with OAuth2, Writing OAuth2 Configuration Files"},"content":{"rendered":"<h2>1. Introduction<\/h2>\n<p>\n    Recently, with the increasing development of web applications based on microservices architecture and cloud services, the popularity of frameworks like Spring Boot is rising.<br \/>\n    Spring Boot is a framework that helps to quickly develop applications without complex configuration.<br \/>\n    In this tutorial, we will learn in detail how to implement login and logout functionalities using OAuth2 in backend development with Spring Boot.\n<\/p>\n<h2>2. What is OAuth2?<\/h2>\n<p>\n    OAuth2 is an authentication protocol that allows users to control access to the client application.<br \/>\n    With OAuth2, users can perform authentication and authorization for specific resources without sharing their credentials with the application.<br \/>\n    This is a way to enhance security and significantly improve user experience.\n<\/p>\n<h3>2.1 Key Components of OAuth2<\/h3>\n<ul>\n<li><strong>Resource Owner<\/strong>: The user who grants permission to a client application<\/li>\n<li><strong>Client<\/strong>: The application attempting to access the Resource Owner&#8217;s resources<\/li>\n<li><strong>Resource Server<\/strong>: The server that stores the user&#8217;s resources<\/li>\n<li><strong>Authorization Server<\/strong>: The server that processes the user&#8217;s authentication information and issues tokens to the client<\/li>\n<\/ul>\n<h2>3. Setting Up a Spring Boot Project<\/h2>\n<p>\n    To start a Spring Boot application, first create a new project.<br \/>\n    You can generate a Maven or Gradle based project using Spring Initializr.\n<\/p>\n<p><strong>3.1 Adding Dependencies<\/strong><\/p>\n<p>\n    To implement OAuth2 authentication, you need to add the following dependencies:\n<\/p>\n<pre><code>dependencies {\n    implementation 'org.springframework.boot:spring-boot-starter-web'\n    implementation 'org.springframework.boot:spring-boot-starter-security'\n    implementation 'org.springframework.security.oauth.boot:spring-security-oauth2-autoconfigure:2.1.0.RELEASE'\n}<\/code><\/pre>\n<p><strong>3.2 Configuring application.properties<\/strong><\/p>\n<p>\n    Next, open the <code>src\/main\/resources\/application.properties<\/code> file to add the necessary configurations for OAuth2 authentication.<br \/>\n    Refer to the example configuration below.\n<\/p>\n<pre><code>\nspring.security.oauth2.client.registration.google.client-id={YOUR_CLIENT_ID}\nspring.security.oauth2.client.registration.google.client-secret={YOUR_CLIENT_SECRET}\nspring.security.oauth2.client.registration.google.scope=profile, email\nspring.security.oauth2.client.registration.google.redirect-uri=http:\/\/localhost:8080\/login\/oauth2\/code\/google\nspring.security.oauth2.client.provider.google.authorization-uri=https:\/\/accounts.google.com\/o\/oauth2\/auth\nspring.security.oauth2.client.provider.google.token-uri=https:\/\/oauth2.googleapis.com\/token\nspring.security.oauth2.client.provider.google.user-info-uri=https:\/\/www.googleapis.com\/oauth2\/v3\/userinfo\n<\/code><\/pre>\n<h2>4. Implementing OAuth2 Login<\/h2>\n<p>\n    Now, let&#8217;s implement OAuth2 login in the application. This can be easily configured through Spring Security.\n<\/p>\n<p><strong>4.1 Security Configuration<\/strong><\/p>\n<p>\n    Create a new Java class to configure the security settings.\n<\/p>\n<pre><code>import 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**\", \"\/error**\").permitAll()\n                .anyRequest().authenticated()\n                .and()\n            .oauth2Login();\n    }\n}<\/code><\/pre>\n<h2>5. Retrieving User Profile<\/h2>\n<p>\n    Once the user logs in, you can request user information from the OAuth2 server to retrieve the profile.<br \/>\n    We will write a controller for this purpose.\n<\/p>\n<pre><code>import org.springframework.security.core.annotation.AuthenticationPrincipal;\nimport org.springframework.security.oauth2.core.user.OAuth2User;\nimport org.springframework.stereotype.Controller;\nimport org.springframework.ui.Model;\nimport org.springframework.web.bind.annotation.GetMapping;\n\n@Controller\npublic class UserProfileController {\n    @GetMapping(\"\/user\")\n    public String user(@AuthenticationPrincipal OAuth2User principal, Model model) {\n        model.addAttribute(\"name\", principal.getAttribute(\"name\"));\n        model.addAttribute(\"email\", principal.getAttribute(\"email\"));\n        return \"userProfile\";\n    }\n}<\/code><\/pre>\n<h2>6. Implementing Logout<\/h2>\n<p>\n    The logout functionality is provided by Spring Security by default, and it can be easily implemented through configuration.<br \/>\n    Add the logout URL and related settings as shown below.\n<\/p>\n<pre><code>    @Override\n    protected void configure(HttpSecurity http) throws Exception {\n        http\n            .authorizeRequests()\n                .antMatchers(\"\/\", \"\/login**\", \"\/error**\").permitAll()\n                .anyRequest().authenticated()\n                .and()\n            .oauth2Login()\n                .and()\n            .logout()\n                .logoutSuccessUrl(\"\/\")\n                .invalidateHttpSession(true)\n                .deleteCookies(\"JSESSIONID\");\n    }<\/code><\/pre>\n<h2>7. Conclusion<\/h2>\n<p>\n    In this tutorial, we explored a simple way to implement login and logout functionalities using OAuth2 with Spring Boot.<br \/>\n    Using Spring Boot and OAuth2 allows for easy integration with external authentication systems, greatly enhancing application security and improving user experience.<br \/>\n    Consider applying OAuth2 to your projects to provide safer and more convenient services!\n<\/p>\n<h2>8. Additional Resources<\/h2>\n<p>\n    For more information, please refer to the official documentation and other resources.\n<\/p>\n<ul>\n<li><a href=\"https:\/\/spring.io\/projects\/spring-boot\">Spring Boot Official Documentation<\/a><\/li>\n<li><a href=\"https:\/\/spring.io\/projects\/spring-security\">Spring Security Official Documentation<\/a><\/li>\n<li><a href=\"https:\/\/oauth.net\/2\/\">OAuth 2.0 Official Documentation<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>1. Introduction Recently, with the increasing development of web applications based on microservices architecture and cloud services, the popularity of frameworks like Spring Boot is rising. Spring Boot is a framework that helps to quickly develop applications without complex configuration. In this tutorial, we will learn in detail how to implement login and logout functionalities &hellip; <a href=\"https:\/\/atmokpo.com\/w\/33009\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;Spring Boot Backend Development Course, Implementing Login and Logout with OAuth2, Writing OAuth2 Configuration Files&#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-33009","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, Writing OAuth2 Configuration Files - \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\/33009\/\" \/>\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, Writing OAuth2 Configuration Files - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"1. Introduction Recently, with the increasing development of web applications based on microservices architecture and cloud services, the popularity of frameworks like Spring Boot is rising. Spring Boot is a framework that helps to quickly develop applications without complex configuration. In this tutorial, we will learn in detail how to implement login and logout functionalities &hellip; \ub354 \ubcf4\uae30 &quot;Spring Boot Backend Development Course, Implementing Login and Logout with OAuth2, Writing OAuth2 Configuration Files&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/33009\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:13:05+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\/33009\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33009\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"Spring Boot Backend Development Course, Implementing Login and Logout with OAuth2, Writing OAuth2 Configuration Files\",\"datePublished\":\"2024-11-01T09:13:05+00:00\",\"dateModified\":\"2024-11-01T11:29:23+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33009\/\"},\"wordCount\":411,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Spring Boot backend development\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/33009\/\",\"url\":\"https:\/\/atmokpo.com\/w\/33009\/\",\"name\":\"Spring Boot Backend Development Course, Implementing Login and Logout with OAuth2, Writing OAuth2 Configuration Files - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:13:05+00:00\",\"dateModified\":\"2024-11-01T11:29:23+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33009\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/33009\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/33009\/#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, Writing OAuth2 Configuration Files\"}]},{\"@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, Writing OAuth2 Configuration Files - \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\/33009\/","og_locale":"ko_KR","og_type":"article","og_title":"Spring Boot Backend Development Course, Implementing Login and Logout with OAuth2, Writing OAuth2 Configuration Files - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"1. Introduction Recently, with the increasing development of web applications based on microservices architecture and cloud services, the popularity of frameworks like Spring Boot is rising. Spring Boot is a framework that helps to quickly develop applications without complex configuration. In this tutorial, we will learn in detail how to implement login and logout functionalities &hellip; \ub354 \ubcf4\uae30 \"Spring Boot Backend Development Course, Implementing Login and Logout with OAuth2, Writing OAuth2 Configuration Files\"","og_url":"https:\/\/atmokpo.com\/w\/33009\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:13:05+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\/33009\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/33009\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"Spring Boot Backend Development Course, Implementing Login and Logout with OAuth2, Writing OAuth2 Configuration Files","datePublished":"2024-11-01T09:13:05+00:00","dateModified":"2024-11-01T11:29:23+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/33009\/"},"wordCount":411,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Spring Boot backend development"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/33009\/","url":"https:\/\/atmokpo.com\/w\/33009\/","name":"Spring Boot Backend Development Course, Implementing Login and Logout with OAuth2, Writing OAuth2 Configuration Files - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:13:05+00:00","dateModified":"2024-11-01T11:29:23+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/33009\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/33009\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/33009\/#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, Writing OAuth2 Configuration Files"}]},{"@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\/33009","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=33009"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/33009\/revisions"}],"predecessor-version":[{"id":33010,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/33009\/revisions\/33010"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=33009"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=33009"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=33009"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}