org.springframework.security.core.userdetails.UserDetailsService#loadUserByUsername ( )源码实例Demo

下面列出了org.springframework.security.core.userdetails.UserDetailsService#loadUserByUsername ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: sk-admin   文件: SecurityUtils.java
/**
 * 获取当前登录的用户
 *
 * @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;
}
 
源代码3 项目: eladmin   文件: SecurityUtils.java
/**
 * 获取当前登录的用户
 * @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, "找不到当前登录的信息");
}
 
源代码4 项目: hawkbit   文件: UserDetailsFormatter.java
@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());
}