OAuth 2.0 ( 카카오톡 로그인 구현 )

TopOfTheHead·2026년 5월 22일

Spring OAuth

목록 보기
3/12

카카오 개발자 센터에서 OAuth 2.0을 위한 생성 후 Client-IDClient Secret Key 확인

  • 카카오 개발자 센터 -> 앱 -> 앱 생성에서 앱 생성


  • 앱 -> 제품설정 -> 카카오로그인 -> 일반에서 사용 설정 : ON


  • 앱 -> 제품설정 -> 카카오로그인 -> 동의항목 -> 개인정보에서 요청사용자정보 권한을 설정
    。현재 단계에서는 닉네임 / 프로필 사진접근 가능하며, 추가적인 사용자정보가 필요한 경우 사업자 등록이 필요.

    사용자 정보보수적으로 최소한으로 가져오는게 좋다.
    개인정보( 생일 / 연령대 / 출생연도 / 휴대전화 등 )의 경우 DB에 저장 중, 노출되는 경우 책임을 개발자가 지므로.

  • 앱 -> 앱설정 -> 플랫폼 키에서 REST API 키 선택 후 카카오 로그인 리다이렉트 지정 및 클라이언트 시크릿 정보 확인

    카카오 로그인 리다이렉트 URI : http://localhost:8080/login/oauth/code/kakao로 설정

    Spring에서 OAuth2.0 설정 시
    클라이언트 ID : 카카오 REST API 키
    Client Secret Key : 카카오 로그인 코드

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 등 )에 의해 로그인 옵션이 자동으로 표시됨.

profile
공부기록 블로그

0개의 댓글