네이버 개발자 센터에서어플리케이션 등록및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 Starter지정 시OAuth2 Client,Spring Security정의
。OAuth2 Client만 정의가 필요
application.yml 파일에 OAuth 2.0 설정 정보 정의
。카카오 OAuth2.0 Provider 설정 정보 참고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등 )에 의해로그인 옵션이 자동으로 표시됨.