Merge remote-tracking branch 'origin/dev' into dev

This commit is contained in:
jarvis
2022-04-10 15:42:57 +08:00
5 changed files with 651 additions and 476 deletions
@@ -39,6 +39,7 @@ import java.util.List;
*/ */
public class CustomRedisTokenStore implements TokenStore { public class CustomRedisTokenStore implements TokenStore {
private static final String ACCESS = "access:"; private static final String ACCESS = "access:";
private static final String ACCESS_BAK = "access_bak:";
private static final String AUTH_TO_ACCESS = "auth_to_access:"; private static final String AUTH_TO_ACCESS = "auth_to_access:";
private static final String REFRESH_AUTH = "refresh_auth:"; private static final String REFRESH_AUTH = "refresh_auth:";
private static final String ACCESS_TO_REFRESH = "access_to_refresh:"; private static final String ACCESS_TO_REFRESH = "access_to_refresh:";
@@ -118,7 +119,7 @@ public class CustomRedisTokenStore implements TokenStore {
} }
private ClientDetails deserializeClientDetails(byte[] bytes) { private ClientDetails deserializeClientDetails(byte[] bytes) {
return (ClientDetails)redisValueSerializer.deserialize(bytes); return (ClientDetails) redisValueSerializer.deserialize(bytes);
} }
private byte[] serialize(String string) { private byte[] serialize(String string) {
@@ -166,7 +167,7 @@ public class CustomRedisTokenStore implements TokenStore {
//获取过期时长 //获取过期时长
int validitySeconds = getAccessTokenValiditySeconds(clientAuth.getClientId()); int validitySeconds = getAccessTokenValiditySeconds(clientAuth.getClientId());
if (validitySeconds > 0) { if (validitySeconds > 0) {
double expiresRatio = token.getExpiresIn() / (double)validitySeconds; double expiresRatio = token.getExpiresIn() / (double) validitySeconds;
//判断是否需要续签,当前剩余时间小于过期时长的50%则续签 //判断是否需要续签,当前剩余时间小于过期时长的50%则续签
if (expiresRatio <= securityProperties.getAuth().getRenew().getTimeRatio()) { if (expiresRatio <= securityProperties.getAuth().getRenew().getTimeRatio()) {
//更新AccessToken过期时间 //更新AccessToken过期时间
@@ -182,6 +183,7 @@ public class CustomRedisTokenStore implements TokenStore {
/** /**
* 判断应用自动续签是否满足白名单和黑名单的过滤逻辑 * 判断应用自动续签是否满足白名单和黑名单的过滤逻辑
*
* @param clientId 应用id * @param clientId 应用id
* @return 是否满足 * @return 是否满足
*/ */
@@ -193,7 +195,7 @@ public class CustomRedisTokenStore implements TokenStore {
List<String> exclusiveClientIds = securityProperties.getAuth().getRenew().getExclusiveClientIds(); List<String> exclusiveClientIds = securityProperties.getAuth().getRenew().getExclusiveClientIds();
if (includeClientIds.size() > 0) { if (includeClientIds.size() > 0) {
result = includeClientIds.contains(clientId); result = includeClientIds.contains(clientId);
} else if(exclusiveClientIds.size() > 0) { } else if (exclusiveClientIds.size() > 0) {
result = !exclusiveClientIds.contains(clientId); result = !exclusiveClientIds.contains(clientId);
} }
return result; return result;
@@ -201,6 +203,7 @@ public class CustomRedisTokenStore implements TokenStore {
/** /**
* 获取token的总有效时长 * 获取token的总有效时长
*
* @param clientId 应用id * @param clientId 应用id
*/ */
private int getAccessTokenValiditySeconds(String clientId) { private int getAccessTokenValiditySeconds(String clientId) {
@@ -256,12 +259,14 @@ public class CustomRedisTokenStore implements TokenStore {
/** /**
* 存储token * 存储token
*
* @param isRenew 是否续签 * @param isRenew 是否续签
*/ */
private void storeAccessToken(OAuth2AccessToken token, OAuth2Authentication authentication, boolean isRenew) { 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());
byte[] accessBakKey = serializeKey(ACCESS_BAK + token.getValue());
byte[] authKey = serializeKey(SecurityConstants.REDIS_TOKEN_AUTH + token.getValue()); byte[] authKey = serializeKey(SecurityConstants.REDIS_TOKEN_AUTH + token.getValue());
byte[] authToAccessKey = serializeKey(AUTH_TO_ACCESS + authenticationKeyGenerator.extractKey(authentication)); byte[] authToAccessKey = serializeKey(AUTH_TO_ACCESS + authenticationKeyGenerator.extractKey(authentication));
byte[] approvalKey = serializeKey(SecurityConstants.REDIS_UNAME_TO_ACCESS + getApprovalKey(authentication)); byte[] approvalKey = serializeKey(SecurityConstants.REDIS_UNAME_TO_ACCESS + getApprovalKey(authentication));
@@ -279,6 +284,7 @@ public class CustomRedisTokenStore implements TokenStore {
if (springDataRedis_2_0) { if (springDataRedis_2_0) {
try { try {
this.redisConnectionSet_2_0.invoke(conn, accessKey, serializedAccessToken); this.redisConnectionSet_2_0.invoke(conn, accessKey, serializedAccessToken);
this.redisConnectionSet_2_0.invoke(conn, accessBakKey, serializedAccessToken);
this.redisConnectionSet_2_0.invoke(conn, authKey, serializedAuth); this.redisConnectionSet_2_0.invoke(conn, authKey, serializedAuth);
this.redisConnectionSet_2_0.invoke(conn, authToAccessKey, serializedAccessToken); this.redisConnectionSet_2_0.invoke(conn, authToAccessKey, serializedAccessToken);
} catch (Exception ex) { } catch (Exception ex) {
@@ -286,6 +292,7 @@ public class CustomRedisTokenStore implements TokenStore {
} }
} else { } else {
conn.set(accessKey, serializedAccessToken); conn.set(accessKey, serializedAccessToken);
conn.set(accessBakKey, serializedAccessToken);
conn.set(authKey, serializedAuth); conn.set(authKey, serializedAuth);
conn.set(authToAccessKey, serializedAccessToken); conn.set(authToAccessKey, serializedAccessToken);
} }
@@ -303,6 +310,7 @@ public class CustomRedisTokenStore implements TokenStore {
if (token.getExpiration() != null) { if (token.getExpiration() != null) {
int seconds = token.getExpiresIn(); int seconds = token.getExpiresIn();
conn.expire(accessKey, seconds); conn.expire(accessKey, seconds);
conn.expire(accessBakKey, seconds + 60);
conn.expire(authKey, seconds); conn.expire(authKey, seconds);
conn.expire(authToAccessKey, seconds); conn.expire(authToAccessKey, seconds);
conn.expire(clientId, seconds); conn.expire(clientId, seconds);
@@ -363,6 +371,7 @@ public class CustomRedisTokenStore implements TokenStore {
public void removeAccessToken(String tokenValue) { public void removeAccessToken(String tokenValue) {
byte[] accessKey = serializeKey(ACCESS + tokenValue); byte[] accessKey = serializeKey(ACCESS + tokenValue);
byte[] accessBakKey = serializeKey(ACCESS_BAK + tokenValue);
byte[] authKey = serializeKey(SecurityConstants.REDIS_TOKEN_AUTH + tokenValue); byte[] authKey = serializeKey(SecurityConstants.REDIS_TOKEN_AUTH + tokenValue);
byte[] accessToRefreshKey = serializeKey(ACCESS_TO_REFRESH + tokenValue); byte[] accessToRefreshKey = serializeKey(ACCESS_TO_REFRESH + tokenValue);
RedisConnection conn = getConnection(); RedisConnection conn = getConnection();
@@ -371,6 +380,7 @@ public class CustomRedisTokenStore implements TokenStore {
byte[] auth = conn.get(authKey); byte[] auth = conn.get(authKey);
conn.openPipeline(); conn.openPipeline();
conn.del(accessKey); conn.del(accessKey);
conn.del(accessBakKey);
conn.del(accessToRefreshKey); conn.del(accessToRefreshKey);
// Don't remove the refresh token - it's up to the caller to do that // Don't remove the refresh token - it's up to the caller to do that
conn.del(authKey); conn.del(authKey);
@@ -154,6 +154,10 @@ public interface SecurityConstants {
* redis中授权token对应的key * redis中授权token对应的key
*/ */
String REDIS_TOKEN_AUTH = "auth:"; String REDIS_TOKEN_AUTH = "auth:";
/**
* 值同access 过期时间+60
*/
String ACCESS_BAK = "access_bak:";
/** /**
* redis中应用对应的token集合的key * redis中应用对应的token集合的key
*/ */
@@ -1,473 +1,498 @@
package com.central.common.redis.template; package com.central.common.redis.template;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DataAccessException; import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.connection.RedisClusterNode; import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection; import org.springframework.data.redis.connection.RedisClusterNode;
import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisServerCommands; import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.*; import org.springframework.data.redis.connection.RedisServerCommands;
import org.springframework.data.redis.serializer.RedisSerializer; import org.springframework.data.redis.core.*;
import org.springframework.data.redis.serializer.SerializationUtils; import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.util.Assert; import org.springframework.data.redis.serializer.SerializationUtils;
import org.springframework.util.Assert;
import java.util.*;
import java.util.concurrent.TimeUnit; import java.util.*;
import java.util.concurrent.TimeUnit;
/**
* Redis Repository /**
* redis 基本操作 可扩展,基本够用了 * Redis Repository
* * redis 基本操作 可扩展,基本够用了
* @author zlt *
* <p> * @author zlt
* Blog: https://zlt2000.gitee.io * <p>
* Github: https://github.com/zlt2000 * Blog: https://zlt2000.gitee.io
*/ * Github: https://github.com/zlt2000
@Slf4j */
public class RedisRepository { @Slf4j
/** public class RedisRepository {
* Spring Redis Template /**
*/ * Spring Redis Template
private RedisTemplate<String, Object> redisTemplate; */
private RedisTemplate<String, Object> redisTemplate;
public RedisRepository(RedisTemplate<String, Object> redisTemplate) {
this.redisTemplate = redisTemplate; public RedisRepository(RedisTemplate<String, Object> redisTemplate) {
} this.redisTemplate = redisTemplate;
}
/**
* 获取链接工厂 /**
*/ * 获取链接工厂
public RedisConnectionFactory getConnectionFactory() { */
return this.redisTemplate.getConnectionFactory(); public RedisConnectionFactory getConnectionFactory() {
} return this.redisTemplate.getConnectionFactory();
}
/**
* 获取 RedisTemplate对象 /**
*/ * 获取 RedisTemplate对象
public RedisTemplate<String, Object> getRedisTemplate() { */
return redisTemplate; public RedisTemplate<String, Object> getRedisTemplate() {
} return redisTemplate;
}
/**
* 清空DB /**
* * 清空DB
* @param node redis 节点 *
*/ * @param node redis 节点
public void flushDB(RedisClusterNode node) { */
this.redisTemplate.opsForCluster().flushDb(node); public void flushDB(RedisClusterNode node) {
} this.redisTemplate.opsForCluster().flushDb(node);
}
/**
* 添加到带有 过期时间的 缓存 /**
* * 添加到带有 过期时间的 缓存
* @param key redis主键 *
* @param value 值 * @param key redis主键
* @param time 过期时间(单位秒) * @param value 值
*/ * @param time 过期时间(单位秒)
public void setExpire(final byte[] key, final byte[] value, final long time) { */
redisTemplate.execute((RedisCallback<Long>) connection -> { public void setExpire(final byte[] key, final byte[] value, final long time) {
connection.setEx(key, time, value); redisTemplate.execute((RedisCallback<Long>) connection -> {
return 1L; connection.setEx(key, time, value);
}); return 1L;
} });
}
/**
* 添加到带有 过期时间的 缓存 /**
* * 添加到带有 过期时间的 缓存
* @param key redis主键 *
* @param value 值 * @param key redis主键
* @param time 过期时间 * @param value 值
* @param timeUnit 过期时间单位 * @param time 过期时间
*/ * @param timeUnit 过期时间单位
public void setExpire(final String key, final Object value, final long time, final TimeUnit timeUnit) { */
redisTemplate.opsForValue().set(key, value, time, timeUnit); public void setExpire(final String key, final Object value, final long time, final TimeUnit timeUnit) {
} redisTemplate.opsForValue().set(key, value, time, timeUnit);
public void setExpire(final String key, final Object value, final long time) { }
this.setExpire(key, value, time, TimeUnit.SECONDS); public void setExpire(final String key, final Object value, final long time) {
} this.setExpire(key, value, time, TimeUnit.SECONDS);
public void setExpire(final String key, final Object value, final long time, final TimeUnit timeUnit, RedisSerializer<Object> valueSerializer) { }
byte[] rawKey = rawKey(key); public void setExpire(final String key, final Object value, final long time, final TimeUnit timeUnit, RedisSerializer<Object> valueSerializer) {
byte[] rawValue = rawValue(value, valueSerializer); byte[] rawKey = rawKey(key);
byte[] rawValue = rawValue(value, valueSerializer);
redisTemplate.execute(new RedisCallback<Object>() {
@Override redisTemplate.execute(new RedisCallback<Object>() {
public Object doInRedis(RedisConnection connection) throws DataAccessException { @Override
potentiallyUsePsetEx(connection); public Object doInRedis(RedisConnection connection) throws DataAccessException {
return null; potentiallyUsePsetEx(connection);
} return null;
public void potentiallyUsePsetEx(RedisConnection connection) { }
if (!TimeUnit.MILLISECONDS.equals(timeUnit) || !failsafeInvokePsetEx(connection)) { public void potentiallyUsePsetEx(RedisConnection connection) {
connection.setEx(rawKey, TimeoutUtils.toSeconds(time, timeUnit), rawValue); if (!TimeUnit.MILLISECONDS.equals(timeUnit) || !failsafeInvokePsetEx(connection)) {
} connection.setEx(rawKey, TimeoutUtils.toSeconds(time, timeUnit), rawValue);
} }
private boolean failsafeInvokePsetEx(RedisConnection connection) { }
boolean failed = false; private boolean failsafeInvokePsetEx(RedisConnection connection) {
try { boolean failed = false;
connection.pSetEx(rawKey, time, rawValue); try {
} catch (UnsupportedOperationException e) { connection.pSetEx(rawKey, time, rawValue);
failed = true; } catch (UnsupportedOperationException e) {
} failed = true;
return !failed; }
} return !failed;
}, true); }
} }, true);
}
/**
* 一次性添加数组到 过期时间的 缓存,不用多次连接,节省开销 /**
* * 一次性添加数组到 过期时间的 缓存,不用多次连接,节省开销
* @param keys redis主键数组 *
* @param values 值数组 * @param keys redis主键数组
* @param time 过期时间(单位秒) * @param values 值数组
*/ * @param time 过期时间(单位秒)
public void setExpire(final String[] keys, final Object[] values, final long time) { */
for (int i = 0; i < keys.length; i++) { public void setExpire(final String[] keys, final Object[] values, final long time) {
redisTemplate.opsForValue().set(keys[i], values[i], time, TimeUnit.SECONDS); for (int i = 0; i < keys.length; i++) {
} redisTemplate.opsForValue().set(keys[i], values[i], time, TimeUnit.SECONDS);
} }
}
/**
* 一次性添加数组到 过期时间的 缓存,不用多次连接,节省开销 /**
* * 一次性添加数组到 过期时间的 缓存,不用多次连接,节省开销
* @param keys the keys *
* @param values the values * @param keys the keys
*/ * @param values the values
public void set(final String[] keys, final Object[] values) { */
for (int i = 0; i < keys.length; i++) { public void set(final String[] keys, final Object[] values) {
redisTemplate.opsForValue().set(keys[i], values[i]); for (int i = 0; i < keys.length; i++) {
} redisTemplate.opsForValue().set(keys[i], values[i]);
} }
}
/**
* 添加到缓存 /**
* * 添加到缓存
* @param key the key *
* @param value the value * @param key the key
*/ * @param value the value
public void set(final String key, final Object value) { */
redisTemplate.opsForValue().set(key, value); public void set(final String key, final Object value) {
} redisTemplate.opsForValue().set(key, value);
}
/**
* 查询在以keyPatten的所有 key /**
* * 查询在以keyPatten的所有 key
* @param keyPatten the key patten *
* @return the set * @param keyPatten the key patten
*/ * @return the set
public Set<String> keys(final String keyPatten) { */
return redisTemplate.keys(keyPatten + "*"); public Set<String> keys(final String keyPatten) {
} return redisTemplate.keys(keyPatten + "*");
}
/**
* 根据key获取对象 /**
* * 根据key获取对象
* @param key the key *
* @return the byte [ ] * @param key the key
*/ * @return the byte [ ]
public byte[] get(final byte[] key) { */
return redisTemplate.execute((RedisCallback<byte[]>) connection -> connection.get(key)); public byte[] get(final byte[] key) {
} return redisTemplate.execute((RedisCallback<byte[]>) connection -> connection.get(key));
}
/**
* 根据key获取对象 /**
* * 根据key获取对象
* @param key the key *
* @return the string * @param key the key
*/ * @return the string
public Object get(final String key) { */
return redisTemplate.opsForValue().get(key); public Object get(final String key) {
} return redisTemplate.opsForValue().get(key);
/** }
* 根据key获取对象
* /**
* @param key the key *获取原来key键对应的值并重新赋新值。
* @param valueSerializer 序列化 * @param key
* @return the string * @param value
*/ * @return
public Object get(final String key, RedisSerializer<Object> valueSerializer) { */
byte[] rawKey = rawKey(key); public String getAndSet(final String key,String value) {
return redisTemplate.execute(connection -> deserializeValue(connection.get(rawKey), valueSerializer), true); String result = null;
} if (StringUtils.isEmpty(key)){
log.error("非法入参");
/** return null;
* Ops for hash hash operations. }
* try {
* @return the hash operations Object object =redisTemplate.opsForValue().getAndSet(key, value);
*/ if (object !=null){
public HashOperations<String, String, Object> opsForHash() { result = object.toString();
return redisTemplate.opsForHash(); }
} }catch (Exception e){
log.error("redisTemplate操作异常",e);
/** }
* 对HashMap操作 return result;
* }
* @param key the key /**
* @param hashKey the hash key * 根据key获取对象
* @param hashValue the hash value *
*/ * @param key the key
public void putHashValue(String key, String hashKey, Object hashValue) { * @param valueSerializer 序列化
opsForHash().put(key, hashKey, hashValue); * @return the string
} */
public Object get(final String key, RedisSerializer<Object> valueSerializer) {
/** byte[] rawKey = rawKey(key);
* 获取单个field对应的值 return redisTemplate.execute(connection -> deserializeValue(connection.get(rawKey), valueSerializer), true);
* }
* @param key the key
* @param hashKey the hash key
* @return the hash values /**
*/ * Ops for hash hash operations.
public Object getHashValues(String key, String hashKey) { *
return opsForHash().get(key, hashKey); * @return the hash operations
} */
public HashOperations<String, String, Object> opsForHash() {
/** return redisTemplate.opsForHash();
* 根据key值删除 }
*
* @param key the key /**
* @param hashKeys the hash keys * 对HashMap操作
*/ *
public void delHashValues(String key, Object... hashKeys) { * @param key the key
opsForHash().delete(key, hashKeys); * @param hashKey the hash key
} * @param hashValue the hash value
*/
/** public void putHashValue(String key, String hashKey, Object hashValue) {
* key只匹配map opsForHash().put(key, hashKey, hashValue);
* }
* @param key the key
* @return the hash value /**
*/ * 获取单个field对应的值
public Map<String, Object> getHashValue(String key) { *
return opsForHash().entries(key); * @param key the key
} * @param hashKey the hash key
* @return the hash values
/** */
* 批量添加 public Object getHashValues(String key, String hashKey) {
* return opsForHash().get(key, hashKey);
* @param key the key }
* @param map the map
*/ /**
public void putHashValues(String key, Map<String, Object> map) { * 根据key值删除
opsForHash().putAll(key, map); *
} * @param key the key
* @param hashKeys the hash keys
/** */
* 集合数量 public void delHashValues(String key, Object... hashKeys) {
* opsForHash().delete(key, hashKeys);
* @return the long }
*/
public long dbSize() { /**
return redisTemplate.execute(RedisServerCommands::dbSize); * key只匹配map
} *
* @param key the key
/** * @return the hash value
* 清空redis存储的数据 */
* public Map<String, Object> getHashValue(String key) {
* @return the string return opsForHash().entries(key);
*/ }
public String flushDB() {
return redisTemplate.execute((RedisCallback<String>) connection -> { /**
connection.flushDb(); * 批量添加
return "ok"; *
}); * @param key the key
} * @param map the map
*/
/** public void putHashValues(String key, Map<String, Object> map) {
* 判断某个主键是否存在 opsForHash().putAll(key, map);
* }
* @param key the key
* @return the boolean /**
*/ * 集合数量
public boolean exists(final String key) { *
return redisTemplate.hasKey(key); * @return the long
} */
public long dbSize() {
return redisTemplate.execute(RedisServerCommands::dbSize);
/** }
* 删除key
* /**
* @param keys the keys * 清空redis存储的数据
* @return the long *
*/ * @return the string
public boolean del(final String... keys) { */
boolean result = false; public String flushDB() {
for (String key : keys) { return redisTemplate.execute((RedisCallback<String>) connection -> {
result = redisTemplate.delete(key); connection.flushDb();
} return "ok";
return result; });
} }
/** /**
* 对某个主键对应的值加一,value值必须是全数字的字符串 * 判断某个主键是否存在
* *
* @param key the key * @param key the key
* @return the long * @return the boolean
*/ */
public long incr(final String key) { public boolean exists(final String key) {
return redisTemplate.opsForValue().increment(key); return redisTemplate.hasKey(key);
} }
/**
* redis List 引擎 /**
* * 删除key
* @return the list operations *
*/ * @param keys the keys
public ListOperations<String, Object> opsForList() { * @return the long
return redisTemplate.opsForList(); */
} public boolean del(final String... keys) {
boolean result = false;
/** for (String key : keys) {
* redis List数据结构 : 将一个或多个值 value 插入到列表 key 的表头 result = redisTemplate.delete(key);
* }
* @param key the key return result;
* @param value the value }
* @return the long
*/ /**
public Long leftPush(String key, Object value) { * 对某个主键对应的值加一,value值必须是全数字的字符串
return opsForList().leftPush(key, value); *
} * @param key the key
* @return the long
/** */
* redis List数据结构 : 移除并返回列表 key 的头元素 public long incr(final String key) {
* return redisTemplate.opsForValue().increment(key);
* @param key the key }
* @return the string
*/ /**
public Object leftPop(String key) { * redis List 引擎
return opsForList().leftPop(key); *
} * @return the list operations
*/
/** public ListOperations<String, Object> opsForList() {
* redis List数据结构 :将一个或多个值 value 插入到列表 key 的表尾(最右边)。 return redisTemplate.opsForList();
* }
* @param key the key
* @param value the value /**
* @return the long * redis List数据结构 : 将一个或多个值 value 插入到列表 key 的表头
*/ *
public Long in(String key, Object value) { * @param key the key
return opsForList().rightPush(key, value); * @param value the value
} * @return the long
*/
/** public Long leftPush(String key, Object value) {
* redis List数据结构 : 移除并返回列表 key 的末尾元素 return opsForList().leftPush(key, value);
* }
* @param key the key
* @return the string /**
*/ * redis List数据结构 : 移除并返回列表 key 的头元素
public Object rightPop(String key) { *
return opsForList().rightPop(key); * @param key the key
} * @return the string
*/
public Object leftPop(String key) {
/** return opsForList().leftPop(key);
* redis List数据结构 : 返回列表 key 的长度 ; 如果 key 不存在,则 key 被解释为一个空列表,返回 0 ; 如果 key 不是列表类型,返回一个错误。 }
*
* @param key the key /**
* @return the long * redis List数据结构 :将一个或多个值 value 插入到列表 key 的表尾(最右边)。
*/ *
public Long length(String key) { * @param key the key
return opsForList().size(key); * @param value the value
} * @return the long
*/
public Long in(String key, Object value) {
/** return opsForList().rightPush(key, value);
* redis List数据结构 : 根据参数 i 的值,移除列表中与参数 value 相等的元素 }
*
* @param key the key /**
* @param i the * redis List数据结构 : 移除并返回列表 key 的末尾元素
* @param value the value *
*/ * @param key the key
public void remove(String key, long i, Object value) { * @return the string
opsForList().remove(key, i, value); */
} public Object rightPop(String key) {
return opsForList().rightPop(key);
/** }
* redis List数据结构 : 将列表 key 下标为 index 的元素的值设置为 value
*
* @param key the key /**
* @param index the index * redis List数据结构 : 返回列表 key 的长度 ; 如果 key 不存在,则 key 被解释为一个空列表,返回 0 ; 如果 key 不是列表类型,返回一个错误。
* @param value the value *
*/ * @param key the key
public void set(String key, long index, Object value) { * @return the long
opsForList().set(key, index, value); */
} public Long length(String key) {
return opsForList().size(key);
/** }
* redis List数据结构 : 返回列表 key 中指定区间内的元素,区间以偏移量 start 和 end 指定。
*
* @param key the key /**
* @param start the start * redis List数据结构 : 根据参数 i 的值,移除列表中与参数 value 相等的元素
* @param end the end *
* @return the list * @param key the key
*/ * @param i the
public List<Object> getList(String key, int start, int end) { * @param value the value
return opsForList().range(key, start, end); */
} public void remove(String key, long i, Object value) {
opsForList().remove(key, i, value);
/** }
* redis List数据结构 : 返回列表 key 中指定区间内的元素,区间以偏移量 start 和 end 指定。
* /**
* @param key the key * redis List数据结构 : 将列表 key 下标为 index 的元素的值设置为 value
* @param start the start *
* @param end the end * @param key the key
* @param valueSerializer 序列化 * @param index the index
* @return the list * @param value the value
*/ */
public List<Object> getList(String key, int start, int end, RedisSerializer<Object> valueSerializer) { public void set(String key, long index, Object value) {
byte[] rawKey = rawKey(key); opsForList().set(key, index, value);
return redisTemplate.execute(connection -> deserializeValues(connection.lRange(rawKey, start, end), valueSerializer), true); }
}
/**
/** * redis List数据结构 : 返回列表 key 中指定区间内的元素,区间以偏移量 start 和 end 指定。
* redis List数据结构 : 批量存储 *
* * @param key the key
* @param key the key * @param start the start
* @param list the list * @param end the end
* @return the long * @return the list
*/ */
public Long leftPushAll(String key, List<String> list) { public List<Object> getList(String key, int start, int end) {
return opsForList().leftPushAll(key, list); return opsForList().range(key, start, end);
} }
/** /**
* redis List数据结构 : 将值 value 插入到列表 key 当中,位于值 index 之前或之后,默认之后。 * redis List数据结构 : 返回列表 key 中指定区间内的元素,区间以偏移量 start 和 end 指定。
* *
* @param key the key * @param key the key
* @param index the index * @param start the start
* @param value the value * @param end the end
*/ * @param valueSerializer 序列化
public void insert(String key, long index, Object value) { * @return the list
opsForList().set(key, index, value); */
} public List<Object> getList(String key, int start, int end, RedisSerializer<Object> valueSerializer) {
byte[] rawKey = rawKey(key);
private byte[] rawKey(Object key) { return redisTemplate.execute(connection -> deserializeValues(connection.lRange(rawKey, start, end), valueSerializer), true);
Assert.notNull(key, "non null key required"); }
if (key instanceof byte[]) { /**
return (byte[]) key; * redis List数据结构 : 批量存储
} *
RedisSerializer<Object> redisSerializer = (RedisSerializer<Object>)redisTemplate.getKeySerializer(); * @param key the key
return redisSerializer.serialize(key); * @param list the list
} * @return the long
private byte[] rawValue(Object value, RedisSerializer valueSerializer) { */
if (value instanceof byte[]) { public Long leftPushAll(String key, List<String> list) {
return (byte[]) value; return opsForList().leftPushAll(key, list);
} }
return valueSerializer.serialize(value); /**
} * redis List数据结构 : 将值 value 插入到列表 key 当中,位于值 index 之前或之后,默认之后。
*
private List deserializeValues(List<byte[]> rawValues, RedisSerializer<Object> valueSerializer) { * @param key the key
if (valueSerializer == null) { * @param index the index
return rawValues; * @param value the value
} */
return SerializationUtils.deserialize(rawValues, valueSerializer); public void insert(String key, long index, Object value) {
} opsForList().set(key, index, value);
}
private Object deserializeValue(byte[] value, RedisSerializer<Object> valueSerializer) {
if (valueSerializer == null) { private byte[] rawKey(Object key) {
return value; Assert.notNull(key, "non null key required");
}
return valueSerializer.deserialize(value); if (key instanceof byte[]) {
} return (byte[]) key;
} }
RedisSerializer<Object> redisSerializer = (RedisSerializer<Object>)redisTemplate.getKeySerializer();
return redisSerializer.serialize(key);
}
private byte[] rawValue(Object value, RedisSerializer valueSerializer) {
if (value instanceof byte[]) {
return (byte[]) value;
}
return valueSerializer.serialize(value);
}
private List deserializeValues(List<byte[]> rawValues, RedisSerializer<Object> valueSerializer) {
if (valueSerializer == null) {
return rawValues;
}
return SerializationUtils.deserialize(rawValues, valueSerializer);
}
private Object deserializeValue(byte[] value, RedisSerializer<Object> valueSerializer) {
if (valueSerializer == null) {
return value;
}
return valueSerializer.deserialize(value);
}
}
@@ -0,0 +1,24 @@
package com.central.oauth.config;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
/**
*
* redis过期key监听器配置类
* @author zlt
*
*/
@Configuration
public class RedisListenerConfig {
@Bean
@Primary
public RedisMessageListenerContainer redisMessageListenerContainer(RedisConnectionFactory factory) {
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
container.setConnectionFactory(factory);
return container;
}
}
@@ -0,0 +1,112 @@
package com.central.oauth.listener;
import com.central.common.constant.SecurityConstants;
import com.central.common.redis.template.RedisRepository;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.token.store.redis.JdkSerializationStrategy;
import org.springframework.security.oauth2.provider.token.store.redis.RedisTokenStoreSerializationStrategy;
import org.springframework.stereotype.Component;
/**
*
* redis过期key监听器
* @author zlt
*
*/
@Component
@Slf4j
public class RedisKeyExpirationListener extends KeyExpirationEventMessageListener {
@Autowired
private RedisRepository redisRepository;
private RedisTokenStoreSerializationStrategy serializationStrategy = new JdkSerializationStrategy();
private final RedisConnectionFactory connectionFactory;
public RedisKeyExpirationListener(RedisMessageListenerContainer listenerContainer, RedisConnectionFactory connectionFactory) {
super(listenerContainer);
this.connectionFactory = connectionFactory;
}
@Override
public void onMessage(Message message, byte[] pattern) {
if (message == null) {
log.debug("message不能为空");
return;
}
//获取失效的的key
String expiredKey = message.toString();
if (StringUtils.isEmpty(expiredKey)) {
log.debug("expiredKey不能为空");
return;
}
String accesskey = expiredKey.substring(0, expiredKey.indexOf(":") + 1);
if (!"access:".equals(accesskey)) {
log.debug("非需要监听key,跳过");
return;
}
String accessValue = expiredKey.substring(expiredKey.indexOf(":") + 1);
// 分布式集群部署下防止一个过期被多个服务重复消费
String qc = "qc:" + accessValue;
String oldLock = redisRepository.getAndSet(qc, "1");
if (StringUtils.isNotEmpty(oldLock) && "1".equals(oldLock)) {
log.debug("其他节点已经处理了该数据,跳过");
return;
}
byte[] accessBakKey = serializeKey(SecurityConstants.ACCESS_BAK + accessValue);
byte[] authKey = serializeKey(SecurityConstants.REDIS_TOKEN_AUTH + accessValue);
RedisConnection conn = getConnection();
try {
byte[] access = conn.get(accessBakKey);
byte[] auth = conn.get(authKey);
OAuth2Authentication authentication = deserializeAuthentication(auth);
if (authentication != null) {
byte[] unameKey = serializeKey(SecurityConstants.REDIS_UNAME_TO_ACCESS + getApprovalKey(authentication));
byte[] clientId = serializeKey(SecurityConstants.REDIS_CLIENT_ID_TO_ACCESS + authentication.getOAuth2Request().getClientId());
conn.openPipeline();
conn.lRem(unameKey, 1, access);
conn.lRem(clientId, 1, access);
conn.closePipeline();
}
} catch (Exception e) {
log.error(e.getMessage());
} finally {
conn.del(serializeKey(qc));
conn.close();
}
}
private byte[] serializeKey(String object) {
return serialize("" + object);
}
private byte[] serialize(String string) {
return serializationStrategy.serialize(string);
}
private RedisConnection getConnection() {
return connectionFactory.getConnection();
}
private OAuth2Authentication deserializeAuthentication(byte[] bytes) {
return serializationStrategy.deserialize(bytes, OAuth2Authentication.class);
}
private static String getApprovalKey(OAuth2Authentication authentication) {
String userName = authentication.getUserAuthentication() == null ? ""
: authentication.getUserAuthentication().getName();
return getApprovalKey(authentication.getOAuth2Request().getClientId(), userName);
}
private static String getApprovalKey(String clientId, String userName) {
return clientId + (userName == null ? "" : ":" + userName);
}
}