下面列出了org.springframework.security.core.userdetails.UserDetailsService#loadUserByUsername ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 获取当前登录的用户
*
* @return UserDetails
*/
public static UserDetails getCurrentUser() {
final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication == null) {
throw new SkException(HttpStatus.UNAUTHORIZED, "当前登录状态过期");
}
if (authentication.getPrincipal() instanceof UserDetails) {
UserDetails userDetails = (UserDetails) authentication.getPrincipal();
UserDetailsService userDetailsService = SpringContextHolder.getBean(UserDetailsService.class);
return userDetailsService.loadUserByUsername(userDetails.getUsername());
}
throw new SkException(HttpStatus.UNAUTHORIZED, "找不到当前登录的信息");
}
protected Authentication attemptUserAuthentication(HttpServletRequest request, HttpServletResponse response,
Claims claims) {
String username = claims.getSubject();
if (StringUtils.isBlank(username)) {
log.error("username is blank");
throw new BadCredentialsException("username is blank.");
}
UserDetailsService userService = SpringApplicationContextUtil.getBean(UserDetailsService.class);
if (userService == null) {
log.error("user details service is not configured");
throw new InternalAuthenticationServiceException("user details service is not configured");
}
UserDetails userDetails = userService.loadUserByUsername(username);
if (userDetails == null) {
log.error("such user {} doesnt exist", username);
throw new UsernameNotFoundException("such user doesnt exist");
}
UsernamePasswordAuthenticationToken authToken = new UsernamePasswordAuthenticationToken(username, null,
userDetails.getAuthorities());
return authToken;
}
/**
* 获取当前登录的用户
* @return UserDetails
*/
public static UserDetails getCurrentUser() {
final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication == null) {
throw new BadRequestException(HttpStatus.UNAUTHORIZED, "当前登录状态过期");
}
if (authentication.getPrincipal() instanceof UserDetails) {
UserDetails userDetails = (UserDetails) authentication.getPrincipal();
UserDetailsService userDetailsService = SpringContextHolder.getBean(UserDetailsService.class);
return userDetailsService.loadUserByUsername(userDetails.getUsername());
}
throw new BadRequestException(HttpStatus.UNAUTHORIZED, "找不到当前登录的信息");
}
@SuppressWarnings({ "squid:S1166" })
private static UserDetails loadUserByUsername(final String username) {
final UserDetailsService userDetailsService = SpringContextHelper.getBean(UserDetailsService.class);
try {
return userDetailsService.loadUserByUsername(username);
} catch (final UsernameNotFoundException e) {
return new User(username, "", Collections.emptyList());
}
}
private UsernamePasswordAuthenticationToken authentication(ServletContext servletContext) {
ApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
UserDetailsService userDetailsService = userDetailsService(context);
UserDetails userDetails = userDetailsService.loadUserByUsername(this.username);
return new UsernamePasswordAuthenticationToken(
userDetails, userDetails.getPassword(), userDetails.getAuthorities());
}