From c3c67ee9e88c579e8ac784821ab41f58d0372ebb Mon Sep 17 00:00:00 2001
From: jiangping <jp@doumee.com>
Date: 星期二, 11 六月 2024 15:46:34 +0800
Subject: [PATCH] 最新版本
---
server/system_gateway/src/main/java/com/doumee/config/jwt/JwtTokenUtil.java | 25 ++++++++++++++++++++-----
1 files changed, 20 insertions(+), 5 deletions(-)
diff --git a/server/system_gateway/src/main/java/com/doumee/config/jwt/JwtTokenUtil.java b/server/system_gateway/src/main/java/com/doumee/config/jwt/JwtTokenUtil.java
index 460b974..9198e09 100644
--- a/server/system_gateway/src/main/java/com/doumee/config/jwt/JwtTokenUtil.java
+++ b/server/system_gateway/src/main/java/com/doumee/config/jwt/JwtTokenUtil.java
@@ -2,6 +2,7 @@
import com.alibaba.fastjson.JSONObject;
import com.doumee.core.model.LoginUserInfo;
+import com.doumee.core.utils.Constants;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.beans.factory.annotation.Autowired;
@@ -33,7 +34,7 @@
if(payloads == null){
return null;
}
- payloads.setLoginDate(new Date());
+ payloads.setLoginDate(new Date());
Map<String,Object> map = new HashMap<>();
map.put("id",payloads.getId());
// Map<String,Object> map = BeanUtil.beanToMap(payloads);
@@ -75,7 +76,8 @@
public Boolean isTokenExpired(String token) {
try {
LoginUserInfo claims = getClaimsFromToken(token);
- Date expiration = claims.getLoginDate();
+ Date expiration = //claims.getLoginDate();
+ new Date(claims.getLoginDate().getTime() + jwtProperties.getExpiration());
return expiration.before(new Date());
} catch (Exception e) {
//楠岃瘉JWT绛惧悕澶辫触绛夊悓浜庝护鐗岃繃鏈�
@@ -96,12 +98,25 @@
claims.setLoginDate(new Date());
refreshedToken = generateTokenDo(claims);
if(refreshedToken!=null){
- redisTemplate.delete(token);//鍒犻櫎鑰佺殑token
+ redisTemplate.delete(Constants.REDIS_TOKEN_KEY+token);//鍒犻櫎鑰佺殑token
}
} catch (Exception e) {
refreshedToken = null;
}
return refreshedToken;
+ }
+ /**
+ * 閫�鍑虹櫥闄�
+ *
+ * @param token 鍘熶护鐗�
+ * @return 鏂颁护鐗�
+ */
+ public void logout(String token) {
+ try {
+ redisTemplate.delete(Constants.REDIS_TOKEN_KEY+token);//鍒犻櫎鑰佺殑token
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
}
/**
@@ -131,7 +146,7 @@
.setExpiration(expirationDate)
.signWith(SignatureAlgorithm.HS512, jwtProperties.getSecret())
.compact();
- redisTemplate.opsForValue().set(token,JSONObject.toJSONString(userInfo),jwtProperties.getExpiration(), TimeUnit.MILLISECONDS);
+ redisTemplate.opsForValue().set(Constants.REDIS_TOKEN_KEY+token,JSONObject.toJSONString(userInfo),jwtProperties.getExpiration(), TimeUnit.MILLISECONDS);
return token;
}
@@ -144,7 +159,7 @@
private LoginUserInfo getClaimsFromToken(String token) {
LoginUserInfo claims;
try {
- String userInfo = (String) redisTemplate.opsForValue().get(token);
+ String userInfo = (String) redisTemplate.opsForValue().get(Constants.REDIS_TOKEN_KEY+token);
claims = JSONObject.toJavaObject(JSONObject.parseObject(userInfo),LoginUserInfo.class);
} catch (Exception e) {
claims = null;
--
Gitblit v1.9.3