Search in sources :

Example 1 with User

use of com.besscroft.aurora.mall.auth.domain.User in project aurora-mall by besscroft.

the class UserDetailsServiceImpl method loadUserByUsername.

@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
    String clientId = request.getParameter(AuthConstants.JWT_CLIENT_ID_KEY);
    UserDto userDto = null;
    switch(clientId) {
        case // 后台用户
        AuthConstants.ADMIN_CLIENT_ID:
            userDto = adminFeignClient.loadUserByUsername(username);
            break;
        case // 前台会员
        AuthConstants.PORTAL_CLIENT_ID:
            userDto = userFeignClient.loadUserByUsername(username);
            break;
    }
    if (userDto == null) {
        throw new UsernameNotFoundException(MessageConstant.USERNAME_PASSWORD_ERROR);
    }
    userDto.setClientId(clientId);
    User user = new User(userDto);
    if (!user.isEnabled()) {
        throw new DisabledException(MessageConstant.ACCOUNT_DISABLED);
    } else if (!user.isAccountNonLocked()) {
        throw new LockedException(MessageConstant.ACCOUNT_LOCKED);
    } else if (!user.isAccountNonExpired()) {
        throw new AccountExpiredException(MessageConstant.ACCOUNT_EXPIRED);
    } else if (!user.isCredentialsNonExpired()) {
        throw new CredentialsExpiredException(MessageConstant.CREDENTIALS_EXPIRED);
    }
    return user;
}
Also used : UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) User(com.besscroft.aurora.mall.auth.domain.User) LockedException(org.springframework.security.authentication.LockedException) CredentialsExpiredException(org.springframework.security.authentication.CredentialsExpiredException) AccountExpiredException(org.springframework.security.authentication.AccountExpiredException) UserDto(com.besscroft.aurora.mall.common.domain.UserDto) DisabledException(org.springframework.security.authentication.DisabledException)

Example 2 with User

use of com.besscroft.aurora.mall.auth.domain.User in project aurora-mall by besscroft.

the class JwtTokenEnhancer method enhance.

@Override
public OAuth2AccessToken enhance(OAuth2AccessToken oAuth2AccessToken, OAuth2Authentication oAuth2Authentication) {
    User user = (User) oAuth2Authentication.getPrincipal();
    Map<String, Object> info = new HashMap<>();
    // 把用户ID设置到JWT中
    info.put("id", user.getId());
    info.put("client_id", user.getClientId());
    ((DefaultOAuth2AccessToken) oAuth2AccessToken).setAdditionalInformation(info);
    return oAuth2AccessToken;
}
Also used : User(com.besscroft.aurora.mall.auth.domain.User) HashMap(java.util.HashMap) DefaultOAuth2AccessToken(org.springframework.security.oauth2.common.DefaultOAuth2AccessToken)

Aggregations

User (com.besscroft.aurora.mall.auth.domain.User)2 UserDto (com.besscroft.aurora.mall.common.domain.UserDto)1 HashMap (java.util.HashMap)1 AccountExpiredException (org.springframework.security.authentication.AccountExpiredException)1 CredentialsExpiredException (org.springframework.security.authentication.CredentialsExpiredException)1 DisabledException (org.springframework.security.authentication.DisabledException)1 LockedException (org.springframework.security.authentication.LockedException)1 UsernameNotFoundException (org.springframework.security.core.userdetails.UsernameNotFoundException)1 DefaultOAuth2AccessToken (org.springframework.security.oauth2.common.DefaultOAuth2AccessToken)1