Upgrade load user json dto if it is OAuth2Authentication

0.3
lishengzhao 2015-05-20 15:35:54 +08:00
parent 1a4c5c6bd2
commit b637fb08ec
2 changed files with 34 additions and 6 deletions

View File

@ -20,7 +20,7 @@ public class UserJsonDto implements Serializable {
private String phone; private String phone;
private String email; private String email;
private List<Privilege> privileges = new ArrayList<>(); private List<String> privileges = new ArrayList<>();
public UserJsonDto() { public UserJsonDto() {
} }
@ -32,7 +32,11 @@ public class UserJsonDto implements Serializable {
this.phone = user.phone(); this.phone = user.phone();
this.email = user.email(); this.email = user.email();
this.privileges = user.privileges();
final List<Privilege> privilegeList = user.privileges();
for (Privilege privilege : privilegeList) {
this.privileges.add(privilege.name());
}
} }
public boolean isArchived() { public boolean isArchived() {
@ -75,11 +79,11 @@ public class UserJsonDto implements Serializable {
this.email = email; this.email = email;
} }
public List<Privilege> getPrivileges() { public List<String> getPrivileges() {
return privileges; return privileges;
} }
public void setPrivileges(List<Privilege> privileges) { public void setPrivileges(List<String> privileges) {
this.privileges = privileges; this.privileges = privileges;
} }
} }

View File

@ -6,11 +6,16 @@ import cc.wdcy.domain.user.User;
import cc.wdcy.domain.user.UserRepository; import cc.wdcy.domain.user.UserRepository;
import cc.wdcy.service.UserService; import cc.wdcy.service.UserService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.Collection;
/** /**
* @author Shengzhao Li * @author Shengzhao Li
*/ */
@ -32,7 +37,26 @@ public class UserServiceImpl implements UserService {
@Override @Override
public UserJsonDto loadCurrentUserJsonDto() { public UserJsonDto loadCurrentUserJsonDto() {
final WdcyUserDetails userDetails = (WdcyUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
return new UserJsonDto(userRepository.findByGuid(userDetails.user().guid()));
if (authentication instanceof OAuth2Authentication && authentication.getPrincipal() instanceof String) {
return loadOauthUserJsonDto((OAuth2Authentication) authentication);
} else {
final WdcyUserDetails userDetails = (WdcyUserDetails) authentication.getPrincipal();
return new UserJsonDto(userRepository.findByGuid(userDetails.user().guid()));
}
}
private UserJsonDto loadOauthUserJsonDto(OAuth2Authentication oAuth2Authentication) {
UserJsonDto userJsonDto = new UserJsonDto();
userJsonDto.setUsername(oAuth2Authentication.getName());
final Collection<GrantedAuthority> authorities = oAuth2Authentication.getAuthorities();
for (GrantedAuthority authority : authorities) {
userJsonDto.getPrivileges().add(authority.getAuthority());
}
return userJsonDto;
} }
} }