Revert "perf(token): 禁用token刷新"

This reverts commit 0e6cb26ba9.
This commit is contained in:
fit2cloud-chenyw 2023-02-17 22:05:00 +08:00
parent 0e6cb26ba9
commit a34e99d7c1
4 changed files with 41 additions and 5 deletions

View File

@ -66,7 +66,7 @@ public class JWTFilter extends BasicHttpAuthenticationFilter {
if (StringUtils.startsWith(authorization, "Basic")) { if (StringUtils.startsWith(authorization, "Basic")) {
return false; return false;
} }
if (!TokenCacheUtils.validate(authorization)) { if (!TokenCacheUtils.validate(authorization) && !TokenCacheUtils.validateDelay(authorization)) {
throw new AuthenticationException(expireMessage); throw new AuthenticationException(expireMessage);
} }
// 当没有出现登录超时 且需要刷新token 则执行刷新token // 当没有出现登录超时 且需要刷新token 则执行刷新token
@ -75,6 +75,8 @@ public class JWTFilter extends BasicHttpAuthenticationFilter {
throw new AuthenticationException(expireMessage); throw new AuthenticationException(expireMessage);
} }
if (JWTUtils.needRefresh(authorization)) { if (JWTUtils.needRefresh(authorization)) {
TokenCacheUtils.addWithTtl(authorization, 1L);
TokenCacheUtils.remove(authorization);
authorization = refreshToken(request, response); authorization = refreshToken(request, response);
} }
JWTToken token = new JWTToken(authorization); JWTToken token = new JWTToken(authorization);

View File

@ -20,6 +20,9 @@ import java.util.Date;
public class JWTUtils { public class JWTUtils {
// token过期时间1min (过期会自动刷新续命 目的是避免一直都是同一个token )
private static final long EXPIRE_TIME = 1 * 60 * 1000;
// 登录间隔时间10min 超过这个时间强制重新登录
private static long Login_Interval; private static long Login_Interval;
/** /**
@ -64,7 +67,9 @@ public class JWTUtils {
} }
public static boolean needRefresh(String token) { public static boolean needRefresh(String token) {
return false; Date exp = JWTUtils.getExp(token);
Long advanceTime = 5000L;
return (new Date().getTime() + advanceTime) >= exp.getTime();
} }
/** /**
@ -90,9 +95,18 @@ public class JWTUtils {
return isExpire; return isExpire;
} }
public static Date getExp(String token) {
try {
DecodedJWT jwt = JWT.decode(token);
return jwt.getClaim("exp").asDate();
} catch (JWTDecodeException e) {
e.printStackTrace();
return null;
}
}
/** /**
* 生成签名,5min后过期
* *
* @param tokenInfo 用户信息 * @param tokenInfo 用户信息
* @param secret 用户的密码 * @param secret 用户的密码
@ -100,11 +114,12 @@ public class JWTUtils {
*/ */
public static String sign(TokenInfo tokenInfo, String secret) { public static String sign(TokenInfo tokenInfo, String secret) {
try { try {
Date date = new Date(System.currentTimeMillis() + EXPIRE_TIME);
Algorithm algorithm = Algorithm.HMAC256(secret); Algorithm algorithm = Algorithm.HMAC256(secret);
Builder builder = JWT.create() Builder builder = JWT.create()
.withClaim("username", tokenInfo.getUsername()) .withClaim("username", tokenInfo.getUsername())
.withClaim("userId", tokenInfo.getUserId()); .withClaim("userId", tokenInfo.getUserId());
String sign = builder.sign(algorithm); String sign = builder.withExpiresAt(date).sign(algorithm);
TokenCacheUtils.add(sign, tokenInfo.getUserId()); TokenCacheUtils.add(sign, tokenInfo.getUserId());
return sign; return sign;

View File

@ -7,6 +7,7 @@ import org.apache.commons.lang3.StringUtils;
public class TokenCacheUtils { public class TokenCacheUtils {
private static final String KEY = "sys_token_store"; private static final String KEY = "sys_token_store";
private static final String DELAY_KEY = "sys_token_store_delay";
public static void add(String token, Long userId) { public static void add(String token, Long userId) {
CacheUtils.put(KEY, token, userId, null, null); CacheUtils.put(KEY, token, userId, null, null);
@ -26,4 +27,12 @@ public class TokenCacheUtils {
return ObjectUtils.isNotEmpty(sys_token_store) && StringUtils.isNotBlank(sys_token_store.toString()) && userId == Long.parseLong(sys_token_store.toString()); return ObjectUtils.isNotEmpty(sys_token_store) && StringUtils.isNotBlank(sys_token_store.toString()) && userId == Long.parseLong(sys_token_store.toString());
} }
public static void addWithTtl(String token, Long userId) {
CacheUtils.put(DELAY_KEY, token, userId, 3, 5);
}
public static boolean validateDelay(String token) {
Object tokenObj = CacheUtils.get(DELAY_KEY, token);
return ObjectUtils.isNotEmpty(tokenObj) && StringUtils.isNotBlank(tokenObj.toString());
}
} }

View File

@ -277,9 +277,19 @@
maxElementsOnDisk="3000" maxElementsOnDisk="3000"
overflowToDisk="true" overflowToDisk="true"
diskPersistent="false" diskPersistent="false"
/>
<cache
name="sys_token_store_delay"
eternal="false"
maxElementsInMemory="100"
maxElementsOnDisk="3000"
overflowToDisk="true"
diskPersistent="false"
timeToIdleSeconds="3"
timeToLiveSeconds="5"
memoryStoreEvictionPolicy="LRU" memoryStoreEvictionPolicy="LRU"
/> />
</ehcache> </ehcache>