|
@@ -6,7 +6,6 @@ import cn.hutool.http.useragent.UserAgent;
|
|
|
import cn.hutool.http.useragent.UserAgentUtil;
|
|
|
import com.zhongzheng.common.constant.Constants;
|
|
|
import com.zhongzheng.common.core.domain.model.LoginUser;
|
|
|
-import com.zhongzheng.common.core.domain.model.TopLoginUser;
|
|
|
import com.zhongzheng.common.core.redis.RedisCache;
|
|
|
import com.zhongzheng.common.exception.CustomException;
|
|
|
import com.zhongzheng.common.exception.user.UserPasswordNotMatchException;
|
|
@@ -92,33 +91,6 @@ public class TokenService
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 获取用户身份信息
|
|
|
- *
|
|
|
- * @return 用户信息
|
|
|
- */
|
|
|
- public TopLoginUser getTopLoginUser(HttpServletRequest request) {
|
|
|
- //测试用户
|
|
|
- String test_token = request.getHeader("X-Auth-Token");
|
|
|
- if("test".equals(test_token)){
|
|
|
- return getTestTopUser();
|
|
|
- }
|
|
|
- // 获取请求携带的令牌
|
|
|
- String token = getToken(request);
|
|
|
- if (Validator.isNotEmpty(token))
|
|
|
- {
|
|
|
- String uuid = "";
|
|
|
- Claims claims = parseToken(token);
|
|
|
- // 解析对应的权限以及用户信息
|
|
|
- uuid = (String) claims.get(Constants.LOGIN_USER_KEY);
|
|
|
- String userKey = getTokenKey(uuid);
|
|
|
- TopLoginUser user = redisCache.getCacheObject(userKey);
|
|
|
- return user;
|
|
|
- }
|
|
|
-
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
private LoginUser getTestUser(){
|
|
|
Authentication authentication = null;
|
|
|
try
|
|
@@ -145,32 +117,6 @@ public class TokenService
|
|
|
return loginUser;
|
|
|
}
|
|
|
|
|
|
- private TopLoginUser getTestTopUser(){
|
|
|
- Authentication authentication = null;
|
|
|
- try
|
|
|
- {
|
|
|
- // 该方法会去调用UserDetailsServiceImpl.loadUserByUsername
|
|
|
- authentication = authenticationManager
|
|
|
- .authenticate(new UsernamePasswordAuthenticationToken("admin", "admin123"));
|
|
|
- }
|
|
|
- catch (Exception e)
|
|
|
- {
|
|
|
- if (e instanceof BadCredentialsException)
|
|
|
- {
|
|
|
- AsyncManager.me().execute(AsyncFactory.recordLogininfor("admin", Constants.LOGIN_FAIL, MessageUtils.message("user.password.not.match")));
|
|
|
- throw new UserPasswordNotMatchException();
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- AsyncManager.me().execute(AsyncFactory.recordLogininfor("admin", Constants.LOGIN_FAIL, e.getMessage()));
|
|
|
- throw new CustomException(e.getMessage());
|
|
|
- }
|
|
|
- }
|
|
|
- TopLoginUser loginUser = (TopLoginUser) authentication.getPrincipal();
|
|
|
- loginUser.setExpireTime(System.currentTimeMillis()+200);
|
|
|
- return loginUser;
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* 设置用户身份信息
|
|
|
*/
|
|
@@ -182,17 +128,6 @@ public class TokenService
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 设置用户身份信息
|
|
|
- */
|
|
|
- public void setTopLoginUser(TopLoginUser loginUser)
|
|
|
- {
|
|
|
- if (Validator.isNotNull(loginUser) && Validator.isNotEmpty(loginUser.getToken()))
|
|
|
- {
|
|
|
- refreshTopToken(loginUser);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* 删除用户身份信息
|
|
|
*/
|
|
@@ -223,23 +158,6 @@ public class TokenService
|
|
|
return createToken(claims);
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 创建令牌
|
|
|
- *
|
|
|
- * @param loginUser 用户信息
|
|
|
- * @return 令牌
|
|
|
- */
|
|
|
- public String createTopToken(TopLoginUser loginUser)
|
|
|
- {
|
|
|
- String token = IdUtil.fastUUID();
|
|
|
- loginUser.setToken(token);
|
|
|
- setTopUserAgent(loginUser);
|
|
|
- refreshTopToken(loginUser);
|
|
|
-
|
|
|
- Map<String, Object> claims = new HashMap<>();
|
|
|
- claims.put(Constants.LOGIN_USER_KEY, token);
|
|
|
- return createToken(claims);
|
|
|
- }
|
|
|
|
|
|
/**
|
|
|
* 验证令牌有效期,相差不足20分钟,自动刷新缓存
|
|
@@ -271,19 +189,6 @@ public class TokenService
|
|
|
redisCache.setCacheObject(userKey, loginUser, expireTime, TimeUnit.MINUTES);
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 刷新令牌有效期
|
|
|
- *
|
|
|
- * @param loginUser 登录信息
|
|
|
- */
|
|
|
- public void refreshTopToken(TopLoginUser loginUser)
|
|
|
- {
|
|
|
- loginUser.setLoginTime(System.currentTimeMillis());
|
|
|
- loginUser.setExpireTime(loginUser.getLoginTime() + expireTime * MILLIS_MINUTE);
|
|
|
- // 根据uuid将loginUser缓存
|
|
|
- String userKey = getTokenKey(loginUser.getToken());
|
|
|
- redisCache.setCacheObject(userKey, loginUser, expireTime, TimeUnit.MINUTES);
|
|
|
- }
|
|
|
|
|
|
/**
|
|
|
* 设置用户代理信息
|
|
@@ -300,20 +205,6 @@ public class TokenService
|
|
|
loginUser.setOs(userAgent.getOs().getName());
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 设置用户代理信息
|
|
|
- *
|
|
|
- * @param loginUser 登录信息
|
|
|
- */
|
|
|
- public void setTopUserAgent(TopLoginUser loginUser)
|
|
|
- {
|
|
|
- UserAgent userAgent = UserAgentUtil.parse(ServletUtils.getRequest().getHeader("User-Agent"));
|
|
|
- String ip = IpUtils.getIpAddr(ServletUtils.getRequest());
|
|
|
- loginUser.setIpaddr(ip);
|
|
|
- loginUser.setLoginLocation(AddressUtils.getRealAddressByIP(ip));
|
|
|
- loginUser.setBrowser(userAgent.getBrowser().getName());
|
|
|
- loginUser.setOs(userAgent.getOs().getName());
|
|
|
- }
|
|
|
|
|
|
/**
|
|
|
* 从数据声明生成令牌
|