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)
|
@ConditionalOnProperty(prefix = "zlt.oauth2.token.store", name = "type", havingValue = "redis", matchIfMissing = true)
|
||||||
public class AuthRedisTokenStore {
|
public class AuthRedisTokenStore {
|
||||||
@Bean
|
@Bean
|
||||||
public TokenStore tokenStore(RedisConnectionFactory connectionFactory, SecurityProperties securityProperties) {
|
public TokenStore tokenStore(RedisConnectionFactory connectionFactory, SecurityProperties securityProperties, RedisSerializer<Object> redisValueSerializer) {
|
||||||
return new CustomRedisTokenStore(connectionFactory, securityProperties);
|
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 com.central.oauth2.common.properties.SecurityProperties;
|
||||||
import org.springframework.data.redis.connection.RedisConnection;
|
import org.springframework.data.redis.connection.RedisConnection;
|
||||||
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
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.DefaultOAuth2AccessToken;
|
||||||
import org.springframework.security.oauth2.common.ExpiringOAuth2RefreshToken;
|
import org.springframework.security.oauth2.common.ExpiringOAuth2RefreshToken;
|
||||||
import org.springframework.security.oauth2.common.OAuth2AccessToken;
|
import org.springframework.security.oauth2.common.OAuth2AccessToken;
|
||||||
@@ -61,9 +62,15 @@ public class CustomRedisTokenStore implements TokenStore {
|
|||||||
*/
|
*/
|
||||||
private SecurityProperties securityProperties;
|
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.connectionFactory = connectionFactory;
|
||||||
this.securityProperties = securityProperties;
|
this.securityProperties = securityProperties;
|
||||||
|
this.redisValueSerializer = redisValueSerializer;
|
||||||
if (springDataRedis_2_0) {
|
if (springDataRedis_2_0) {
|
||||||
this.loadRedisConnectionMethods_2_0();
|
this.loadRedisConnectionMethods_2_0();
|
||||||
}
|
}
|
||||||
@@ -111,7 +118,7 @@ public class CustomRedisTokenStore implements TokenStore {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private ClientDetails deserializeClientDetails(byte[] bytes) {
|
private ClientDetails deserializeClientDetails(byte[] bytes) {
|
||||||
return serializationStrategy.deserialize(bytes, ClientDetails.class);
|
return (ClientDetails)redisValueSerializer.deserialize(bytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
private byte[] serialize(String string) {
|
private byte[] serialize(String string) {
|
||||||
|
|||||||
+1
-1
@@ -41,7 +41,7 @@ public class RedisAutoConfigure {
|
|||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public RedisSerializer<Object> redisValueSerializer() {
|
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.ListOperations;
|
||||||
import org.springframework.data.redis.core.RedisCallback;
|
import org.springframework.data.redis.core.RedisCallback;
|
||||||
import org.springframework.data.redis.core.RedisTemplate;
|
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.*;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
@@ -146,6 +149,17 @@ public class RedisRepository {
|
|||||||
public Object get(final String key) {
|
public Object get(final String key) {
|
||||||
return redisTemplate.opsForValue().get(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.
|
* Ops for hash hash operations.
|
||||||
@@ -361,6 +375,20 @@ public class RedisRepository {
|
|||||||
return opsForList().range(key, start, end);
|
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数据结构 : 批量存储
|
* redis List数据结构 : 批量存储
|
||||||
*
|
*
|
||||||
@@ -382,4 +410,28 @@ public class RedisRepository {
|
|||||||
public void insert(String key, long index, Object value) {
|
public void insert(String key, long index, Object value) {
|
||||||
opsForList().set(key, index, 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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import com.central.oauth.service.ITokensService;
|
|||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.apache.commons.collections4.MapUtils;
|
import org.apache.commons.collections4.MapUtils;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.data.redis.serializer.RedisSerializer;
|
||||||
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
|
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
|
||||||
import org.springframework.security.oauth2.provider.OAuth2Authentication;
|
import org.springframework.security.oauth2.provider.OAuth2Authentication;
|
||||||
import org.springframework.security.oauth2.provider.OAuth2Request;
|
import org.springframework.security.oauth2.provider.OAuth2Request;
|
||||||
@@ -40,8 +41,10 @@ public class RedisTokensServiceImpl implements ITokensService {
|
|||||||
String redisKey = getRedisKey(params, clientId);
|
String redisKey = getRedisKey(params, clientId);
|
||||||
long size = redisRepository.length(redisKey);
|
long size = redisRepository.length(redisKey);
|
||||||
List<TokenVo> result = new ArrayList<>(limit);
|
List<TokenVo> result = new ArrayList<>(limit);
|
||||||
|
RedisSerializer<Object> valueSerializer = RedisSerializer.java();
|
||||||
//查询token集合
|
//查询token集合
|
||||||
List<Object> tokenObjs = redisRepository.getList(redisKey, startEnds[0], startEnds[1]-1);
|
//redisRepository.getRedisTemplate().e
|
||||||
|
List<Object> tokenObjs = redisRepository.getList(redisKey, startEnds[0], startEnds[1]-1, valueSerializer);
|
||||||
if (tokenObjs != null) {
|
if (tokenObjs != null) {
|
||||||
for (Object obj : tokenObjs) {
|
for (Object obj : tokenObjs) {
|
||||||
DefaultOAuth2AccessToken accessToken = (DefaultOAuth2AccessToken)obj;
|
DefaultOAuth2AccessToken accessToken = (DefaultOAuth2AccessToken)obj;
|
||||||
@@ -51,7 +54,7 @@ public class RedisTokensServiceImpl implements ITokensService {
|
|||||||
tokenVo.setExpiration(accessToken.getExpiration());
|
tokenVo.setExpiration(accessToken.getExpiration());
|
||||||
|
|
||||||
//获取用户信息
|
//获取用户信息
|
||||||
Object authObj = redisRepository.get(SecurityConstants.REDIS_TOKEN_AUTH + accessToken.getValue());
|
Object authObj = redisRepository.get(SecurityConstants.REDIS_TOKEN_AUTH + accessToken.getValue(), valueSerializer);
|
||||||
OAuth2Authentication authentication = (OAuth2Authentication)authObj;
|
OAuth2Authentication authentication = (OAuth2Authentication)authObj;
|
||||||
if (authentication != null) {
|
if (authentication != null) {
|
||||||
OAuth2Request request = authentication.getOAuth2Request();
|
OAuth2Request request = authentication.getOAuth2Request();
|
||||||
|
|||||||
Reference in New Issue
Block a user