OAuth 2.0 ( 네이버 로그인 구현 )

TopOfTheHead·2026년 5월 22일

Spring OAuth

목록 보기
4/12

네이버 개발자 센터에서 어플리케이션 등록Client-ID / Client Secret Key 확인

  • 네이버 개발자 센터에서 Products -> 네이버 로그인 -> 네이버 로그인 API로 접근 후 오픈 API 이용 신청 수행


  • 어플리케이션 등록

    사용 API : 클라이언트요청사용자 정보에 대한 권한을 설정

    로그인 오픈 API 서비스 환경 :
    서비스 URL : 현재 백엔드 서버 URL : http://localhost:8080
    Callback URL : Redirection URI : http://localhost:8080/login/oauth2/code/naver

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

  • 어플리케이션 생성 및 Client-ID / Client Secret Key 확인

Spring Project 생성

spring:
  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:
          naver:
            client-name: Naver
            client-id: ${네이버 Client-ID}
            client-secret: ${네이버 Client-Secret Key}
            authorization-grant-type: authorization_code
            client-authentication-method: client_secret_post
            redirect-uri: "{baseUrl}/{action}/oauth2/code/{registrationId}"
            scope:
              # https://developers.naver.com/docs/login/profile/profile.md - 출력결과
              - id
              - nickname
				....
        provider:
          naver:
            # https://developers.naver.com/docs/login/api/api.md
            authorization-uri: https://nid.naver.com/oauth2.0/authorize # API 기본 정보
            user-name-attribute: response
            token-uri: https://nid.naver.com/oauth2.0/token # API 기본 정보
            user-info-uri: https://openapi.naver.com/v1/nid/me #자원서버

scope 정보는 다음 페이지에서 확인 가능
네이버 회원 프로필 조회 API 명세


네이버 로그인 API 명세
authorization-uri : 인가 코드 요청 : https://nid.naver.com/oauth2.0/authorize
token-uri : 토큰 요청 : https://nid.naver.com/oauth2.0/token

user-info-uri : 회원 프로필 조회 : https://openapi.naver.com/v1/nid/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개의 댓글