네이버 로그인 추가

박찬미·2022년 2월 13일
0

Spring Boot

목록 보기
11/17

네이버 API 등록

네이버 오픈 API로 이동
네이버 오픈 API 링크


회원이름, 이메일, 프로필 사진은 필수고 나머지는 추가로 선택 가능


서비스 url 필수
Callback URL은 구글에서 등록한 리디렉션 URL과 같은 역할

  • application-oauth.properties

네이버에서 받은 키 값들을 등록
네이버에서는 스프링 시큐리티를 공식 지원하지 않기 때문에 그동안 Common-OAuth2Provider에서 해주던 값들도 전부 수동으로 입력해야 한다.

spring.security.oauth2.client.registration.naver.client-id=받은 클라이언트 id
spring.security.oauth2.client.registration.naver.client-secret=받은 클라이언트 secret
spring.security.oauth2.client.registration.naver.redirect-uri={baseUrl}/{action}/oauth2/code/{registrationId}
spring.security.oauth2.client.registration.naver.authorization-grant-type=authorization_code
spring.security.oauth2.client.registration.naver.scope=name,email,profile_image
spring.security.oauth2.client.registration.naver.client-name=Naver
spring.security.oauth2.client.provider.naver.authorization-uri=https://nid.naver.com/oauth2.0/authorize
spring.security.oauth2.client.provider.naver.token-uri=https://nid.naver.com/oauth2.0/token
spring.security.oauth2.client.provider.naver.user-info-uri=https://openapi.naver.com/v1/nid/me
spring.security.oauth2.client.provider.naver.user-name-attribute=response

위에 설정할 때 공백 조심


네이버 오픈 API의 로그인 회원 결과는 이런 형식이다.

스프링 시큐리티에서는 하위 필드를 명시할 수 없다. 최상위 필드들만 user_name으로 지정 가능하다. 하지만 네이버의 응답 값 최상위 필드는 resultCode, message, response이다.

이러한 이유로 스프링 시큐리티에서 인식 가능한 필드는 저 3개 중에 골라야 한다. 본문에서 담고 있는 response를 user_name으로 지정하고, 이후 자바 코드로 response의 id를 user_name으로 지정하겠다.



스프링 시큐리티 설정 등록

  • OAuthAttributes

  • index.mustache

네이버 부분 추가

/outh2/authorization/naver :
네이버 로그인 URL은 application-oauth.properties에 등록한 redirect-uri 값에 맞춰 자동으로 등록된다.
/oauth2/authorization/까지는 고정이고 마지막 Path만 각 소셜 로그인 코드를 사용하면 된다. 여기서는 naver가 마지막 Path

성공

0개의 댓글