|
|
@@ -1,7 +1,9 @@
|
|
|
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.SecurityConstants;
|
|
|
+import com.xunmei.common.core.constant.SystemParameterConstant;
|
|
|
import com.xunmei.common.core.utils.DateUtils;
|
|
|
import com.xunmei.common.core.utils.JwtUtils;
|
|
|
import com.xunmei.common.core.utils.ServletUtils;
|
|
|
@@ -10,11 +12,14 @@ import com.xunmei.common.core.utils.ip.IpUtils;
|
|
|
import com.xunmei.common.core.utils.uuid.IdUtils;
|
|
|
import com.xunmei.common.redis.utils.RedisUtils;
|
|
|
import com.xunmei.common.security.utils.SecurityUtils;
|
|
|
+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.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
+import javax.annotation.Resource;
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import java.time.Duration;
|
|
|
import java.util.Date;
|
|
|
@@ -43,6 +48,9 @@ public class TokenService {
|
|
|
|
|
|
private final static Long MILLIS_MINUTE_TEN = CacheConstants.REFRESH_TIME * MILLIS_MINUTE;
|
|
|
|
|
|
+ @Resource
|
|
|
+ RemoteConfigService remoteConfigService;
|
|
|
+
|
|
|
/**
|
|
|
* 创建令牌
|
|
|
*/
|
|
|
@@ -78,6 +86,65 @@ public class TokenService {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
+ * 创建令牌
|
|
|
+ */
|
|
|
+ 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 = 1500;
|
|
|
+ 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;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
* 获取用户身份信息
|
|
|
*
|
|
|
* @return 用户信息
|
|
|
@@ -167,6 +234,24 @@ public class TokenService {
|
|
|
// redisService.setCacheObject(userKey, loginUser, expireTime, TimeUnit.MINUTES);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 刷新令牌有效期
|
|
|
+ *
|
|
|
+ * @param loginUser 登录信息
|
|
|
+ */
|
|
|
+ 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());
|
|
|
+ remoteUserService.userLoginInfo(loginUser.getUserid(), DateUtils.getNowDate(), loginUser.getIpaddr(), SecurityConstants.INNER);
|
|
|
+ //登陆后删除之前的token
|
|
|
+ RedisUtils.deleteByPrefix(userKey);
|
|
|
+ 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;
|
|
|
}
|