Spring Project 생성
spring:
application:
name: demo-oauth2-trash
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/bblog
username: root
password: wjd747
jpa:
hibernate:
ddl-auto: create
security:
oauth2:
client:
registration:
kakao:
client-name: Kakao
client-id: ${카카오 REST API 키}
client-secret: ${카카오 로그인 코드}
authorization-grant-type: authorization_code
client-authentication-method: client_secret_post
redirect-uri: "{baseUrl}/{action}/oauth2/code/{registrationId}"
scope:
- profile_nickname
- profile_image
provider:
kakao:
authorization-uri: https://kauth.kakao.com/oauth/authorize
user-name-attribute: id
token-uri: https://kauth.kakao.com/oauth/token
user-info-uri: https://kapi.kakao.com/v2/user/me

▶ authorization-uri : 인가 코드 요청 : https://kauth.kakao.com/oauth/authorize

▶ token-uri : 토큰 요청 : https://kauth.kakao.com/oauth/token

▶ 사용자정보 조회 : https://kapi.kakao.com/v2/user/me
Spring Security에서 @Configuration 클래스 정의
@Configuration
public class SecurityConfig {
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http){
return http
.csrf(csrf -> csrf.disable())
.cors( cors -> cors.disable() )
.oauth2Login(Customizer.withDefaults())
.authorizeHttpRequests(
auth -> auth
.requestMatchers("/login", "/sign-up")
.anonymous()
.requestMatchers("/user/**")
.hasAnyAuthority("USER", "ADMIN")
.requestMatchers("/admin/**")
.hasAuthority("ADMIN")
.anyRequest()
.authenticated()
).build();
}
}
HTTPSecurity객체.oauth2Login(Customizer.withDefaults())
。Spring Security에서 Oauth 2.0 로그인 기능을 활성화하는 메서드

▶ 다음 설정을 끝낸 후 localhost:8080/login 접속 시 해당 API를 보호하기위해 application.yml에 등록된 OAuth2 Provider ( ex. kakao 등 )에 의해 로그인 옵션이 자동으로 표시됨.