【发布时间】:2018-06-07 20:47:37
【问题描述】:
我有一个 userDetailsService 类型的 AuthenticationProvider,如下所示:
public class CustomUDS implements UserDetailsService {
@Override
public UserDetails loadUserByUsername(String clientId) throws UsernameNotFoundException, DataAccessException {
ClientDetails client=clientDetailsService.loadClientByClientId(clientId);
String password=client.getClientSecret();
boolean enabled=true;
boolean accountNonExpired=true;
boolean credentialsNonExpired=true;
boolean accountNonLocked=true;
List<GrantedAuthority> authorities=new ArrayList<GrantedAuthority>();
GrantedAuthority roleClient=new SimpleGrantedAuthority("ROLE_CLIENT");
authorities.add(roleClient);
return new User(clientId,password,enabled,accountNonExpired,credentialsNonExpired,accountNonLocked,authorities);
}
}
当用户登录并使用以下语句获取用户的用户名时,它返回用户类的字符串值:
Object object = SpringSecurityContext.getcontext().getAuthentication.getPrinciple();
对象值将是字符串
"org.springframework.security.core.userdetails.User@db343434: 用户名 ……”
我无法将返回对象转换为 User 或 UserDetails。
现在,我如何访问用户的用户名?
【问题讨论】:
-
原理?我想你的意思是校长。
标签: java authentication spring-security