Skip to content

Commit 1e4f192

Browse files
committed
feat kookmin-sw#2 - OAuth2UserService 기능 구현
로그인 시 카카오 혹은 네이버에서 받아온 사용자 정보를 이용하여 회원가입이 안되어있는 사용자의 경우 save 시키고, 이미 가입된 회원이라면 SharedPostPrincipal 객체 반환하도록 함
1 parent 32e44e6 commit 1e4f192

File tree

1 file changed

+25
-59
lines changed

1 file changed

+25
-59
lines changed
Lines changed: 25 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
1-
package org.capstone.maru.service;
1+
package org.capstone.maru.security;
22

3-
import java.util.Collections;
4-
import java.util.Map;
53
import lombok.RequiredArgsConstructor;
6-
import org.capstone.maru.domain.MemberAccount;
7-
import org.capstone.maru.dto.CustomOAuth2User;
8-
import org.capstone.maru.dto.OAuthAttributes;
9-
import org.capstone.maru.dto.SocialType;
10-
import org.capstone.maru.repository.MemberAccountRepository;
11-
import org.springframework.security.core.authority.SimpleGrantedAuthority;
4+
import org.capstone.maru.security.constant.SocialType;
5+
import org.capstone.maru.service.MemberAccountService;
126
import org.springframework.security.oauth2.client.userinfo.DefaultOAuth2UserService;
137
import org.springframework.security.oauth2.client.userinfo.OAuth2UserRequest;
148
import org.springframework.security.oauth2.client.userinfo.OAuth2UserService;
@@ -20,70 +14,42 @@
2014
@Service
2115
public class CustomOAuth2UserService implements OAuth2UserService<OAuth2UserRequest, OAuth2User> {
2216

23-
private final MemberAccountRepository userRepository;
24-
private static final String NAVER = "naver";
25-
private static final String KAKAO = "kakao";
17+
private final MemberAccountService memberAccountService;
2618

2719
@Override
2820
public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2AuthenticationException {
29-
3021
OAuth2UserService<OAuth2UserRequest, OAuth2User> delegate = new DefaultOAuth2UserService();
3122
OAuth2User oAuth2User = delegate.loadUser(userRequest);
3223

33-
/**
34-
* userRequest에서 registrationId 추출 후 registrationId으로 SocialType 저장
35-
* http://localhost:8080/oauth2/authorization/kakao에서 kakao가 registrationId
36-
* userNameAttributeName은 이후에 nameAttributeKey로 설정된다.
37-
*/
3824
String registrationId = userRequest.getClientRegistration().getRegistrationId();
3925
SocialType socialType = getSocialType(registrationId);
40-
String userNameAttributeName = userRequest.getClientRegistration()
41-
.getProviderDetails().getUserInfoEndpoint()
42-
.getUserNameAttributeName(); // OAuth2 로그인 시 키(PK)가 되는 값
43-
Map<String, Object> attributes = oAuth2User.getAttributes(); // 소셜 로그인에서 API가 제공하는 userInfo의 Json 값(유저 정보들)
44-
45-
// socialType에 따라 유저 정보를 통해 OAuthAttributes 객체 생성
46-
OAuthAttributes extractAttributes = OAuthAttributes.of(socialType, userNameAttributeName,
47-
attributes);
4826

49-
MemberAccount createdUser = getUser(extractAttributes, socialType);
50-
51-
// DefaultOAuth2User를 구현한 CustomOAuth2User 객체를 생성해서 반환
52-
return new CustomOAuth2User(
53-
Collections.singleton(new SimpleGrantedAuthority(createdUser.getRole().getKey())),
54-
attributes,
55-
extractAttributes.getNameAttributeKey(),
56-
createdUser.getEmail()
27+
OAuth2Response extractAttributes = OAuth2Response.of(
28+
socialType,
29+
oAuth2User.getAttributes()
5730
);
58-
}
5931

60-
private SocialType getSocialType(String registrationId) {
61-
if (NAVER.equals(registrationId)) {
62-
return SocialType.NAVER;
63-
}
64-
if (KAKAO.equals(registrationId)) {
65-
return SocialType.KAKAO;
66-
}
67-
return null;
32+
String memberId = getMemberId(registrationId, extractAttributes);
33+
34+
return memberAccountService
35+
.searchMember(memberId)
36+
.map(SharedPostPrincipal::from)
37+
.orElseGet(() ->
38+
SharedPostPrincipal.from(
39+
memberAccountService.saveUser(
40+
memberId,
41+
extractAttributes.email(),
42+
extractAttributes.nickname()
43+
)
44+
)
45+
);
6846
}
6947

70-
private MemberAccount getUser(OAuthAttributes attributes, SocialType socialType) {
71-
MemberAccount findUser = userRepository.findBySocialTypeAndSocialId(socialType,
72-
attributes.getOauth2UserInfo().getId()).orElse(null);
73-
74-
if (findUser == null) {
75-
return saveUser(attributes, socialType);
76-
}
77-
return findUser;
48+
private SocialType getSocialType(String registrationId) {
49+
return SocialType.of(registrationId);
7850
}
7951

80-
/**
81-
* OAuthAttributes의 toEntity() 메소드를 통해 빌더로 User 객체 생성 후 반환 생성된 User 객체를 DB에 저장 : socialType,
82-
* socialId, email, role 값만 있는 상태
83-
*/
84-
private MemberAccount saveUser(OAuthAttributes attributes, SocialType socialType) {
85-
MemberAccount createdUser = attributes.toEntity(socialType, attributes.getOauth2UserInfo());
86-
return userRepository.save(createdUser);
52+
private String getMemberId(String registrationId, OAuth2Response oAuth2Response) {
53+
return registrationId + "_" + oAuth2Response.id();
8754
}
88-
8955
}

0 commit comments

Comments
 (0)