Skip to content

Commit

Permalink
Revert "fix: authority 생성 로직 nonsocial social 분리"
Browse files Browse the repository at this point in the history
fix: authority mapper 삭제"
This reverts commit 670ba58.
  • Loading branch information
goathoon committed Oct 26, 2023
1 parent eaba013 commit c762eed
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 53 deletions.
74 changes: 33 additions & 41 deletions src/main/java/Funssion/Inforum/config/SecurityConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
import Funssion.Inforum.access_handler.JwtAuthenticationEntryPoint;
import Funssion.Inforum.access_handler.NonSocialLoginFailureHandler;
import Funssion.Inforum.domain.member.service.OAuthService;
import Funssion.Inforum.jwt.*;
import Funssion.Inforum.jwt.JwtSecurityConfig;
import Funssion.Inforum.jwt.TokenProvider;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
Expand All @@ -18,22 +19,13 @@
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
import org.springframework.security.config.annotation.web.configurers.HeadersConfigurer;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.mapping.GrantedAuthoritiesMapper;
import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository;
import org.springframework.security.oauth2.core.oidc.OidcIdToken;
import org.springframework.security.oauth2.core.oidc.OidcUserInfo;
import org.springframework.security.oauth2.core.oidc.user.OidcUserAuthority;
import org.springframework.security.oauth2.core.user.OAuth2UserAuthority;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

@Slf4j
@Configuration
Expand Down Expand Up @@ -136,35 +128,35 @@ CorsConfigurationSource corsConfigurationSource() {
return source;
}

@Bean
public GrantedAuthoritiesMapper userAuthoritiesMapper() {
return (authorities) -> {
Set<GrantedAuthority> mappedAuthorities = new HashSet<>();

authorities.forEach(authority -> {
if (OidcUserAuthority.class.isInstance(authority)) {
OidcUserAuthority oidcUserAuthority = (OidcUserAuthority) authority;

OidcIdToken idToken = oidcUserAuthority.getIdToken();
OidcUserInfo userInfo = oidcUserAuthority.getUserInfo();

// Map the claims found in idToken and/or userInfo
// to one or more GrantedAuthority's and add it to mappedAuthorities

} else if (OAuth2UserAuthority.class.isInstance(authority)) {
OAuth2UserAuthority oauth2UserAuthority = (OAuth2UserAuthority) authority;

Map<String, Object> userAttributes = oauth2UserAuthority.getAttributes();

// Map the attributes found in userAttributes
// to one or more GrantedAuthority's and add it to mappedAuthorities

}
});

return mappedAuthorities;


};
}
// @Bean
// public GrantedAuthoritiesMapper userAuthoritiesMapper() {
// return (authorities) -> {
// Set<GrantedAuthority> mappedAuthorities = new HashSet<>();
//
// authorities.forEach(authority -> {
// if (OidcUserAuthority.class.isInstance(authority)) {
// OidcUserAuthority oidcUserAuthority = (OidcUserAuthority) authority;
//
// OidcIdToken idToken = oidcUserAuthority.getIdToken();
// OidcUserInfo userInfo = oidcUserAuthority.getUserInfo();
//
// // Map the claims found in idToken and/or userInfo
// // to one or more GrantedAuthority's and add it to mappedAuthorities
//
// } else if (OAuth2UserAuthority.class.isInstance(authority)) {
// OAuth2UserAuthority oauth2UserAuthority = (OAuth2UserAuthority) authority;
//
// Map<String, Object> userAttributes = oauth2UserAuthority.getAttributes();
//
// // Map the attributes found in userAttributes
// // to one or more GrantedAuthority's and add it to mappedAuthorities
//
// }
// });
//
// return mappedAuthorities;
//
//
// };
// }
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
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 Down Expand Up @@ -34,21 +33,21 @@ public class CustomUserDetails implements UserDetails, OAuth2User, Serializable
public CustomUserDetails(String id, String roles, Map<String, Object> attributes) {
//PrincipalOauth2UserService 참고
this.id = id;
this.authorities = createAuthoritiesOfSocial(roles,attributes);
this.authorities = createAuthorities(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 = createAuthoritiesOfSocial(roles);
this.authorities = createAuthorities(roles);
this.email = userEmail;
this.password = userPw;
this.emailVerified = emailVerified;
this.locked = !locked;
}

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

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

for(String role : roles.split(",")){
if (!StringUtils.hasText(role)) continue;
authorities.add(new OAuth2UserAuthority(role,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,9 +40,11 @@ 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 c762eed

Please sign in to comment.