|
|
@@ -2,6 +2,7 @@ package com.xunmei.common.security.service;
|
|
|
|
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
|
import com.xunmei.common.core.constant.CacheConstants;
|
|
|
+import com.xunmei.common.core.constant.Constants;
|
|
|
import com.xunmei.common.core.constant.SecurityConstants;
|
|
|
import com.xunmei.common.core.constant.SystemParameterConstant;
|
|
|
import com.xunmei.common.core.utils.DateUtils;
|
|
|
@@ -16,6 +17,8 @@ import com.xunmei.system.api.RemoteConfigService;
|
|
|
import com.xunmei.system.api.RemoteUserService;
|
|
|
import com.xunmei.system.api.domain.SysConfig;
|
|
|
import com.xunmei.system.api.model.LoginUser;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
@@ -34,7 +37,7 @@ import java.util.concurrent.TimeUnit;
|
|
|
*/
|
|
|
@Component
|
|
|
public class TokenService {
|
|
|
-
|
|
|
+ protected final Logger logger = LoggerFactory.getLogger(this.getClass());
|
|
|
@Autowired
|
|
|
private RemoteUserService remoteUserService;
|
|
|
|
|
|
@@ -59,7 +62,7 @@ public class TokenService {
|
|
|
|
|
|
String userName = loginUser.getSysUser().getName();
|
|
|
if(StringUtils.isEmpty(loginUser.getToken())){
|
|
|
- String token = userId + "_" + IdUtils.fastUUID();
|
|
|
+ String token = userId + "_"+ loginUser.getPlatformType()+"_" + IdUtils.fastUUID();
|
|
|
loginUser.setToken(token);
|
|
|
}
|
|
|
|
|
|
@@ -68,7 +71,8 @@ public class TokenService {
|
|
|
loginUser.setOrgId(loginUser.getSysUser().getOrgId());
|
|
|
loginUser.setName(loginUser.getSysUser().getName());
|
|
|
loginUser.setIpaddr(IpUtils.getIpAddr());
|
|
|
- refreshToken(loginUser);
|
|
|
+ long configExpireTime = getTokenExpireTime(loginUser.getPlatformType());
|
|
|
+ refreshTokenNew(loginUser,configExpireTime);
|
|
|
|
|
|
// Jwt存储信息
|
|
|
Map<String, Object> claimsMap = new HashMap<String, Object>();
|
|
|
@@ -81,68 +85,46 @@ public class TokenService {
|
|
|
// 接口返回信息
|
|
|
Map<String, Object> rspMap = new HashMap<String, Object>();
|
|
|
rspMap.put("access_token", JwtUtils.createToken(claimsMap));
|
|
|
- rspMap.put("expires_in", expireTime);
|
|
|
+ rspMap.put("expires_in", configExpireTime);
|
|
|
return rspMap;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 创建令牌
|
|
|
+ *
|
|
|
*/
|
|
|
public Map<String, Object> createTokenNew(LoginUser loginUser, Integer loginFrom) {
|
|
|
- Long userId = loginUser.getSysUser().getId();
|
|
|
-
|
|
|
- String userName = loginUser.getSysUser().getName();
|
|
|
- if(StringUtils.isEmpty(loginUser.getToken())){
|
|
|
- String token = userId + "_" + 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 = expireTime;
|
|
|
- if(loginFrom != null){
|
|
|
- if(loginFrom == 0){
|
|
|
- //大屏
|
|
|
- final SysConfig config = remoteConfigService.findSysConfigByCode(SystemParameterConstant.TOKEN_LARGESCREEN_EXPIRETIME, SecurityConstants.INNER);
|
|
|
- if (ObjectUtil.isNotEmpty(config)){
|
|
|
- configExpireTime = Long.parseLong(config.getConfigValue());
|
|
|
- }
|
|
|
- }
|
|
|
- else if(loginFrom == 1){
|
|
|
- //app
|
|
|
- final SysConfig config = remoteConfigService.findSysConfigByCode(SystemParameterConstant.TOKEN_APP_EXPIRETIME, SecurityConstants.INNER);
|
|
|
- if (ObjectUtil.isNotEmpty(config)){
|
|
|
- configExpireTime = Long.parseLong(config.getConfigValue());
|
|
|
- }
|
|
|
- }else if(loginFrom == 2){
|
|
|
- //app
|
|
|
- final SysConfig config = remoteConfigService.findSysConfigByCode(SystemParameterConstant.TOKEN_WEB_EXPIRETIME, SecurityConstants.INNER);
|
|
|
- if (ObjectUtil.isNotEmpty(config)){
|
|
|
- configExpireTime = Long.parseLong(config.getConfigValue());
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- refreshTokenNew(loginUser,configExpireTime);
|
|
|
-
|
|
|
- // Jwt存储信息
|
|
|
- Map<String, Object> claimsMap = new HashMap<String, Object>();
|
|
|
- claimsMap.put(SecurityConstants.USER_KEY, loginUser.getToken());
|
|
|
- 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));
|
|
|
- rspMap.put("expires_in", configExpireTime);
|
|
|
- return rspMap;
|
|
|
+ //现场大屏登录也是 走APP登录接口,但是请求携带了 loginFrom 参数;根据 loginFrom 转换 PlatformType 值
|
|
|
+ loginUser.setPlatformType(getPlatformTypeByLoginFrom(loginFrom));
|
|
|
+ return createToken(loginUser);
|
|
|
+// Long userId = loginUser.getSysUser().getId();
|
|
|
+// String userName = loginUser.getSysUser().getName();
|
|
|
+// if(StringUtils.isEmpty(loginUser.getToken())){
|
|
|
+// String token = userId + "_" + 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);
|
|
|
+//
|
|
|
+// // Jwt存储信息
|
|
|
+// Map<String, Object> claimsMap = new HashMap<String, Object>();
|
|
|
+// claimsMap.put(SecurityConstants.USER_KEY, loginUser.getToken());
|
|
|
+// 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));
|
|
|
+// rspMap.put("expires_in", configExpireTime);
|
|
|
+// return rspMap;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -213,6 +195,7 @@ public class TokenService {
|
|
|
long expireTime = loginUser.getExpireTime();
|
|
|
long currentTime = System.currentTimeMillis();
|
|
|
if (expireTime - currentTime <= MILLIS_MINUTE_TEN) {
|
|
|
+ logger.info("Token 即将过期,刷新Token 有效时间,{}",loginUser.getToken());
|
|
|
refreshToken(loginUser);
|
|
|
}
|
|
|
}
|
|
|
@@ -223,16 +206,21 @@ public class TokenService {
|
|
|
* @param loginUser 登录信息
|
|
|
*/
|
|
|
public void refreshToken(LoginUser loginUser) {
|
|
|
- loginUser.setLoginTime(System.currentTimeMillis());
|
|
|
- loginUser.setExpireTime(loginUser.getLoginTime() + expireTime * MILLIS_MINUTE);
|
|
|
- // 根据uuid将loginUser缓存
|
|
|
- String userKey = getTokenKey(loginUser.getToken());
|
|
|
- remoteUserService.userLoginInfo(loginUser.getUserid(), DateUtils.getNowDate(), loginUser.getIpaddr(), SecurityConstants.INNER);
|
|
|
- //登陆后删除之前的token
|
|
|
- RedisUtils.deleteByPrefix(userKey);
|
|
|
- RedisUtils.setCacheObject(userKey, loginUser, Duration.ofMinutes(expireTime));
|
|
|
- //redisService.deleteByPrefix(userKey);
|
|
|
- // redisService.setCacheObject(userKey, loginUser, expireTime, TimeUnit.MINUTES);
|
|
|
+// loginUser.setLoginTime(System.currentTimeMillis());
|
|
|
+// loginUser.setExpireTime(loginUser.getLoginTime() + expireTime * MILLIS_MINUTE);
|
|
|
+// // 根据uuid将loginUser缓存
|
|
|
+// String userKey = getTokenKey(loginUser.getToken());
|
|
|
+//// 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());
|
|
|
+// RedisUtils.deleteByPrefix(userKeyPrefix);
|
|
|
+// }
|
|
|
+// RedisUtils.setCacheObject(userKey, loginUser, Duration.ofMinutes(expireTime));
|
|
|
+// //redisService.deleteByPrefix(userKey);
|
|
|
+// // redisService.setCacheObject(userKey, loginUser, expireTime, TimeUnit.MINUTES);
|
|
|
+ refreshTokenNew(loginUser,expireTime);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -245,9 +233,14 @@ public class TokenService {
|
|
|
loginUser.setExpireTime(loginUser.getLoginTime() + configExpireTime * MILLIS_MINUTE);
|
|
|
// 根据uuid将loginUser缓存
|
|
|
String userKey = getTokenKey(loginUser.getToken());
|
|
|
- remoteUserService.userLoginInfo(loginUser.getUserid(), DateUtils.getNowDate(), loginUser.getIpaddr(), SecurityConstants.INNER);
|
|
|
+// remoteUserService.userLoginInfo(loginUser.getUserid(), DateUtils.getNowDate(), loginUser.getIpaddr(), SecurityConstants.INNER);
|
|
|
//登陆后删除之前的token
|
|
|
- RedisUtils.deleteByPrefix(userKey);
|
|
|
+ // admin 用户的 ID 是1 按前缀删除缓存时 会把其他人的Token都删除掉, 固排除 admin
|
|
|
+ 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);
|
|
|
@@ -256,4 +249,69 @@ public class TokenService {
|
|
|
private String getTokenKey(String token) {
|
|
|
return ACCESS_TOKEN + token;
|
|
|
}
|
|
|
+
|
|
|
+ private Long getTokenExpireTime(String platformType)
|
|
|
+ {
|
|
|
+ long configExpireTime = 0;
|
|
|
+ String configExpireTimeStr = "";
|
|
|
+ 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)){
|
|
|
+ //app
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if(StringUtils.isNotEmpty(configExpireTimeStr))
|
|
|
+ {
|
|
|
+ configExpireTime= Long.parseLong(configExpireTimeStr);
|
|
|
+ }
|
|
|
+ else{
|
|
|
+ configExpireTime=expireTime;
|
|
|
+ }
|
|
|
+ return configExpireTime;
|
|
|
+ }
|
|
|
+
|
|
|
+ private String getConfigValueFromRedis(String cacheKey)
|
|
|
+ {
|
|
|
+ if(RedisUtils.hasKey(cacheKey))
|
|
|
+ {
|
|
|
+ return RedisUtils.getCacheObject(cacheKey);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private String getPlatformTypeByLoginFrom(Integer logFrom) {
|
|
|
+ // loginFrom 0 大屏,1 app ,2 web
|
|
|
+ // 大屏登录时会带入 logFrom 参数
|
|
|
+ //移动端端登录
|
|
|
+ if (logFrom==null || logFrom==1) {
|
|
|
+ // app
|
|
|
+ return Constants.LOGIN_TYPE_0;
|
|
|
+ }
|
|
|
+ if(logFrom==0)
|
|
|
+ {
|
|
|
+ // 大屏
|
|
|
+ return Constants.LOGIN_TYPE_2;
|
|
|
+ }
|
|
|
+ if(logFrom==2)
|
|
|
+ {
|
|
|
+ // web
|
|
|
+ return Constants.LOGIN_TYPE_1;
|
|
|
+ }
|
|
|
+ // 默认为APP登录
|
|
|
+ return Constants.LOGIN_TYPE_0;
|
|
|
+ }
|
|
|
}
|