修复集合刷新问题
This commit is contained in:
+23
-11
@@ -170,8 +170,7 @@ public class CustomRedisTokenStore implements TokenStore {
|
|||||||
//获取过期时长
|
//获取过期时长
|
||||||
int validitySeconds = getAccessTokenValiditySeconds(clientAuth.getClientId());
|
int validitySeconds = getAccessTokenValiditySeconds(clientAuth.getClientId());
|
||||||
if (validitySeconds > 0) {
|
if (validitySeconds > 0) {
|
||||||
int expiresIn = token.getExpiresIn();
|
double expiresRatio = token.getExpiresIn() / (double)validitySeconds;
|
||||||
double expiresRatio = expiresIn / (double)validitySeconds;
|
|
||||||
//判断是否需要续签,当前剩余时间小于过期时长的50%则续签
|
//判断是否需要续签,当前剩余时间小于过期时长的50%则续签
|
||||||
if (expiresRatio <= RENEW_RATIO) {
|
if (expiresRatio <= RENEW_RATIO) {
|
||||||
//更新AccessToken过期时间
|
//更新AccessToken过期时间
|
||||||
@@ -216,10 +215,13 @@ public class CustomRedisTokenStore implements TokenStore {
|
|||||||
} finally {
|
} finally {
|
||||||
conn.close();
|
conn.close();
|
||||||
}
|
}
|
||||||
ClientDetails clientDetails = deserializeClientDetails(bytes);
|
if (bytes != null) {
|
||||||
if (clientDetails != null && clientDetails.getAccessTokenValiditySeconds() != null) {
|
ClientDetails clientDetails = deserializeClientDetails(bytes);
|
||||||
return clientDetails.getAccessTokenValiditySeconds();
|
if (clientDetails.getAccessTokenValiditySeconds() != null) {
|
||||||
|
return clientDetails.getAccessTokenValiditySeconds();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//返回默认值
|
//返回默认值
|
||||||
return SecurityConstants.ACCESS_TOKEN_VALIDITY_SECONDS;
|
return SecurityConstants.ACCESS_TOKEN_VALIDITY_SECONDS;
|
||||||
}
|
}
|
||||||
@@ -258,9 +260,9 @@ public class CustomRedisTokenStore implements TokenStore {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 存储token
|
* 存储token
|
||||||
* @param isExpire 是否刷新过期时间
|
* @param isRenew 是否续签
|
||||||
*/
|
*/
|
||||||
private void storeAccessToken(OAuth2AccessToken token, OAuth2Authentication authentication, boolean isExpire) {
|
private void storeAccessToken(OAuth2AccessToken token, OAuth2Authentication authentication, boolean isRenew) {
|
||||||
byte[] serializedAccessToken = serialize(token);
|
byte[] serializedAccessToken = serialize(token);
|
||||||
byte[] serializedAuth = serialize(authentication);
|
byte[] serializedAuth = serialize(authentication);
|
||||||
byte[] accessKey = serializeKey(ACCESS + token.getValue());
|
byte[] accessKey = serializeKey(ACCESS + token.getValue());
|
||||||
@@ -271,6 +273,12 @@ public class CustomRedisTokenStore implements TokenStore {
|
|||||||
|
|
||||||
RedisConnection conn = getConnection();
|
RedisConnection conn = getConnection();
|
||||||
try {
|
try {
|
||||||
|
byte[] oldAccessToken = null;
|
||||||
|
//续签的话需要查询旧的token,用于集合删除
|
||||||
|
if (isRenew) {
|
||||||
|
oldAccessToken = conn.get(accessKey);
|
||||||
|
}
|
||||||
|
|
||||||
this.openPipeline(conn);
|
this.openPipeline(conn);
|
||||||
if (springDataRedis_2_0) {
|
if (springDataRedis_2_0) {
|
||||||
try {
|
try {
|
||||||
@@ -285,13 +293,17 @@ public class CustomRedisTokenStore implements TokenStore {
|
|||||||
conn.set(authKey, serializedAuth);
|
conn.set(authKey, serializedAuth);
|
||||||
conn.set(authToAccessKey, serializedAccessToken);
|
conn.set(authToAccessKey, serializedAccessToken);
|
||||||
}
|
}
|
||||||
//如果是刷新token过期时间,不需要再往集合添加token
|
//如果是续签token,需要先删除集合里旧的值
|
||||||
if (!isExpire) {
|
if (oldAccessToken != null) {
|
||||||
if (!authentication.isClientOnly()) {
|
if (!authentication.isClientOnly()) {
|
||||||
conn.sAdd(approvalKey, serializedAccessToken);
|
conn.sRem(approvalKey, oldAccessToken);
|
||||||
}
|
}
|
||||||
conn.rPush(clientId, serializedAccessToken);
|
conn.lRem(clientId, 1, oldAccessToken);
|
||||||
}
|
}
|
||||||
|
if (!authentication.isClientOnly()) {
|
||||||
|
conn.sAdd(approvalKey, serializedAccessToken);
|
||||||
|
}
|
||||||
|
conn.rPush(clientId, serializedAccessToken);
|
||||||
if (token.getExpiration() != null) {
|
if (token.getExpiration() != null) {
|
||||||
int seconds = token.getExpiresIn();
|
int seconds = token.getExpiresIn();
|
||||||
conn.expire(accessKey, seconds);
|
conn.expire(accessKey, seconds);
|
||||||
|
|||||||
Reference in New Issue
Block a user