Merge pull request #4510 from dataease/pr@dev@fix_token_cache
fix(登录): 刷新token导致退出登录
This commit is contained in:
commit
34c4785d60
@ -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,7 @@ 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);
|
TokenCacheUtils.remove(authorization);
|
||||||
authorization = refreshToken(request, response);
|
authorization = refreshToken(request, response);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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);
|
||||||
@ -25,4 +26,13 @@ public class TokenCacheUtils {
|
|||||||
Object sys_token_store = CacheUtils.get(KEY, token);
|
Object sys_token_store = CacheUtils.get(KEY, token);
|
||||||
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());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -279,5 +279,17 @@
|
|||||||
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"
|
||||||
|
/>
|
||||||
|
|
||||||
|
|
||||||
</ehcache>
|
</ehcache>
|
||||||
@ -1,7 +1,7 @@
|
|||||||
import axios from 'axios'
|
import axios from 'axios'
|
||||||
import store from '@/store'
|
import store from '@/store'
|
||||||
import { $alert, $error } from './message'
|
import { $alert, $error } from './message'
|
||||||
import { getToken, getIdToken } from '@/utils/auth'
|
import { getToken, getIdToken, setToken } from '@/utils/auth'
|
||||||
import Config from '@/settings'
|
import Config from '@/settings'
|
||||||
import i18n from '@/lang'
|
import i18n from '@/lang'
|
||||||
import { tryShowLoading, tryHideLoading } from './loading'
|
import { tryShowLoading, tryHideLoading } from './loading'
|
||||||
@ -157,6 +157,7 @@ const checkAuth = response => {
|
|||||||
// token到期后自动续命 刷新token
|
// token到期后自动续命 刷新token
|
||||||
if (response.headers[RefreshTokenKey]) {
|
if (response.headers[RefreshTokenKey]) {
|
||||||
const refreshToken = response.headers[RefreshTokenKey]
|
const refreshToken = response.headers[RefreshTokenKey]
|
||||||
|
setToken(refreshToken)
|
||||||
store.dispatch('user/refreshToken', refreshToken)
|
store.dispatch('user/refreshToken', refreshToken)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user