Search in sources :

Example 1 with UserTokenDto

use of com.paascloud.base.dto.UserTokenDto in project paascloud-master by paascloud.

the class UacUserTokenServiceImpl method getByAccessToken.

@Override
public UserTokenDto getByAccessToken(String accessToken) {
    UserTokenDto userTokenDto = (UserTokenDto) redisTemplate.opsForValue().get(RedisKeyUtil.getAccessTokenKey(accessToken));
    if (userTokenDto == null) {
        UacUserToken uacUserToken = new UacUserToken();
        uacUserToken.setAccessToken(accessToken);
        uacUserToken = uacUserTokenMapper.selectOne(uacUserToken);
        userTokenDto = new ModelMapper().map(uacUserToken, UserTokenDto.class);
    }
    return userTokenDto;
}
Also used : UserTokenDto(com.paascloud.base.dto.UserTokenDto) UacUserToken(com.paascloud.provider.model.domain.UacUserToken) ModelMapper(org.modelmapper.ModelMapper)

Example 2 with UserTokenDto

use of com.paascloud.base.dto.UserTokenDto in project paascloud-master by paascloud.

the class UacUserTokenServiceImpl method saveUserToken.

@Override
public void saveUserToken(String accessToken, String refreshToken, LoginAuthDto loginAuthDto, HttpServletRequest request) {
    // 获取登录时间
    Long userId = loginAuthDto.getUserId();
    UacUser uacUser = uacUserService.selectByKey(userId);
    final UserAgent userAgent = UserAgent.parseUserAgentString(request.getHeader("User-Agent"));
    // 获取客户端操作系统
    final String os = userAgent.getOperatingSystem().getName();
    // 获取客户端浏览器
    final String browser = userAgent.getBrowser().getName();
    final String remoteAddr = RequestUtil.getRemoteAddr(request);
    // 根据IP获取位置信息
    final String remoteLocation = opcRpcService.getLocationById(remoteAddr);
    // 存入mysql数据库
    UacUserToken uacUserToken = new UacUserToken();
    OAuth2ClientProperties[] clients = securityProperties.getOauth2().getClients();
    int accessTokenValidateSeconds = clients[0].getAccessTokenValidateSeconds();
    int refreshTokenValiditySeconds = clients[0].getRefreshTokenValiditySeconds();
    uacUserToken.setOs(os);
    uacUserToken.setBrowser(browser);
    uacUserToken.setAccessToken(accessToken);
    uacUserToken.setAccessTokenValidity(accessTokenValidateSeconds);
    uacUserToken.setLoginIp(remoteAddr);
    uacUserToken.setLoginLocation(remoteLocation);
    uacUserToken.setLoginTime(uacUser.getLastLoginTime());
    uacUserToken.setLoginName(loginAuthDto.getLoginName());
    uacUserToken.setRefreshToken(refreshToken);
    uacUserToken.setRefreshTokenValidity(refreshTokenValiditySeconds);
    uacUserToken.setStatus(UacUserTokenStatusEnum.ON_LINE.getStatus());
    uacUserToken.setUserId(userId);
    uacUserToken.setUserName(loginAuthDto.getUserName());
    uacUserToken.setUpdateInfo(loginAuthDto);
    uacUserToken.setGroupId(loginAuthDto.getGroupId());
    uacUserToken.setGroupName(loginAuthDto.getGroupName());
    uacUserToken.setId(generateId());
    uacUserTokenMapper.insertSelective(uacUserToken);
    UserTokenDto userTokenDto = new ModelMapper().map(uacUserToken, UserTokenDto.class);
    // 存入redis数据库
    updateRedisUserToken(accessToken, accessTokenValidateSeconds, userTokenDto);
}
Also used : UacUser(com.paascloud.provider.model.domain.UacUser) UserAgent(eu.bitwalker.useragentutils.UserAgent) UacUserToken(com.paascloud.provider.model.domain.UacUserToken) UserTokenDto(com.paascloud.base.dto.UserTokenDto) OAuth2ClientProperties(com.paascloud.security.core.properties.OAuth2ClientProperties) ModelMapper(org.modelmapper.ModelMapper)

Example 3 with UserTokenDto

use of com.paascloud.base.dto.UserTokenDto in project paascloud-master by paascloud.

the class UacUserTokenServiceImpl method refreshToken.

@Override
public String refreshToken(String accessToken, String refreshToken, HttpServletRequest request) throws HttpProcessException {
    String token;
    Map<String, Object> map = new HashMap<>(2);
    map.put("grant_type", "refresh_token");
    map.put("refresh_token", refreshToken);
    // 插件式配置请求参数(网址、请求参数、编码、client)
    Header[] headers = HttpHeader.custom().contentType(HttpHeader.Headers.APP_FORM_URLENCODED).authorization(request.getHeader(HttpHeaders.AUTHORIZATION)).build();
    HttpConfig config = HttpConfig.custom().headers(headers).url(refreshTokenUrl).map(map);
    token = HttpClientUtil.post(config);
    JSONObject jsonObj = JSON.parseObject(token);
    String accessTokenNew = (String) jsonObj.get("access_token");
    String refreshTokenNew = (String) jsonObj.get("refresh_token");
    String loginName = (String) jsonObj.get("loginName");
    // 更新本次token数据
    UserTokenDto tokenDto = this.getByAccessToken(accessToken);
    tokenDto.setStatus(UacUserTokenStatusEnum.ON_REFRESH.getStatus());
    UacUser uacUser = uacUserService.findUserInfoByLoginName(loginName);
    LoginAuthDto loginAuthDto = new LoginAuthDto(uacUser.getId(), uacUser.getLoginName(), uacUser.getUserName(), uacUser.getGroupId(), uacUser.getGroupName());
    this.updateUacUserToken(tokenDto, loginAuthDto);
    // 创建刷新token
    this.saveUserToken(accessTokenNew, refreshTokenNew, loginAuthDto, request);
    return token;
}
Also used : UacUser(com.paascloud.provider.model.domain.UacUser) Header(org.apache.http.Header) HttpHeader(com.arronlong.httpclientutil.common.HttpHeader) JSONObject(com.alibaba.fastjson.JSONObject) HashMap(java.util.HashMap) JSONObject(com.alibaba.fastjson.JSONObject) HttpConfig(com.arronlong.httpclientutil.common.HttpConfig) LoginAuthDto(com.paascloud.base.dto.LoginAuthDto) UserTokenDto(com.paascloud.base.dto.UserTokenDto)

Example 4 with UserTokenDto

use of com.paascloud.base.dto.UserTokenDto in project paascloud-master by paascloud.

the class UacUserLoginController method loginAfter.

/**
 * 登出.
 *
 * @param accessToken the access token
 *
 * @return the wrapper
 */
@PostMapping(value = "/user/logout")
@ApiOperation(httpMethod = "POST", value = "登出")
public Wrapper loginAfter(String accessToken) {
    if (!StringUtils.isEmpty(accessToken)) {
        // 修改用户在线状态
        UserTokenDto userTokenDto = uacUserTokenService.getByAccessToken(accessToken);
        userTokenDto.setStatus(UacUserTokenStatusEnum.OFF_LINE.getStatus());
        uacUserTokenService.updateUacUserToken(userTokenDto, getLoginAuthDto());
    }
    return WrapMapper.ok();
}
Also used : UserTokenDto(com.paascloud.base.dto.UserTokenDto) ApiOperation(io.swagger.annotations.ApiOperation)

Example 5 with UserTokenDto

use of com.paascloud.base.dto.UserTokenDto in project paascloud-master by paascloud.

the class TokenInterceptor method preHandle.

/**
 * Pre handle boolean.
 *
 * @param request  the request
 * @param response the response
 * @param handler  the handler
 *
 * @return the boolean
 */
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
    String uri = request.getRequestURI();
    log.info("<== preHandle - 权限拦截器.  url={}", uri);
    if (uri.contains(AUTH_PATH1) || uri.contains(AUTH_PATH2) || uri.contains(AUTH_PATH3) || uri.contains(AUTH_PATH4)) {
        log.info("<== preHandle - 配置URL不走认证.  url={}", uri);
        return true;
    }
    log.info("<== preHandle - 调试模式不走认证.  OPTIONS={}", request.getMethod().toUpperCase());
    if (OPTIONS.equalsIgnoreCase(request.getMethod())) {
        log.info("<== preHandle - 调试模式不走认证.  url={}", uri);
        return true;
    }
    if (isHaveAccess(handler)) {
        log.info("<== preHandle - 不需要认证注解不走认证.  token={}");
        return true;
    }
    String token = StringUtils.substringAfter(request.getHeader(HttpHeaders.AUTHORIZATION), "Bearer ");
    log.info("<== preHandle - 权限拦截器.  token={}", token);
    LoginAuthDto loginUser = (UserTokenDto) redisTemplate.opsForValue().get(RedisKeyUtil.getAccessTokenKey(token));
    if (loginUser == null) {
        log.error("获取用户信息失败, 不允许操作");
        return false;
    }
    log.info("<== preHandle - 权限拦截器.  loginUser={}", loginUser);
    ThreadLocalMap.put(GlobalConstant.Sys.TOKEN_AUTH_DTO, loginUser);
    log.info("<== preHandle - 权限拦截器.  url={}, loginUser={}", uri, loginUser);
    return true;
}
Also used : LoginAuthDto(com.paascloud.base.dto.LoginAuthDto) UserTokenDto(com.paascloud.base.dto.UserTokenDto)

Aggregations

UserTokenDto (com.paascloud.base.dto.UserTokenDto)5 LoginAuthDto (com.paascloud.base.dto.LoginAuthDto)2 UacUser (com.paascloud.provider.model.domain.UacUser)2 UacUserToken (com.paascloud.provider.model.domain.UacUserToken)2 ModelMapper (org.modelmapper.ModelMapper)2 JSONObject (com.alibaba.fastjson.JSONObject)1 HttpConfig (com.arronlong.httpclientutil.common.HttpConfig)1 HttpHeader (com.arronlong.httpclientutil.common.HttpHeader)1 OAuth2ClientProperties (com.paascloud.security.core.properties.OAuth2ClientProperties)1 UserAgent (eu.bitwalker.useragentutils.UserAgent)1 ApiOperation (io.swagger.annotations.ApiOperation)1 HashMap (java.util.HashMap)1 Header (org.apache.http.Header)1