From d8a687741273d121586b83745280c57f7e9d3297 Mon Sep 17 00:00:00 2001
From: XiaoRuby <3114200645@qq.com>
Date: 星期一, 07 八月 2023 17:51:28 +0800
Subject: [PATCH] Merge branch 'master' of https://gitee.com/yuanchu_code/lims-management-system

---
 user-server/src/main/java/com/yuanchu/limslaboratory/shiro/utils/JwtUtils.java |  159 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 159 insertions(+), 0 deletions(-)

diff --git a/user-server/src/main/java/com/yuanchu/limslaboratory/shiro/utils/JwtUtils.java b/user-server/src/main/java/com/yuanchu/limslaboratory/shiro/utils/JwtUtils.java
new file mode 100644
index 0000000..68ae14f
--- /dev/null
+++ b/user-server/src/main/java/com/yuanchu/limslaboratory/shiro/utils/JwtUtils.java
@@ -0,0 +1,159 @@
+package com.yuanchu.limslaboratory.shiro.utils;
+
+
+import com.auth0.jwt.JWT;
+import com.auth0.jwt.JWTCreator;
+import com.auth0.jwt.JWTVerifier;
+import com.auth0.jwt.algorithms.Algorithm;
+import com.auth0.jwt.exceptions.JWTCreationException;
+import com.auth0.jwt.exceptions.JWTDecodeException;
+import com.auth0.jwt.exceptions.JWTVerificationException;
+import com.auth0.jwt.interfaces.Claim;
+import com.auth0.jwt.interfaces.DecodedJWT;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Component;
+import org.springframework.util.ObjectUtils;
+
+import javax.annotation.PostConstruct;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.Map;
+
+@Component
+public class JwtUtils {
+    private static String staticSecret;
+
+    @Value("${login.secret}")
+    private String secret;
+
+    @PostConstruct
+    public void getApiToken() {
+        staticSecret = this.secret;
+    }
+
+    public static String getSecret() {
+        // lockie.zou
+        return staticSecret;
+    }
+
+    // 杩囨湡鏃堕棿 2 灏忔椂
+    private static final long EXPIRE_TIME = 2 * 60 * 60 * 1000;
+    //鑷繁瀹氬埗瀵嗛挜
+    public static final String SECRET = "J-(t]Poe9P";
+
+    //璇锋眰澶�
+    public static final String AUTH_HEADER = "X-Token";  // X-Authorization-With
+
+    /**
+     * 楠岃瘉token鏄惁姝g‘
+     * @param token
+     * @return
+     */
+    public static boolean verify(String token){
+        try{
+            String account = getClaimFiled(token, "account");
+            if (account == null){
+                return false;
+            }
+            Algorithm algorithm = Algorithm.HMAC256(getSecret());
+            JWTVerifier verifier = JWT.require(algorithm).withClaim("account",account).build();
+            verifier.verify(token);
+            return true;
+        } catch (JWTVerificationException exception){
+            return false;
+        } catch (Exception e){
+            e.printStackTrace();
+            return false;
+        }
+    }
+
+    /**
+     * 鑾峰緱token涓殑鑷畾涔変俊鎭�,涓�鑸槸鑾峰彇token鐨剈sername锛屾棤闇�secret瑙e瘑涔熻兘鑾峰緱
+     * @param token
+     * @param filed
+     * @return
+     */
+    public static String getClaimFiled(String token, String filed){
+        try{
+            if (!ObjectUtils.isEmpty(token)){
+                DecodedJWT jwt = JWT.decode(token);
+                return jwt.getClaim(filed).asString();
+            }
+            return null;
+        } catch (JWTDecodeException e){
+            return null;
+        }
+    }
+
+    /**
+     * 鐢熸垚绛惧悕,鍑嗙‘鍦拌鏄敓鎴恡oken
+     * @return
+     */
+    public static String sign(String account){
+        try{
+            Date date = new Date(System.currentTimeMillis() + EXPIRE_TIME);
+            Algorithm algorithm = Algorithm.HMAC256(SECRET);
+            //闄勫甫username,nickname淇℃伅
+            return JWT.create()
+                    .withClaim("account",account)
+                    .withExpiresAt(date)
+                    .sign(algorithm);
+        } catch (JWTCreationException e){
+            e.printStackTrace();
+            return null;
+        } catch (Exception e){
+            e.printStackTrace();
+            return null;
+        }
+    }
+
+    /**
+     * 鑾峰彇token鐨勭鍙戞椂闂�
+     * @param token
+     * @return
+     */
+    public static Date getIssueAt(String token){
+        try{
+            DecodedJWT jwt = JWT.decode(token);
+            return jwt.getIssuedAt();
+        } catch (JWTDecodeException e){
+            e.printStackTrace();
+            return null;
+        }
+    }
+
+    /**
+     * 楠岃瘉token鏄惁杩囨湡
+     * @param token
+     * @return
+     */
+    public static boolean isTokenExpired(String token){
+        Date now = Calendar.getInstance().getTime();
+        DecodedJWT jwt = JWT.decode(token);
+        return jwt.getExpiresAt().before(now);
+    }
+
+    /**
+     * 鍒锋柊token鐨勬湁鏁堟湡
+     * @param token
+     * @param secret
+     * @return
+     */
+    public static String refreshTokenExpired(String token, String secret){
+        DecodedJWT jwt = JWT.decode(token); //瑙f瀽token
+        Map<String, Claim> claims = jwt.getClaims(); //鑾峰彇token鐨勫弬鏁颁俊鎭�
+
+        try{
+            Date date = new Date(System.currentTimeMillis() + EXPIRE_TIME);
+            Algorithm algorithm = Algorithm.HMAC256(secret);
+            JWTCreator.Builder builder = JWT.create().withExpiresAt(date);
+            for(Map.Entry<String,Claim> entry : claims.entrySet()){
+                builder.withClaim(entry.getKey(),entry.getValue().asString());
+            }
+            return builder.sign(algorithm);
+        } catch (JWTCreationException e){
+            e.printStackTrace();
+            return null;
+        }
+    }
+}

--
Gitblit v1.9.3