Skip to content

Commit

Permalink
Merge pull request #152 from Funssion-SWM/user
Browse files Browse the repository at this point in the history
fix: authority 생성 로직 nonsocial social 분리
  • Loading branch information
goathoon authored Oct 26, 2023
2 parents 925fe8b + 670ba58 commit 051b34a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.oauth2.core.user.OAuth2User;
import org.springframework.security.oauth2.core.user.OAuth2UserAuthority;
import org.springframework.util.StringUtils;

import java.io.Serializable;
Expand All @@ -30,24 +31,24 @@ public class CustomUserDetails implements UserDetails, OAuth2User, Serializable
private Map<String, Object> attributes;

//Social Login 용
public CustomUserDetails(String id, String roles, Map<String, Object> attributes) {
public CustomUserDetails(String id`, String roles, Map<String, Object> attributes) {
//PrincipalOauth2UserService 참고
this.id = id;
this.authorities = createAuthorities(roles);
this.authorities = createAuthoritiesOfSocial(roles);
this.attributes = attributes;
}

//Non Social + Employer 로그인 용도
public CustomUserDetails(Long authId, String roles, String userEmail, String userPw, boolean emailVerified, boolean locked) {
this.id = String.valueOf(authId);
this.authorities = createAuthorities(roles);
this.authorities = createAuthoritiesOfSocial(roles);
this.email = userEmail;
this.password = userPw;
this.emailVerified = emailVerified;
this.locked = !locked;
}

private Collection<GrantedAuthority> createAuthorities(String roles){
private Collection<GrantedAuthority> createAuthoritiesOfNonSocial(String roles){
Collection<GrantedAuthority> authorities = new ArrayList<>();

for(String role : roles.split(",")){
Expand All @@ -56,6 +57,15 @@ private Collection<GrantedAuthority> createAuthorities(String roles){
}
return authorities;
}
private Collection<GrantedAuthority> createAuthoritiesOfSocial(String roles){
Collection<GrantedAuthority> authorities = new ArrayList<>();

for(String role : roles.split(",")){
if (!StringUtils.hasText(role)) continue;
authorities.add(new OAuth2UserAuthority(role,this.attributes));
}
return authorities;
}
@Override
public Map<String, Object> getAttributes() {
return attributes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,9 @@ public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2Authentic
SocialMember savedSocialMember = SocialMember.createSocialMember(email, nickname);
SaveMemberResponseDto savedResponse = memberRepository.save(savedSocialMember);
String roles = Role.addRole(Role.getIncludingRoles(savedResponse.getRole()), Role.OAUTH_FIRST_JOIN);// 최초 회원가입을 위한 임시 role 추가
log.info("roles in load user = {}",roles);
return new CustomUserDetails(String.valueOf(savedResponse.getId()),roles,oAuth2User.getAttributes());
}
else{
log.info("roles in load user = {}",Role.getIncludingRoles(socialMember.get().getRole()));
return new CustomUserDetails(String.valueOf(socialMember.get().getUserId()),Role.getIncludingRoles(socialMember.get().getRole()),oAuth2User.getAttributes());
}
}
Expand Down

0 comments on commit 051b34a

Please sign in to comment.