فهرست منبع

feat: 修复token解析时,token格式不正确导致的错误

xdd 1 ماه پیش
والد
کامیت
4228f81818
1فایلهای تغییر یافته به همراه9 افزوده شده و 1 حذف شده
  1. 9 1
      fs-user-app/src/main/java/com/fs/app/utils/JwtUtils.java

+ 9 - 1
fs-user-app/src/main/java/com/fs/app/utils/JwtUtils.java

@@ -1,6 +1,8 @@
 package com.fs.app.utils;
 
+import com.fs.app.exception.FSException;
 import com.fs.common.core.redis.RedisCache;
+import com.fs.common.utils.StringUtils;
 import io.jsonwebtoken.Claims;
 import io.jsonwebtoken.Jwts;
 import io.jsonwebtoken.SignatureAlgorithm;
@@ -8,6 +10,7 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.context.properties.ConfigurationProperties;
+import org.springframework.http.HttpStatus;
 import org.springframework.stereotype.Component;
 
 import java.util.Date;
@@ -44,6 +47,9 @@ public class JwtUtils {
     }
 
     public Claims getClaimByToken(String token) {
+        if (StringUtils.isEmpty(token)) {
+            throw new FSException("token不能为空", HttpStatus.UNAUTHORIZED.value());
+        }
         try {
             return Jwts.parser()
                     .setSigningKey(secret)
@@ -51,7 +57,9 @@ public class JwtUtils {
                     .getBody();
         }catch (Exception e){
             logger.debug("validate is token error ", e);
-            return null;
+            logger.info("token异常,重新登录,token: {}",token);
+
+            throw new FSException("token 失效,请重新登录", HttpStatus.UNAUTHORIZED.value());
         }
     }