Browse Source

优化旧语法

tsurumure 10 months ago
parent
commit
528df807cb

+ 4 - 11
pom.xml

@@ -101,17 +101,7 @@
         <!-- JWT (备份 0.11.5)-->
         <dependency>
             <groupId>io.jsonwebtoken</groupId>
-            <artifactId>jjwt-api</artifactId>
-            <version>0.12.5</version>
-        </dependency>
-        <dependency>
-            <groupId>io.jsonwebtoken</groupId>
-            <artifactId>jjwt-impl</artifactId>
-            <version>0.12.5</version>
-        </dependency>
-        <dependency>
-            <groupId>io.jsonwebtoken</groupId>
-            <artifactId>jjwt-jackson</artifactId>
+            <artifactId>jjwt</artifactId>
             <version>0.12.5</version>
         </dependency>
 
@@ -457,6 +447,9 @@
                     <source>${java.version}</source>
                     <target>${java.version}</target>
                     <encoding>${project.build.sourceEncoding}</encoding>
+                    <compilerArgs>
+                        <arg>-Xlint:deprecation</arg> <!-- 启用过时 API 警告 -->
+                    </compilerArgs>
                 </configuration>
             </plugin>
 

+ 8 - 1
src/main/java/com/backendsys/config/Security/utils/JwtUtil.java

@@ -9,6 +9,7 @@ import org.springframework.beans.factory.annotation.Value;
 import org.springframework.security.core.userdetails.UserDetails;
 import org.springframework.stereotype.Service;
 
+import javax.crypto.SecretKey;
 import java.security.Key;
 import java.util.Date;
 import java.util.Map;
@@ -39,6 +40,11 @@ public class JwtUtil {
      */
     // private static final long TOKEN_EXPIRED = 86400000;
 
+    private SecretKey getSigningKey() {
+        byte[] keyBytes = Decoders.BASE64.decode(SECRET_KEY);
+        return Keys.hmacShaKeyFor(keyBytes);
+    }
+
     /**
      * 创建Token (sysUser 系统用户 实体类)
      */
@@ -62,7 +68,8 @@ public class JwtUtil {
                 .claim("is_super", sysUser.get("is_super"))
                 .claim("modules", sysUser.get("modules"))
                 .claim("target", "System")
-                .signWith(SignatureAlgorithm.HS256, SECRET_KEY)
+//                .signWith(SignatureAlgorithm.HS256, SECRET_KEY)
+                .signWith(getSigningKey())
                 .setExpiration(expiration)
                 .compact();
     }

+ 12 - 6
src/main/java/com/backendsys/utils/MapUtil.java

@@ -89,7 +89,8 @@ public class MapUtil {
                             jsonArray.put(item);
                         }
                     }
-                    json.put(fieldName, jsonArray);
+                    json.set(fieldName, jsonArray);
+//                    json.put(fieldName, jsonArray);
                 } else if (Map.class.isAssignableFrom(field.getType())) {
                     // 字段是Map类型
                     Map<?, ?> map = (Map<?, ?>) fieldValue;
@@ -99,13 +100,16 @@ public class MapUtil {
                         Object value = entry.getValue();
                         if (value instanceof Object) {
                             // 如果Map的值是实体类对象,则递归转换
-                            jsonMap.put(key.toString(), convertEntityToJson(value));
+                            jsonMap.set(key.toString(), convertEntityToJson(value));
+//                            jsonMap.put(key.toString(), convertEntityToJson(value));
                         } else {
                             // 否则直接添加到Map中
-                            jsonMap.put(key.toString(), value);
+                            jsonMap.set(key.toString(), value);
+//                            jsonMap.put(key.toString(), value);
                         }
                     }
-                    json.put(fieldName, jsonMap);
+                    json.set(fieldName, jsonMap);
+//                    json.put(fieldName, jsonMap);
                 } else if (
                         String.class.isAssignableFrom(field.getType()) ||
                                 Integer.class.isAssignableFrom(field.getType()) ||
@@ -119,11 +123,13 @@ public class MapUtil {
                                 field.getType().isArray()
                 ) {
                     // 判断不了实体类,只能判断上面的通用类型,适合类型的直接输出
-                    json.put(fieldName, fieldValue);
+                    json.set(fieldName, fieldValue);
+//                    json.put(fieldName, fieldValue);
                 } else {
                     // 无法识别实体类类型 else if (field.getType().isAssignableFrom(Entity.class)
                     // 字段是实体类的实例,递归转换
-                    json.put(fieldName, convertEntityToJson(fieldValue));
+                    json.set(fieldName, convertEntityToJson(fieldValue));
+//                    json.put(fieldName, convertEntityToJson(fieldValue));
                 }
             }
         }