스프링 시큐리티 OAuth2/OAuth2LoginConfigurer 초기화 이해

OAuth2 로그인 구현 - OAuth 2.0 Login Page 생성

webmaster 2023. 1. 16. 23:38
728x90

OAuth 2.0 로그인 페이지 자동 생성

OAuth 2.0 로그인 페이지 자동 생성

  • 기본적으로 OAuth 2.0 로그인 페이지는 DefaultLoginPageGeneratingFilter 가 자동으로 생성해 준다
  • 이 디폴트 로그인 페이지는 OAuth 2.0 클라이언트명을 보여준다
  • 링크를 누르면 인가 요청을 (또는 OAuth 2.0 로그인을) 시작할 수 있다
  • 요청 매핑 Url
    • RequestMatcher : /oauth2/authorization/{registrationId}*
    • 디폴트 로그인 페이지를 재정의하려면 oauth2Login().loginPage() 사용하면 된다

TEST

DefaultLoginPage 이동

@EnableWebSecurity
public class OAuth2ClientConfig {

  @Bean
  public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
    http.authorizeHttpRequests(authRequest -> authRequest
        .anyRequest().authenticated());
    http.oauth2Login(Customizer.withDefaults());
    return http.build();
  }
}

디폴트 로그인 페이지

  • defaultLoginPage로 이동한다.
  • 로그인하고자 하는 클라이언트 클릭 시 해당 클라이언트 로그인 화면으로 이동한다.

CustomLoginPage 이동

@EnableWebSecurity
public class OAuth2ClientConfig {

  @Bean
  public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
    http.authorizeHttpRequests(authRequest -> authRequest
        .antMatchers("/loginPage").permitAll()
        .anyRequest().authenticated());
    http.oauth2Login(oauth2 -> oauth2.loginPage("/loginPage"));//여기서 Customizer한 설정을 할 수 있다.
    return http.build();
  }
}

loginController

@RestController
public class LoginController {

  @GetMapping("/loginPage")
  public String loginPage() {
    return "loginPage";
  }
}
  • customLoginPage로 이동
728x90