forked from github/dataease
parent
0e6cb26ba9
commit
a34e99d7c1
@ -66,7 +66,7 @@ public class JWTFilter extends BasicHttpAuthenticationFilter {
|
||||
if (StringUtils.startsWith(authorization, "Basic")) {
|
||||
return false;
|
||||
}
|
||||
if (!TokenCacheUtils.validate(authorization)) {
|
||||
if (!TokenCacheUtils.validate(authorization) && !TokenCacheUtils.validateDelay(authorization)) {
|
||||
throw new AuthenticationException(expireMessage);
|
||||
}
|
||||
// 当没有出现登录超时 且需要刷新token 则执行刷新token
|
||||
@ -75,6 +75,8 @@ public class JWTFilter extends BasicHttpAuthenticationFilter {
|
||||
throw new AuthenticationException(expireMessage);
|
||||
}
|
||||
if (JWTUtils.needRefresh(authorization)) {
|
||||
TokenCacheUtils.addWithTtl(authorization, 1L);
|
||||
TokenCacheUtils.remove(authorization);
|
||||
authorization = refreshToken(request, response);
|
||||
}
|
||||
JWTToken token = new JWTToken(authorization);
|
||||
|
@ -20,6 +20,9 @@ import java.util.Date;
|
||||
|
||||
public class JWTUtils {
|
||||
|
||||
// token过期时间1min (过期会自动刷新续命 目的是避免一直都是同一个token )
|
||||
private static final long EXPIRE_TIME = 1 * 60 * 1000;
|
||||
// 登录间隔时间10min 超过这个时间强制重新登录
|
||||
private static long Login_Interval;
|
||||
|
||||
/**
|
||||
@ -64,7 +67,9 @@ public class JWTUtils {
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
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 secret 用户的密码
|
||||
@ -100,11 +114,12 @@ public class JWTUtils {
|
||||
*/
|
||||
public static String sign(TokenInfo tokenInfo, String secret) {
|
||||
try {
|
||||
Date date = new Date(System.currentTimeMillis() + EXPIRE_TIME);
|
||||
Algorithm algorithm = Algorithm.HMAC256(secret);
|
||||
Builder builder = JWT.create()
|
||||
.withClaim("username", tokenInfo.getUsername())
|
||||
.withClaim("userId", tokenInfo.getUserId());
|
||||
String sign = builder.sign(algorithm);
|
||||
String sign = builder.withExpiresAt(date).sign(algorithm);
|
||||
TokenCacheUtils.add(sign, tokenInfo.getUserId());
|
||||
return sign;
|
||||
|
||||
|
@ -7,6 +7,7 @@ import org.apache.commons.lang3.StringUtils;
|
||||
public class TokenCacheUtils {
|
||||
|
||||
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) {
|
||||
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());
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
@ -277,9 +277,19 @@
|
||||
maxElementsOnDisk="3000"
|
||||
overflowToDisk="true"
|
||||
diskPersistent="false"
|
||||
/>
|
||||
|
||||
<cache
|
||||
name="sys_token_store_delay"
|
||||
eternal="false"
|
||||
maxElementsInMemory="100"
|
||||
maxElementsOnDisk="3000"
|
||||
overflowToDisk="true"
|
||||
diskPersistent="false"
|
||||
timeToIdleSeconds="3"
|
||||
timeToLiveSeconds="5"
|
||||
memoryStoreEvictionPolicy="LRU"
|
||||
/>
|
||||
|
||||
|
||||
|
||||
</ehcache>
|
Loading…
Reference in New Issue
Block a user