From 28aaf2ffa1dbb860a292ba330a7e9362e60e7832 Mon Sep 17 00:00:00 2001
From: kongzy <kongzy>
Date: Fri, 12 Jul 2024 16:41:03 +0800
Subject: [PATCH] update
---
assess-common/src/main/java/com/gkhy/assess/common/utils/JwtTokenUtil.java | 104 ++++++++++++++++++++++++++++++++++++++++++++++++---
1 files changed, 97 insertions(+), 7 deletions(-)
diff --git a/assess-common/src/main/java/com/gkhy/assess/common/utils/JwtTokenUtil.java b/assess-common/src/main/java/com/gkhy/assess/common/utils/JwtTokenUtil.java
index fec4f2d..302045a 100644
--- a/assess-common/src/main/java/com/gkhy/assess/common/utils/JwtTokenUtil.java
+++ b/assess-common/src/main/java/com/gkhy/assess/common/utils/JwtTokenUtil.java
@@ -1,18 +1,30 @@
package com.gkhy.assess.common.utils;
+import cn.hutool.core.date.DateUtil;
import cn.hutool.crypto.digest.DigestUtil;
import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.JWTDecodeException;
+import com.auth0.jwt.interfaces.Claim;
import com.auth0.jwt.interfaces.DecodedJWT;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.gkhy.assess.common.api.CommonResult;
import com.gkhy.assess.common.exception.ApiException;
+import io.swagger.models.auth.In;
import org.apache.commons.lang3.StringUtils;
+import org.apache.shiro.crypto.hash.Md5Hash;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import java.io.OutputStream;
import java.util.Date;
+import java.util.HashMap;
+import java.util.Map;
/**
* JwtToken生成的工具类
@@ -36,8 +48,13 @@
public static String tokenHead="";
- /**Token有效期为7天(Token在reids中缓存时间为两倍)*/
- public static final long EXPIRATION=(7 * 12) * 60 * 60 * 1000; //JWT的超期限时间(60*60*24*7)
+ /**Token有效期为1天(Token在reids中缓存时间为两倍) 单位ms*/
+ public static final long EXPIRATION=(1 *12) * 60 * 60 * 1000; //JWT的超期限时间(60*60*24*1)
+
+ /**
+ * token有效期还有30分钟,刷新token 单位ms
+ */
+ public static final long NEED_UPDATE_TIME= 30 * 60 * 1000;
/**
* 校验token是否正确
@@ -46,15 +63,31 @@
* @param secret 用户密码
* @return
*/
- public static boolean verify(String token,String username,String secret){
+ public static boolean verify(String token,String username,String secret,Integer identity){
try {
Algorithm algorithm = Algorithm.HMAC256(secret);
- JWTVerifier verifier = JWT.require(algorithm).withClaim("username", username).build();
+ JWTVerifier verifier = JWT.require(algorithm).withClaim("username", username)
+ .withClaim("identity",identity).build();
DecodedJWT jwt = verifier.verify(token);
return true;
}catch (Exception e){
return false;
}
+ }
+
+
+ public static boolean isNeedUpdate(String token, String username, String secret,Integer identity){
+ Date expertsAt =null;
+ try {
+ Algorithm algorithm = Algorithm.HMAC256(secret);
+ JWTVerifier verifier = JWT.require(algorithm).withClaim("username", username)
+ .withClaim("identity",identity).build();
+ expertsAt = verifier.verify(token).getExpiresAt();
+ }catch (Exception e){
+ throw new ApiException("token非法无效");
+ }
+ //如果剩余过期时间少于过期时常的一半时 需要更新
+ return (expertsAt.getTime()-System.currentTimeMillis()) < NEED_UPDATE_TIME;
}
/**
@@ -72,15 +105,32 @@
}
/**
+ * 获取token中的信息 无需secret解密也能获得
+ * @param token
+ * @return
+ */
+ public static Integer getIdentity(String token){
+ try {
+ DecodedJWT jwt = JWT.decode(token);
+ return jwt.getClaim("identity").asInt();
+ }catch (JWTDecodeException e){
+ return null;
+ }
+ }
+
+
+
+ /**
* 生成签名
* @param username
* @param secret
* @return
*/
- public static String sign(String username,String secret){
- Date date=new Date(System.currentTimeMillis()+EXPIRATION*1000);
+ public static String sign(String username,String secret,Integer identity){
+ Date date=new Date(System.currentTimeMillis()+EXPIRATION);
Algorithm algorithm=Algorithm.HMAC256(secret);
- return JWT.create().withClaim("username",username).withExpiresAt(date).sign(algorithm);
+ return JWT.create().withClaim("username",username)
+ .withClaim("identity",identity).withExpiresAt(date).sign(algorithm);
}
/**
@@ -111,5 +161,45 @@
}
+ /**
+ * 密码加密
+ * @param username
+ * @param password
+ * @param salt
+ * @return
+ */
+ public static String encryptPassword(String username,String password,String salt){
+ if(salt==null){
+ salt="";
+ }
+ return new Md5Hash(username+password+salt).toHex();
+ }
+
+
+ /**
+ *
+ * @param response
+ * @param code
+ * @param errorMsg
+ */
+ public static void responseError(ServletResponse response, String errorMsg) {
+ HttpServletResponse httpServletResponse = (HttpServletResponse) response;
+ // issues/I4YH95浏览器显示乱码问题
+ httpServletResponse.setHeader("Content-type", "text/html;charset=UTF-8");
+ CommonResult jsonResult = CommonResult.failed(errorMsg);
+ OutputStream os = null;
+ try {
+ os = httpServletResponse.getOutputStream();
+ httpServletResponse.setCharacterEncoding("UTF-8");
+ // httpServletResponse.setStatus(code);
+ os.write(new ObjectMapper().writeValueAsString(jsonResult).getBytes("UTF-8"));
+ os.flush();
+ os.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+
+
}
--
Gitblit v1.9.2