Search in sources :

Example 1 with SecurityUser

use of com.paascloud.security.core.SecurityUser in project paascloud-master by paascloud.

the class UacUserMainController method user.

/**
 * User security user.
 *
 * @return the security user
 */
@GetMapping("/user")
public SecurityUser user() {
    String loginName = SecurityUtils.getCurrentLoginName();
    logger.info("{}", loginName);
    UacUser user = uacUserService.findByLoginName(loginName);
    return user == null ? null : new SecurityUser(user.getId(), user.getLoginName(), user.getLoginPwd(), user.getUserName(), user.getGroupId(), user.getGroupName());
}
Also used : UacUser(com.paascloud.provider.model.domain.UacUser) SecurityUser(com.paascloud.security.core.SecurityUser)

Example 2 with SecurityUser

use of com.paascloud.security.core.SecurityUser in project paascloud-master by paascloud.

the class UacUserDetailsServiceImpl method loadUserByUsername.

/**
 * Load user by username user details.
 *
 * @param username the username
 *
 * @return the user details
 */
@Override
public UserDetails loadUserByUsername(String username) {
    Collection<GrantedAuthority> grantedAuthorities;
    UacUser user = uacUserService.findByLoginName(username);
    if (user == null) {
        throw new BadCredentialsException("用户名不存在或者密码错误");
    }
    user = uacUserService.findUserInfoByUserId(user.getId());
    grantedAuthorities = uacUserService.loadUserAuthorities(user.getId());
    return new SecurityUser(user.getId(), user.getLoginName(), user.getLoginPwd(), user.getUserName(), user.getGroupId(), user.getGroupName(), user.getStatus(), grantedAuthorities);
}
Also used : UacUser(com.paascloud.provider.model.domain.UacUser) SecurityUser(com.paascloud.security.core.SecurityUser) GrantedAuthority(org.springframework.security.core.GrantedAuthority) BadCredentialsException(org.springframework.security.authentication.BadCredentialsException)

Example 3 with SecurityUser

use of com.paascloud.security.core.SecurityUser in project paascloud-master by paascloud.

the class PcAuthenticationSuccessHandler method onAuthenticationSuccess.

@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
    logger.info("登录成功");
    String header = request.getHeader(HttpHeaders.AUTHORIZATION);
    if (header == null || !header.startsWith(BEARER_TOKEN_TYPE)) {
        throw new UnapprovedClientAuthenticationException("请求头中无client信息");
    }
    String[] tokens = RequestUtil.extractAndDecodeHeader(header);
    assert tokens.length == 2;
    String clientId = tokens[0];
    String clientSecret = tokens[1];
    ClientDetails clientDetails = clientDetailsService.loadClientByClientId(clientId);
    if (clientDetails == null) {
        throw new UnapprovedClientAuthenticationException("clientId对应的配置信息不存在:" + clientId);
    } else if (!StringUtils.equals(clientDetails.getClientSecret(), clientSecret)) {
        throw new UnapprovedClientAuthenticationException("clientSecret不匹配:" + clientId);
    }
    TokenRequest tokenRequest = new TokenRequest(MapUtils.EMPTY_MAP, clientId, clientDetails.getScope(), "custom");
    OAuth2Request oAuth2Request = tokenRequest.createOAuth2Request(clientDetails);
    OAuth2Authentication oAuth2Authentication = new OAuth2Authentication(oAuth2Request, authentication);
    OAuth2AccessToken token = authorizationServerTokenServices.createAccessToken(oAuth2Authentication);
    SecurityUser principal = (SecurityUser) authentication.getPrincipal();
    uacUserService.handlerLoginData(token, principal, request);
    log.info("用户【 {} 】记录登录日志", principal.getUsername());
    response.setContentType("application/json;charset=UTF-8");
    response.getWriter().write((objectMapper.writeValueAsString(WrapMapper.ok(token))));
}
Also used : SecurityUser(com.paascloud.security.core.SecurityUser) UnapprovedClientAuthenticationException(org.springframework.security.oauth2.common.exceptions.UnapprovedClientAuthenticationException) OAuth2AccessToken(org.springframework.security.oauth2.common.OAuth2AccessToken)

Aggregations

SecurityUser (com.paascloud.security.core.SecurityUser)3 UacUser (com.paascloud.provider.model.domain.UacUser)2 BadCredentialsException (org.springframework.security.authentication.BadCredentialsException)1 GrantedAuthority (org.springframework.security.core.GrantedAuthority)1 OAuth2AccessToken (org.springframework.security.oauth2.common.OAuth2AccessToken)1 UnapprovedClientAuthenticationException (org.springframework.security.oauth2.common.exceptions.UnapprovedClientAuthenticationException)1