Răsfoiți Sursa

增加token时间校验

wukai 1 an în urmă
părinte
comite
c01aa49813

+ 6 - 5
doc-admin/src/main/resources/application-test.yml

@@ -138,9 +138,9 @@ spring:
   servlet:
     multipart:
       # 单个文件大小
-      max-file-size: 10GB
+      max-file-size: 10MB
       # 设置总上传的文件大小
-      max-request-size: 20GB
+      max-request-size: 100MB
   # 服务模块
   devtools:
     restart:
@@ -149,8 +149,8 @@ spring:
 # 日志配置
 logging:
   level:
-    com.doc: debug
-    org.springframework: warn
+    com.doc: error
+    org.springframework: error
 # token配置
 token:
   # 令牌自定义标识
@@ -161,7 +161,8 @@ token:
   expireTime: 30
   # 是否允许账户多终端同时登录(true允许 false不允许)
   soloLogin: false
-
+  # 请求有效时间(秒)
+  requestTime: 5
 # MyBatis配置
 mybatis-plus:
   # 搜索指定包别名

+ 2 - 0
doc-admin/src/main/resources/application.yml

@@ -60,6 +60,8 @@ token:
   expireTime: 30
   # 是否允许账户多终端同时登录(true允许 false不允许)
   soloLogin: false
+  # 请求有效时间(秒)
+  requestTime: 5
 
 # MyBatis配置
 mybatis-plus:

+ 32 - 1
doc-common/src/main/java/com/doc/common/utils/encrypt/Sm2Util.java

@@ -29,6 +29,14 @@ import java.util.Base64;
  * @author van
  */
 public class Sm2Util {
+    /**
+     * 私有key
+     */
+    private static final String PRIVATE_KEY = "ebaedbccd3f730ec1985b945f10429b468c7f3f70e66da19b8657f023e6cded4";
+    /**
+     * 公有key
+     */
+    private static final String PUBLIC_KEY = "04404c634de4deda80486b4a331adf03448a449d980fe040540abe242ba275cc815aada6a63c63e24d4672e14360c72b1819914f49708f25498ededc2217384960";
 
     /**
      * 生成 SM2 公私钥对
@@ -128,6 +136,16 @@ public class Sm2Util {
         return encrypt(data.getBytes(StandardCharsets.UTF_8), publicKey);
     }
 
+    /**
+     * SM2加密算法
+     *
+     * @param data 明文数据
+     * @return
+     */
+    public static String encrypt(String data) {
+        return encrypt(data.getBytes(StandardCharsets.UTF_8), PUBLIC_KEY);
+    }
+
     public static String encrypt(byte[] data, PublicKey publicKey) {
         BCECPublicKey key = (BCECPublicKey) publicKey;
         return encrypt(data, Hex.toHexString(key.getQ().getEncoded(false)));
@@ -172,6 +190,16 @@ public class Sm2Util {
      * SM2解密算法
      *
      * @param cipherData hex格式密文
+     * @return 明文
+     */
+    public static String decrypt(String cipherData) {
+        return decrypt(Hex.decode(cipherData), PRIVATE_KEY);
+    }
+
+    /**
+     * SM2解密算法
+     *
+     * @param cipherData hex格式密文
      * @param privateKey 密钥PrivateKey型
      * @return 明文
      */
@@ -330,7 +358,7 @@ public class Sm2Util {
     public static void main(String[] args) throws Exception {
         System.out.println("======  sm2utils test  ======");
 
-        String M = "data_message";
+        String M = "123456";
         System.out.println("明文:\t" + M);
 
         System.out.println("begin 开始生成密钥对>>>");
@@ -354,6 +382,9 @@ public class Sm2Util {
         String text = decrypt(cipherData, priKeyHexString);
         System.out.println("解密:\t" + text);
 
+        text = decrypt("04294f7750ff7ed05e620923b23a9fe9477146983d981f9de135909f66265ec440b110b7ec46568148a011358050671e52af6a69585fb94d44f47d2176827151a7ae1ac1a7f5263dcbabb3eb45b5898901703a533ead697c49e222ca8717c97e6f3269727924fe", priKeyHexString);
+        System.out.println("解密:\t" + text);
+
         String sign = sign(M, priKeyHexString);
         System.out.println("signvalue\t" + sign);
         sign = "3045022100d69b69421985160ac2a116a006525b0a414090d8d4b815dd98f97c74c1de68bd02201bfbddc24b3ee159faf13948f1ba1ef3368a5d019ea4137d2f69d4d5aaf2a05a";

+ 21 - 0
doc-framework/src/main/java/com/doc/framework/web/service/TokenService.java

@@ -4,8 +4,10 @@ import com.doc.common.constant.CacheConstants;
 import com.doc.common.constant.Constants;
 import com.doc.common.core.domain.model.LoginUser;
 import com.doc.common.core.redis.RedisCache;
+import com.doc.common.utils.DateUtils;
 import com.doc.common.utils.ServletUtils;
 import com.doc.common.utils.StringUtils;
+import com.doc.common.utils.encrypt.Sm2Util;
 import com.doc.common.utils.ip.AddressUtils;
 import com.doc.common.utils.ip.IpUtils;
 import com.doc.common.utils.uuid.IdUtils;
@@ -51,6 +53,11 @@ public class TokenService {
      */
     @Value("${token.soloLogin}")
     private boolean soloLogin;
+    /**
+     * 请求有效时间(秒)
+     */
+    @Value("${token.requestTime}")
+    private int requestTime;
 
     protected static final long MILLIS_SECOND = 1000;
 
@@ -71,6 +78,20 @@ public class TokenService {
         String token = getToken(request);
         if (StringUtils.isNotEmpty(token)) {
             try {
+                token = Sm2Util.decrypt(token);
+                String[] tmp = token.split("///");
+                token = tmp[0];
+                long time = Long.parseLong(tmp[1]);
+                long nowTime = DateUtils.getNowDate().getTime();
+                if (nowTime - requestTime * 1000L > time) {
+                    return null;
+                }
+            } catch (Exception ignored) {
+                //容错机制
+                //如果报错,则证明没有经过SM2加密
+                //暂时不用管的
+            }
+            try {
                 Claims claims = parseToken(token);
                 // 解析对应的权限以及用户信息
                 String uuid = (String) claims.get(Constants.LOGIN_USER_KEY);