Java Code Examples for org.springframework.security.core.userdetails.UserDetailsService#loadUserByUsername()

The following examples show how to use org.springframework.security.core.userdetails.UserDetailsService#loadUserByUsername() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SecurityUtils.java    From sk-admin with Apache License 2.0 5 votes vote down vote up
/**
 * 获取当前登录的用户
 *
 * @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, "找不到当前登录的信息");
}
 
Example 2
Source File: JwtSsoBasedRefreshTokenFilter.java    From wecube-platform with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 3
Source File: SecurityUtils.java    From eladmin with Apache License 2.0 5 votes vote down vote up
/**
 * 获取当前登录的用户
 * @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, "找不到当前登录的信息");
}
 
Example 4
Source File: UserDetailsFormatter.java    From hawkbit with Eclipse Public License 1.0 5 votes vote down vote up
@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());
    }
}
 
Example 5
Source File: SecurityRequestPostProcessors.java    From maven-framework-project with MIT License 5 votes vote down vote up
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());
}