Spring Boot + Java + GitHub authentication login

I struggled with SSO using OAuth2, so I will leave it as a memorandum.

  1. Generate a client ID and client secret for "OAuth Apps" on GitHub.

  2. Open GitHub

  3. Press the profile image on the upper right and press "Settings"

  4. Press "Developer Settings" in the sidebar

  5. Press "OAuth Apps" in the sidebar

  6. Press the [New Auth App] button

  7. Enter the name of the application in "Application name"

  8. Enter the URL of the top page of the application being created in "Home page URL", and if it is being created in the local environment, "http: // localhost: 8080"

  9. Enter "http: // localhost: 8080/login/oauth2/code/github" in "Authorization callback URL"

  10. Make a note of the client ID and client secret when the creation is completed by the above procedure.

  11. Enter the following in application.yml

spring
  security:
    oauth2:
      client:
        registration:
          github:
            client-id:The client ID you wrote down
            client-secret:A copy of the client secret
            scope:
              - read:user
  1. Add the following dependency to pom.xml
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-oauth2-client</artifactId>
</dependency>
  1. Create a "configuration" package and create a configuration file in it
@Configuration
public class OAuth2ClientConfig extends WebSecurityConfigurerAdapter {

  @Override
  protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests()
                            .antMatchers("/")
                            .permitAll()
                            .antMatchers("/home")
                            .permitAll()
                            .antMatchers("/home/**").permitAll()
                            .and()
                            .oauth2Login()
                            .loginPage("/login");
  }

  @Override
  public void configure(WebSecurity web) throws Exception {
    web.ignoring().antMatchers("/css/**", "/img/**", "/js/**" ); 
  //Static files under the static folder are configured(WebSecurity web)Override with
  }

}
```java
@Controller
@RequestMapping("")
public class OAuth2ClientController {


  @GetMapping("")
  public String index(OAuth2AuthenticationToken authentication,
                      @AuthenticationPrincipal OAuth2User oauth2User,
                      Model model) {
      if(Objects.isNull(authentication)){
        return "index";
      }
 String userName = oauth2User.getAttributes (). get ("name"). toString (); // Get username from github account information
      model.addAttribute("userName", userName);
      return "index";
  }
}
<p th:text="${userName}"></p> 
 <!-Output: github username->

Recommended Posts

Spring Boot + Java + GitHub authentication login
[Java / Spring Boot] Spring security ④ --Implementation of login process
SNS login with Spring Boot
[Java] Thymeleaf Basic (Spring Boot)
CICS-Run Java application-(4) Spring Boot application
[Java] [Spring] Spring Boot 1.4-> 1.2 Downgrade Note
Use Basic Authentication with Spring Boot
Spring Boot Tutorial Using Spring Security Authentication
Elastic Beanstalk (Java) + Spring Boot + https
Java --Jersey Framework vs Spring Boot
Spring Java
[Java] LINE integration with Spring Boot
[Java] [Spring] Spring Boot Dependency injection mysterious hamarineta
Create Java Spring Boot project in IntelliJ
Spring Boot + Docker Java development environment construction
Achieve BASIC authentication with Spring Boot + Spring Security
Challenge Spring Boot
Spring Boot Form
Spring Boot Memorandum
gae + spring boot
[Java] Spring DI ③
Try LDAP authentication with Spring Security (Spring Boot) + OpenLDAP
Try to implement login function with Spring Boot
Java tips-Create a Spring Boot project in Gradle
[Java / Spring Boot] Spring security ⑤ --Implementation of logout processing
[Java] Hello World with Java 14 x Spring Boot 2.3 x JUnit 5 ~
[Java] Article to add validation with Spring Boot 2.3.1.
LINE Bot x Java (Spring Boot) construction procedure
[Introduction to Spring Boot] Authentication function with Spring Security
Execute arbitrary processing after Basic authentication with Spring boot.
SPRING BOOT learning record 01
Spring Boot + Heroku Postgres
About Spring Security authentication
[Java] [Spring Boot] Specify runtime profile --Spring Boot starting with NetBeans
Spring boot memo writing (1)
First Spring Boot (DI)
SPRING BOOT learning record 02
Spring Boot2 cheat sheet
Spring Boot exception handling
Spring Boot Servlet mapping
Spring boot development-development environment-
Spring Boot learning procedure
SSO with GitHub OAuth in Spring Boot 1.5.x environment
Learning Spring Boot [Beginning]
Spring boot memo writing (2)
Spring Boot 2.2 Document Summary
[Spring Boot] DataSourceProperties $ DataSourceBeanCreationException
Spring Boot 2.3 Application Availability
Spring boot tutorials Topics
Download with Spring Boot
[Java / Kotlin] Escape (sanitize) HTML5 support with unbescape [Spring Boot]
Handle Java 8 date and time API with Thymeleaf with Spring Boot
[Java] Deploy the Spring Boot application to Azure App Service
How to call and use API in Java (Spring Boot)
Introducing Spring Boot2, a Java framework for web development (for beginners)
Domain Driven Development with Java and Spring Boot ~ Layers and Modules ~
[Java] Sample project for developing web applications with Spring Boot
[Spring Boot] Environment construction (macOS)
Set context-param in Spring Boot
Try Spring Boot from 0 to 100.
Generate barcode with Spring Boot