fix #I1X2U5 redis的increment命令的反序列化问题
This commit is contained in:
+2
-2
@@ -21,7 +21,7 @@ import org.springframework.security.oauth2.provider.token.TokenStore;
|
||||
@ConditionalOnProperty(prefix = "zlt.oauth2.token.store", name = "type", havingValue = "redis", matchIfMissing = true)
|
||||
public class AuthRedisTokenStore {
|
||||
@Bean
|
||||
public TokenStore tokenStore(RedisConnectionFactory connectionFactory, SecurityProperties securityProperties) {
|
||||
return new CustomRedisTokenStore(connectionFactory, securityProperties);
|
||||
public TokenStore tokenStore(RedisConnectionFactory connectionFactory, SecurityProperties securityProperties, RedisSerializer<Object> redisValueSerializer) {
|
||||
return new CustomRedisTokenStore(connectionFactory, securityProperties, redisValueSerializer);
|
||||
}
|
||||
}
|
||||
|
||||
+9
-2
@@ -4,6 +4,7 @@ import com.central.common.constant.SecurityConstants;
|
||||
import com.central.oauth2.common.properties.SecurityProperties;
|
||||
import org.springframework.data.redis.connection.RedisConnection;
|
||||
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||
import org.springframework.data.redis.serializer.RedisSerializer;
|
||||
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
|
||||
import org.springframework.security.oauth2.common.ExpiringOAuth2RefreshToken;
|
||||
import org.springframework.security.oauth2.common.OAuth2AccessToken;
|
||||
@@ -61,9 +62,15 @@ public class CustomRedisTokenStore implements TokenStore {
|
||||
*/
|
||||
private SecurityProperties securityProperties;
|
||||
|
||||
public CustomRedisTokenStore(RedisConnectionFactory connectionFactory, SecurityProperties securityProperties) {
|
||||
/**
|
||||
* 业务redis的value序列化
|
||||
*/
|
||||
private RedisSerializer<Object> redisValueSerializer;
|
||||
|
||||
public CustomRedisTokenStore(RedisConnectionFactory connectionFactory, SecurityProperties securityProperties, RedisSerializer<Object> redisValueSerializer) {
|
||||
this.connectionFactory = connectionFactory;
|
||||
this.securityProperties = securityProperties;
|
||||
this.redisValueSerializer = redisValueSerializer;
|
||||
if (springDataRedis_2_0) {
|
||||
this.loadRedisConnectionMethods_2_0();
|
||||
}
|
||||
@@ -111,7 +118,7 @@ public class CustomRedisTokenStore implements TokenStore {
|
||||
}
|
||||
|
||||
private ClientDetails deserializeClientDetails(byte[] bytes) {
|
||||
return serializationStrategy.deserialize(bytes, ClientDetails.class);
|
||||
return (ClientDetails)redisValueSerializer.deserialize(bytes);
|
||||
}
|
||||
|
||||
private byte[] serialize(String string) {
|
||||
|
||||
+1
-1
@@ -41,7 +41,7 @@ public class RedisAutoConfigure {
|
||||
|
||||
@Bean
|
||||
public RedisSerializer<Object> redisValueSerializer() {
|
||||
return RedisSerializer.java();
|
||||
return RedisSerializer.json();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+52
@@ -8,6 +8,9 @@ import org.springframework.data.redis.core.HashOperations;
|
||||
import org.springframework.data.redis.core.ListOperations;
|
||||
import org.springframework.data.redis.core.RedisCallback;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.serializer.RedisSerializer;
|
||||
import org.springframework.data.redis.serializer.SerializationUtils;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
@@ -146,6 +149,17 @@ public class RedisRepository {
|
||||
public Object get(final String key) {
|
||||
return redisTemplate.opsForValue().get(key);
|
||||
}
|
||||
/**
|
||||
* 根据key获取对象
|
||||
*
|
||||
* @param key the key
|
||||
* @param valueSerializer 序列化
|
||||
* @return the string
|
||||
*/
|
||||
public Object get(final String key, RedisSerializer<Object> valueSerializer) {
|
||||
byte[] rawKey = rawKey(key);
|
||||
return redisTemplate.execute(connection -> deserializeValue(connection.get(rawKey), valueSerializer), true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ops for hash hash operations.
|
||||
@@ -361,6 +375,20 @@ public class RedisRepository {
|
||||
return opsForList().range(key, start, end);
|
||||
}
|
||||
|
||||
/**
|
||||
* redis List数据结构 : 返回列表 key 中指定区间内的元素,区间以偏移量 start 和 end 指定。
|
||||
*
|
||||
* @param key the key
|
||||
* @param start the start
|
||||
* @param end the end
|
||||
* @param valueSerializer 序列化
|
||||
* @return the list
|
||||
*/
|
||||
public List<Object> getList(String key, int start, int end, RedisSerializer<Object> valueSerializer) {
|
||||
byte[] rawKey = rawKey(key);
|
||||
return redisTemplate.execute(connection -> deserializeValues(connection.lRange(rawKey, start, end), valueSerializer), true);
|
||||
}
|
||||
|
||||
/**
|
||||
* redis List数据结构 : 批量存储
|
||||
*
|
||||
@@ -382,4 +410,28 @@ public class RedisRepository {
|
||||
public void insert(String key, long index, Object value) {
|
||||
opsForList().set(key, index, value);
|
||||
}
|
||||
|
||||
private byte[] rawKey(Object key) {
|
||||
Assert.notNull(key, "non null key required");
|
||||
|
||||
if (key instanceof byte[]) {
|
||||
return (byte[]) key;
|
||||
}
|
||||
RedisSerializer<Object> redisSerializer = (RedisSerializer<Object>)redisTemplate.getKeySerializer();
|
||||
return redisSerializer.serialize(key);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user