|
|
@@ -58,30 +58,27 @@ public class TokenService {
|
|
|
* 创建令牌
|
|
|
*/
|
|
|
public Map<String, Object> createToken(LoginUser loginUser) {
|
|
|
- Long userId = loginUser.getSysUser().getId();
|
|
|
|
|
|
+ Long userId = loginUser.getSysUser().getId();
|
|
|
String userName = loginUser.getSysUser().getName();
|
|
|
- if(StringUtils.isEmpty(loginUser.getToken())){
|
|
|
- String token = userId + "_"+ loginUser.getPlatformType()+"_" + IdUtils.fastUUID();
|
|
|
- loginUser.setToken(token);
|
|
|
- }
|
|
|
-
|
|
|
+ String token = IdUtils.fastUUID();
|
|
|
+ loginUser.setToken(token);
|
|
|
loginUser.setUserid(userId);
|
|
|
loginUser.setUsername(userName);
|
|
|
loginUser.setOrgId(loginUser.getSysUser().getOrgId());
|
|
|
loginUser.setName(loginUser.getSysUser().getName());
|
|
|
loginUser.setIpaddr(IpUtils.getIpAddr());
|
|
|
- long configExpireTime = getTokenExpireTime(loginUser.getPlatformType());
|
|
|
- refreshTokenNew(loginUser,configExpireTime);
|
|
|
+ long configExpireTime = getTokenExpireTime(loginUser.getPlatformType());
|
|
|
+ refreshTokenNew(loginUser, configExpireTime);
|
|
|
|
|
|
// Jwt存储信息
|
|
|
Map<String, Object> claimsMap = new HashMap<String, Object>();
|
|
|
claimsMap.put(SecurityConstants.USER_KEY, loginUser.getToken());
|
|
|
+ claimsMap.put(SecurityConstants.PLATFORM_TYPE, loginUser.getPlatformType());
|
|
|
claimsMap.put(SecurityConstants.DETAILS_USER_ID, userId);
|
|
|
claimsMap.put(SecurityConstants.DETAILS_USERNAME, userName);
|
|
|
claimsMap.put(SecurityConstants.DETAILS_MASTER_USER_ID, loginUser.getMasterUserId());
|
|
|
claimsMap.put(SecurityConstants.DETAILS_MASTERUSERNAME, loginUser.getMasterUserName());
|
|
|
-
|
|
|
// 接口返回信息
|
|
|
Map<String, Object> rspMap = new HashMap<String, Object>();
|
|
|
rspMap.put("access_token", JwtUtils.createToken(claimsMap));
|
|
|
@@ -91,7 +88,6 @@ public class TokenService {
|
|
|
|
|
|
/**
|
|
|
* 创建令牌
|
|
|
- *
|
|
|
*/
|
|
|
public Map<String, Object> createTokenNew(LoginUser loginUser, Integer loginFrom) {
|
|
|
//现场大屏登录也是 走APP登录接口,但是请求携带了 loginFrom 参数;根据 loginFrom 转换 PlatformType 值
|
|
|
@@ -157,13 +153,14 @@ public class TokenService {
|
|
|
try {
|
|
|
if (StringUtils.isNotEmpty(token)) {
|
|
|
String userkey = JwtUtils.getUserKey(token);
|
|
|
- user = RedisUtils.getCacheObject(getTokenKey(userkey));
|
|
|
- //user = redisService.getCacheObject(getTokenKey(userkey));
|
|
|
- return user;
|
|
|
+ String userId = JwtUtils.getUserId(token);
|
|
|
+ String platformType = JwtUtils.getPlatformType(token);
|
|
|
+ user = RedisUtils.getCacheObject(getTokenKey(userkey, userId, platformType));
|
|
|
}
|
|
|
+ return user;
|
|
|
} catch (Exception e) {
|
|
|
+ return user;
|
|
|
}
|
|
|
- return user;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -181,8 +178,10 @@ public class TokenService {
|
|
|
public void delLoginUser(String token) {
|
|
|
if (StringUtils.isNotEmpty(token)) {
|
|
|
String userkey = JwtUtils.getUserKey(token);
|
|
|
+ String userId = JwtUtils.getUserId(token);
|
|
|
+ String platformType = JwtUtils.getPlatformType(token);
|
|
|
//redisService.deleteObject(getTokenKey(userkey));
|
|
|
- RedisUtils.deleteObject(getTokenKey(userkey));
|
|
|
+ RedisUtils.deleteObject(getTokenKey(userkey, userId, platformType));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -195,7 +194,7 @@ public class TokenService {
|
|
|
long expireTime = loginUser.getExpireTime();
|
|
|
long currentTime = System.currentTimeMillis();
|
|
|
if (expireTime - currentTime <= MILLIS_MINUTE_TEN) {
|
|
|
- logger.info("Token 即将过期,刷新Token 有效时间,{}",loginUser.getToken());
|
|
|
+ logger.info("Token 即将过期,刷新Token 有效时间,{}", loginUser.getToken());
|
|
|
refreshToken(loginUser);
|
|
|
}
|
|
|
}
|
|
|
@@ -220,7 +219,7 @@ public class TokenService {
|
|
|
// RedisUtils.setCacheObject(userKey, loginUser, Duration.ofMinutes(expireTime));
|
|
|
// //redisService.deleteByPrefix(userKey);
|
|
|
// // redisService.setCacheObject(userKey, loginUser, expireTime, TimeUnit.MINUTES);
|
|
|
- refreshTokenNew(loginUser,expireTime);
|
|
|
+ refreshTokenNew(loginUser, expireTime);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -228,66 +227,66 @@ public class TokenService {
|
|
|
*
|
|
|
* @param loginUser 登录信息
|
|
|
*/
|
|
|
- public void refreshTokenNew(LoginUser loginUser,long configExpireTime) {
|
|
|
+ public void refreshTokenNew(LoginUser loginUser, long configExpireTime) {
|
|
|
loginUser.setLoginTime(System.currentTimeMillis());
|
|
|
loginUser.setExpireTime(loginUser.getLoginTime() + configExpireTime * MILLIS_MINUTE);
|
|
|
// 根据uuid将loginUser缓存
|
|
|
- String userKey = getTokenKey(loginUser.getToken());
|
|
|
+ String userKey = getTokenKey(loginUser.getToken(), String.valueOf(loginUser.getUserid()), loginUser.getPlatformType());
|
|
|
// remoteUserService.userLoginInfo(loginUser.getUserid(), DateUtils.getNowDate(), loginUser.getIpaddr(), SecurityConstants.INNER);
|
|
|
//登陆后删除之前的token
|
|
|
// admin 用户的 ID 是1 按前缀删除缓存时 会把其他人的Token都删除掉, 固排除 admin
|
|
|
- if(!"admin".equals(loginUser.getSysUser().getUsername())){
|
|
|
- String userKeyPrefix= getTokenKey(loginUser.getUserid().toString()+"_"+loginUser.getPlatformType());
|
|
|
+ /* if (!"admin".equals(loginUser.getSysUser().getUsername())) {
|
|
|
+ String userKeyPrefix = getTokenKey(loginUser.getUserid().toString() + "_" + loginUser.getPlatformType());
|
|
|
RedisUtils.deleteByPrefix(userKeyPrefix);
|
|
|
- }
|
|
|
+ }*/
|
|
|
|
|
|
RedisUtils.setCacheObject(userKey, loginUser, Duration.ofMinutes(configExpireTime));
|
|
|
//redisService.deleteByPrefix(userKey);
|
|
|
// redisService.setCacheObject(userKey, loginUser, expireTime, TimeUnit.MINUTES);
|
|
|
}
|
|
|
|
|
|
- private String getTokenKey(String token) {
|
|
|
- return ACCESS_TOKEN + token;
|
|
|
+ /**
|
|
|
+ * 登录token记录数据
|
|
|
+ *
|
|
|
+ * @param token 令牌
|
|
|
+ * @param userId 用户id
|
|
|
+ * @param patformType 平台类型
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private String getTokenKey(String token, String userId, String patformType) {
|
|
|
+ return ACCESS_TOKEN + userId + ":" + patformType + ":" + token;
|
|
|
}
|
|
|
|
|
|
- private Long getTokenExpireTime(String platformType)
|
|
|
- {
|
|
|
+ private Long getTokenExpireTime(String platformType) {
|
|
|
long configExpireTime = 0;
|
|
|
String configExpireTimeStr = "";
|
|
|
- if(platformType != null){
|
|
|
- if(Constants.LOGIN_TYPE_2.equals(platformType)){
|
|
|
+ if (platformType != null) {
|
|
|
+ if (Constants.LOGIN_TYPE_2.equals(platformType)) {
|
|
|
//大屏
|
|
|
- String screenCacheKey=CacheConstants.SYS_CONFIG_KEY+SystemParameterConstant.TOKEN_LARGESCREEN_EXPIRETIME;
|
|
|
- configExpireTimeStr= getConfigValueFromRedis(screenCacheKey);
|
|
|
- }
|
|
|
- else if(Constants.LOGIN_TYPE_0.equals(platformType)){
|
|
|
+ String screenCacheKey = CacheConstants.SYS_CONFIG_KEY + SystemParameterConstant.TOKEN_LARGESCREEN_EXPIRETIME;
|
|
|
+ configExpireTimeStr = getConfigValueFromRedis(screenCacheKey);
|
|
|
+ } else if (Constants.LOGIN_TYPE_0.equals(platformType)) {
|
|
|
//app
|
|
|
- String appCacheKey=CacheConstants.SYS_CONFIG_KEY+SystemParameterConstant.TOKEN_APP_EXPIRETIME;
|
|
|
- configExpireTimeStr= getConfigValueFromRedis(appCacheKey);
|
|
|
- }else if(Constants.LOGIN_TYPE_1.equals(platformType)){
|
|
|
+ String appCacheKey = CacheConstants.SYS_CONFIG_KEY + SystemParameterConstant.TOKEN_APP_EXPIRETIME;
|
|
|
+ configExpireTimeStr = getConfigValueFromRedis(appCacheKey);
|
|
|
+ } else if (Constants.LOGIN_TYPE_1.equals(platformType)) {
|
|
|
//web
|
|
|
- String webCacheKey=CacheConstants.SYS_CONFIG_KEY+SystemParameterConstant.TOKEN_WEB_EXPIRETIME;
|
|
|
- configExpireTimeStr= getConfigValueFromRedis(webCacheKey);
|
|
|
+ String webCacheKey = CacheConstants.SYS_CONFIG_KEY + SystemParameterConstant.TOKEN_WEB_EXPIRETIME;
|
|
|
+ configExpireTimeStr = getConfigValueFromRedis(webCacheKey);
|
|
|
}
|
|
|
}
|
|
|
- if(StringUtils.isNotEmpty(configExpireTimeStr))
|
|
|
- {
|
|
|
- configExpireTime= Long.parseLong(configExpireTimeStr);
|
|
|
- }
|
|
|
- else{
|
|
|
- configExpireTime=expireTime;
|
|
|
+ if (StringUtils.isNotEmpty(configExpireTimeStr)) {
|
|
|
+ configExpireTime = Long.parseLong(configExpireTimeStr);
|
|
|
+ } else {
|
|
|
+ configExpireTime = expireTime;
|
|
|
}
|
|
|
return configExpireTime;
|
|
|
}
|
|
|
|
|
|
- private String getConfigValueFromRedis(String cacheKey)
|
|
|
- {
|
|
|
- if(RedisUtils.hasKey(cacheKey))
|
|
|
- {
|
|
|
+ private String getConfigValueFromRedis(String cacheKey) {
|
|
|
+ if (RedisUtils.hasKey(cacheKey)) {
|
|
|
return RedisUtils.getCacheObject(cacheKey);
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
+ } else {
|
|
|
return null;
|
|
|
}
|
|
|
}
|
|
|
@@ -297,17 +296,15 @@ public class TokenService {
|
|
|
// loginFrom 0 大屏,1 app ,2 web
|
|
|
// 大屏登录时会带入 logFrom 参数
|
|
|
//移动端端登录
|
|
|
- if (logFrom==null || logFrom==1) {
|
|
|
+ if (logFrom == null || logFrom == 1) {
|
|
|
// app
|
|
|
return Constants.LOGIN_TYPE_0;
|
|
|
}
|
|
|
- if(logFrom==0)
|
|
|
- {
|
|
|
+ if (logFrom == 0) {
|
|
|
// 大屏
|
|
|
return Constants.LOGIN_TYPE_2;
|
|
|
}
|
|
|
- if(logFrom==2)
|
|
|
- {
|
|
|
+ if (logFrom == 2) {
|
|
|
// web
|
|
|
return Constants.LOGIN_TYPE_1;
|
|
|
}
|