-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
386 additions
and
150 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
...ication/src/main/java/com/devtoon/jtoon/security/application/CustomOAuth2UserService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package com.devtoon.jtoon.security.application; | ||
|
||
import org.springframework.security.core.authority.SimpleGrantedAuthority; | ||
import org.springframework.security.oauth2.client.userinfo.DefaultOAuth2UserService; | ||
import org.springframework.security.oauth2.client.userinfo.OAuth2UserRequest; | ||
import org.springframework.security.oauth2.client.userinfo.OAuth2UserService; | ||
import org.springframework.security.oauth2.core.OAuth2AuthenticationException; | ||
import org.springframework.security.oauth2.core.user.OAuth2User; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import com.devtoon.jtoon.member.entity.LoginType; | ||
import com.devtoon.jtoon.member.entity.Member; | ||
import com.devtoon.jtoon.member.repository.MemberRepository; | ||
import com.devtoon.jtoon.security.domain.jwt.MemberThreadLocal; | ||
import com.devtoon.jtoon.security.domain.oauth.CustomOAuth2User; | ||
import com.devtoon.jtoon.security.domain.oauth.OAuthAttributes; | ||
import java.util.Collections; | ||
import java.util.Map; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Transactional(readOnly = true) | ||
public class CustomOAuth2UserService implements OAuth2UserService<OAuth2UserRequest, OAuth2User> { | ||
|
||
private final MemberRepository memberRepository; | ||
|
||
@Override | ||
@Transactional | ||
public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2AuthenticationException { | ||
OAuth2UserService<OAuth2UserRequest, OAuth2User> delegate = new DefaultOAuth2UserService(); | ||
OAuth2User oauth2User = delegate.loadUser(userRequest); | ||
String registrationId = userRequest.getClientRegistration().getRegistrationId(); | ||
LoginType loginType = LoginType.from(registrationId); | ||
String userNameAttributeName = userRequest.getClientRegistration() | ||
.getProviderDetails() | ||
.getUserInfoEndpoint() | ||
.getUserNameAttributeName(); | ||
Map<String, Object> attributes = oauth2User.getAttributes(); | ||
OAuthAttributes extractedAttributes = OAuthAttributes.of(loginType, userNameAttributeName, attributes); | ||
Member member = generateMember(extractedAttributes); | ||
MemberThreadLocal.setMember(member); | ||
|
||
return new CustomOAuth2User( | ||
Collections.singleton(new SimpleGrantedAuthority(member.getRole().toString())), | ||
attributes, | ||
userNameAttributeName | ||
); | ||
} | ||
|
||
private Member generateMember(OAuthAttributes extractedAttributes) { | ||
Member member = memberRepository.findByEmail(extractedAttributes.email()) | ||
.orElse(null); | ||
|
||
if (member == null) { | ||
return memberRepository.save(extractedAttributes.toEntity()); | ||
} | ||
|
||
if (extractedAttributes.loginType() != member.getLoginType()) { | ||
throw new RuntimeException("이미 다른 소셜 로그인으로 등록된 회원입니다"); | ||
} | ||
|
||
return member; | ||
} | ||
} | ||
|
4 changes: 2 additions & 2 deletions
4
...application/CustomUserDetailsService.java → ...application/CustomUserDetailsService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...ecurity/jwt/domain/CustomUserDetails.java → ...ecurity/domain/jwt/CustomUserDetails.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...ecurity/jwt/domain/MemberThreadLocal.java → ...ecurity/domain/jwt/MemberThreadLocal.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
...e-application/src/main/java/com/devtoon/jtoon/security/domain/oauth/CustomOAuth2User.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.devtoon.jtoon.security.domain.oauth; | ||
|
||
import org.springframework.security.core.GrantedAuthority; | ||
import org.springframework.security.oauth2.core.user.DefaultOAuth2User; | ||
|
||
import java.util.Collection; | ||
import java.util.Map; | ||
|
||
public class CustomOAuth2User extends DefaultOAuth2User { | ||
|
||
public CustomOAuth2User( | ||
Collection<? extends GrantedAuthority> authorities, | ||
Map<String, Object> attributes, | ||
String nameAttributeKey | ||
) { | ||
super(authorities, attributes, nameAttributeKey); | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
...le-application/src/main/java/com/devtoon/jtoon/security/domain/oauth/OAuthAttributes.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package com.devtoon.jtoon.security.domain.oauth; | ||
|
||
import com.devtoon.jtoon.member.entity.Gender; | ||
import com.devtoon.jtoon.member.entity.LoginType; | ||
import com.devtoon.jtoon.member.entity.Member; | ||
import com.devtoon.jtoon.member.entity.Role; | ||
import java.util.Map; | ||
import java.util.UUID; | ||
import lombok.AccessLevel; | ||
import lombok.Builder; | ||
|
||
@Builder(access = AccessLevel.PRIVATE) | ||
public record OAuthAttributes( | ||
String nameAttributeKey, | ||
String email, | ||
String name, | ||
String nickname, | ||
String mobile, | ||
String gender, | ||
String password, | ||
LoginType loginType | ||
) { | ||
|
||
public static OAuthAttributes of(LoginType loginType, String nameAttributeKey, Map<String, Object> attributes) { | ||
return switch (loginType) { | ||
case NAVER -> ofNaver(loginType, nameAttributeKey, attributes); | ||
case KAKAO -> ofKakao(loginType, nameAttributeKey, attributes); | ||
case LOCAL -> throw new IllegalArgumentException("안돼"); | ||
}; | ||
} | ||
|
||
private static OAuthAttributes ofNaver( | ||
LoginType loginType, | ||
String nameAttributeKey, | ||
Map<String, Object> attributes | ||
) { | ||
Map<String, Object> response = (Map<String, Object>)attributes.get("response"); | ||
String naverEmail = (String)response.get("email"); | ||
String naverNickname = response.get("nickname") == null ? naverEmail : (String)response.get("nickname"); | ||
String naverPhone = ((String)response.get("mobile")).replace("-", ""); | ||
|
||
return OAuthAttributes.builder() | ||
.nameAttributeKey(nameAttributeKey) | ||
.email(naverEmail) | ||
.nickname(naverNickname) | ||
.mobile(naverPhone) | ||
.name((String)response.get("name")) | ||
.gender((String)response.get("gender")) | ||
.loginType(loginType) | ||
.build(); | ||
} | ||
|
||
private static OAuthAttributes ofKakao(LoginType loginType, String nameAttributeKey, | ||
Map<String, Object> attributes) { | ||
// TODO : KAKAO 추입 도입 예정 | ||
return null; | ||
} | ||
|
||
public Member toEntity() { | ||
return Member.builder() | ||
.email(this.email) | ||
.password(UUID.randomUUID().toString()) | ||
.name(this.name) | ||
.nickname(this.nickname) | ||
.gender(Gender.from(this.gender)) | ||
.phone(this.mobile) | ||
.role(Role.USER) | ||
.loginType(this.loginType) | ||
.build(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.