Skip to content

Commit 8771330

Browse files
committed
Feat: 네이버 로그인 구현
1 parent b5a9f64 commit 8771330

File tree

3 files changed

+56
-2
lines changed

3 files changed

+56
-2
lines changed

src/main/java/com/back/global/security/oauth/CustomOAuth2UserService.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@
2828
@Service
2929
@RequiredArgsConstructor
3030
public class CustomOAuth2UserService extends DefaultOAuth2UserService {
31-
3231
private final UserRepository userRepository;
33-
private final UserProfileRepository userProfileRepository;
3432

3533
@Override
3634
public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2AuthenticationException {
@@ -45,6 +43,7 @@ public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2Authentic
4543
// 소셜 제공자별로 사용자 정보 매핑
4644
OAuth2UserInfo userInfo = switch (registrationId) {
4745
case "kakao" -> new KakaoOAuth2UserInfo(attributes);
46+
case "naver" -> new NaverOAuth2UserInfo(attributes);
4847
default -> throw new CustomException(ErrorCode.UNSUPPORTED_OAUTH_PROVIDER);
4948
};
5049

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.back.global.security.oauth;
2+
3+
import java.util.Map;
4+
5+
/**
6+
* 네이버 OAuth2 사용자 정보 구현체
7+
*/
8+
public class NaverOAuth2UserInfo implements OAuth2UserInfo {
9+
private final Map<String, Object> attributes;
10+
11+
public NaverOAuth2UserInfo(Map<String, Object> attributes) {
12+
this.attributes = attributes;
13+
}
14+
15+
@Override
16+
public String getProvider() {
17+
return "naver";
18+
}
19+
20+
@Override
21+
public String getProviderId() {
22+
Map<String, Object> response = (Map<String, Object>) attributes.get("response");
23+
return response != null ? (String) response.get("id") : null;
24+
}
25+
26+
@Override
27+
public String getEmail() {
28+
Map<String, Object> response = (Map<String, Object>) attributes.get("response");
29+
return response != null ? (String) response.get("email") : null;
30+
}
31+
32+
@Override
33+
public String getNickname() {
34+
Map<String, Object> response = (Map<String, Object>) attributes.get("response");
35+
return response != null ? (String) response.get("nickname") : null;
36+
}
37+
38+
@Override
39+
public String getProfileImageUrl() {
40+
Map<String, Object> response = (Map<String, Object>) attributes.get("response");
41+
return response != null ? (String) response.get("profile_image") : null;
42+
}
43+
}

src/main/resources/application-dev.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,24 @@ spring:
3434
redirect-uri: "{baseUrl}/login/oauth2/code/{registrationId}"
3535
scope: profile_nickname, profile_image, account_email
3636
client-name: Kakao
37+
naver:
38+
client-id: ${NAVER_CLIENT_ID}
39+
client-secret: ${NAVER_CLIENT_SECRET}
40+
authorization-grant-type: authorization_code
41+
redirect-uri: "{baseUrl}/login/oauth2/code/{registrationId}"
42+
scope: email, nickname, profile_image
43+
client-name: Naver
3744
provider:
3845
kakao:
3946
authorization-uri: https://kauth.kakao.com/oauth/authorize
4047
token-uri: https://kauth.kakao.com/oauth/token
4148
user-info-uri: https://kapi.kakao.com/v2/user/me
4249
user-name-attribute: id
50+
naver:
51+
authorization-uri: https://nid.naver.com/oauth2.0/authorize
52+
token-uri: https://nid.naver.com/oauth2.0/token
53+
user-info-uri: https://openapi.naver.com/v1/nid/me
54+
user-name-attribute: response
4355

4456
springdoc:
4557
default-produces-media-type: application/json;charset=UTF-8

0 commit comments

Comments
 (0)