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;
}
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);
}
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;
}
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();
}
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;
}
Aggregations