diff --git a/src/main/java/Funssion/Inforum/domain/member/entity/CustomUserDetails.java b/src/main/java/Funssion/Inforum/domain/member/entity/CustomUserDetails.java index 4cc78a77..26d23f3c 100644 --- a/src/main/java/Funssion/Inforum/domain/member/entity/CustomUserDetails.java +++ b/src/main/java/Funssion/Inforum/domain/member/entity/CustomUserDetails.java @@ -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; @@ -30,24 +31,24 @@ public class CustomUserDetails implements UserDetails, OAuth2User, Serializable private Map attributes; //Social Login 용 - public CustomUserDetails(String id, String roles, Map attributes) { + public CustomUserDetails(String id`, String roles, Map 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 createAuthorities(String roles){ + private Collection createAuthoritiesOfNonSocial(String roles){ Collection authorities = new ArrayList<>(); for(String role : roles.split(",")){ @@ -56,6 +57,15 @@ private Collection createAuthorities(String roles){ } return authorities; } + private Collection createAuthoritiesOfSocial(String roles){ + Collection 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 getAttributes() { return attributes; diff --git a/src/main/java/Funssion/Inforum/domain/member/service/OAuthService.java b/src/main/java/Funssion/Inforum/domain/member/service/OAuthService.java index 10a08c60..48c0a801 100644 --- a/src/main/java/Funssion/Inforum/domain/member/service/OAuthService.java +++ b/src/main/java/Funssion/Inforum/domain/member/service/OAuthService.java @@ -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()); } }