瀏覽代碼

增加三个config参数sql;app登录接口根据登录终端去数据库选择过期时间,之前的接口默认1500

liyi 11 月之前
父節點
當前提交
e146d48117

+ 12 - 0
project_data/sql/0.1.1/soc/soc.sql

@@ -1419,4 +1419,16 @@ update  sys_device  set device_brand = '1849277638232182790' WHERE device_brand
 -- 处理设备品牌为空字符串的
 update  sys_device  set device_brand = null WHERE device_brand ='';
 
+delete from sys_config where config_key = 'TOKEN_LARGESCREEN_EXPIRETIME';
+INSERT INTO `sys_config`(config_name,config_key,config_value,config_type,create_by,create_time,update_by,update_time,remark)
+VALUES ('终端token过期时间(单位:分钟)', 'TOKEN_LARGESCREEN_EXPIRETIME', '1500', 'Y', null, null, '', null, null);
+
+delete from sys_config where config_key = 'TOKEN_WEB_EXPIRETIME';
+INSERT INTO `sys_config`(config_name,config_key,config_value,config_type,create_by,create_time,update_by,update_time,remark)
+VALUES ('web token过期时间(单位:分钟)', 'TOKEN_WEB_EXPIRETIME', '1500', 'Y', null, null, '', null, null);
+
+delete from sys_config where config_key = 'TOKEN_APP_EXPIRETIME';
+INSERT INTO `sys_config`(config_name,config_key,config_value,config_type,create_by,create_time,update_by,update_time,remark)
+VALUES ('app token过期时间(单位:分钟)', 'TOKEN_APP_EXPIRETIME', '1500', 'Y', null, null, '', null, null);
+
 -- 升级脚本执行完成

+ 10 - 2
soc-auth/src/main/java/com/xunmei/auth/controller/TokenController.java

@@ -67,9 +67,17 @@ public class TokenController {
                 //登录重放问题处理,待前端完成放开
                 //loginService.checkLogin(form.getAuthCode());
             }
-            LoginUser userInfo = loginService.loginByPassword(form.getUsername(), form.getPassword(), Constants.LOGIN_TYPE_0);
+
+            String platformType = Constants.LOGIN_TYPE_0;
+            if(form.getDeviceFrom() != null){
+                if(form.getDeviceFrom() == 0){
+                    platformType = Constants.LOGIN_TYPE_2;
+                }
+            }
+
+            LoginUser userInfo = loginService.loginByPassword(form.getUsername(), form.getPassword(), platformType);
             // 获取登录token
-            return R.ok(tokenService.createToken(userInfo));
+            return R.ok(tokenService.createTokenNew(userInfo,form.getDeviceFrom()));
         } catch (Exception e) {
             return R.fail(e.getMessage());
         }

+ 3 - 0
soc-auth/src/main/java/com/xunmei/auth/form/LoginBody.java

@@ -20,4 +20,7 @@ public class LoginBody {
     @ApiModelProperty(value = "随机码", required = true)
     private String authCode;
 
+    @ApiModelProperty(value = "登录终端 0大屏 1app 2web", required = false)
+    private Integer deviceFrom;
+
 }

+ 5 - 0
soc-common/soc-common-core/src/main/java/com/xunmei/common/core/constant/Constants.java

@@ -151,6 +151,11 @@ public class Constants {
     public static final String LOGIN_TYPE_1 = "1";
 
     /**
+     * 大屏登录
+     */
+    public static final String LOGIN_TYPE_2 = "2";
+
+    /**
      * 数字常量
      */
     public static final Integer ONE = 1;

+ 15 - 0
soc-common/soc-common-core/src/main/java/com/xunmei/common/core/constant/SystemParameterConstant.java

@@ -189,4 +189,19 @@ public class SystemParameterConstant {
      */
     public static final String ALARM_LOSE_DURATION = "ALARM_LOSE_DURATION";
 
+    /**
+     *  终端token过期时间(单位:分钟)
+     */
+    public static final String TOKEN_LARGESCREEN_EXPIRETIME = "TOKEN_LARGESCREEN_EXPIRETIME";
+
+    /**
+     *  WEB token过期时间(单位:分钟)
+     */
+    public static final String TOKEN_WEB_EXPIRETIME = "TOKEN_WEB_EXPIRETIME";
+
+    /**
+     *  APP token过期时间(单位:分钟)
+     */
+    public static final String TOKEN_APP_EXPIRETIME = "TOKEN_APP_EXPIRETIME";
+
 }

+ 85 - 0
soc-common/soc-common-security/src/main/java/com/xunmei/common/security/service/TokenService.java

@@ -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;
     }