修复redis序列化问题
This commit is contained in:
+42
-6
@@ -1,13 +1,12 @@
|
|||||||
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.springframework.data.redis.connection.RedisClusterNode;
|
import org.springframework.data.redis.connection.RedisClusterNode;
|
||||||
|
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.connection.RedisServerCommands;
|
import org.springframework.data.redis.connection.RedisServerCommands;
|
||||||
import org.springframework.data.redis.core.HashOperations;
|
import org.springframework.data.redis.core.*;
|
||||||
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.RedisSerializer;
|
||||||
import org.springframework.data.redis.serializer.SerializationUtils;
|
import org.springframework.data.redis.serializer.SerializationUtils;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
@@ -77,10 +76,40 @@ public class RedisRepository {
|
|||||||
*
|
*
|
||||||
* @param key redis主键
|
* @param key redis主键
|
||||||
* @param value 值
|
* @param value 值
|
||||||
* @param time 过期时间(单位秒)
|
* @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) {
|
public void setExpire(final String key, final Object value, final long time) {
|
||||||
redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
|
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);
|
||||||
|
byte[] rawValue = rawValue(value, valueSerializer);
|
||||||
|
|
||||||
|
redisTemplate.execute(new RedisCallback<Object>() {
|
||||||
|
@Override
|
||||||
|
public Object doInRedis(RedisConnection connection) throws DataAccessException {
|
||||||
|
potentiallyUsePsetEx(connection);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
public void potentiallyUsePsetEx(RedisConnection connection) {
|
||||||
|
if (!TimeUnit.MILLISECONDS.equals(timeUnit) || !failsafeInvokePsetEx(connection)) {
|
||||||
|
connection.setEx(rawKey, TimeoutUtils.toSeconds(time, timeUnit), rawValue);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
private boolean failsafeInvokePsetEx(RedisConnection connection) {
|
||||||
|
boolean failed = false;
|
||||||
|
try {
|
||||||
|
connection.pSetEx(rawKey, time, rawValue);
|
||||||
|
} catch (UnsupportedOperationException e) {
|
||||||
|
failed = true;
|
||||||
|
}
|
||||||
|
return !failed;
|
||||||
|
}
|
||||||
|
}, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -420,6 +449,13 @@ public class RedisRepository {
|
|||||||
RedisSerializer<Object> redisSerializer = (RedisSerializer<Object>)redisTemplate.getKeySerializer();
|
RedisSerializer<Object> redisSerializer = (RedisSerializer<Object>)redisTemplate.getKeySerializer();
|
||||||
return redisSerializer.serialize(key);
|
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) {
|
private List deserializeValues(List<byte[]> rawValues, RedisSerializer<Object> valueSerializer) {
|
||||||
if (valueSerializer == null) {
|
if (valueSerializer == null) {
|
||||||
|
|||||||
@@ -123,7 +123,7 @@ INSERT INTO `sys_menu` VALUES (2, 12, '用户管理', '#!user', 'system/user.htm
|
|||||||
INSERT INTO `sys_menu` VALUES (3, 12, '角色管理', '#!role', 'system/role.html', NULL, 'layui-icon-user', 3, '2017-11-17 16:56:59', '2019-01-14 15:34:40', 1, 0, 'webApp');
|
INSERT INTO `sys_menu` VALUES (3, 12, '角色管理', '#!role', 'system/role.html', NULL, 'layui-icon-user', 3, '2017-11-17 16:56:59', '2019-01-14 15:34:40', 1, 0, 'webApp');
|
||||||
INSERT INTO `sys_menu` VALUES (4, 12, '菜单管理', '#!menus', 'system/menus.html', NULL, 'layui-icon-menu-fill', 4, '2017-11-17 16:56:59', '2018-09-03 02:23:47', 1, 0, 'webApp');
|
INSERT INTO `sys_menu` VALUES (4, 12, '菜单管理', '#!menus', 'system/menus.html', NULL, 'layui-icon-menu-fill', 4, '2017-11-17 16:56:59', '2018-09-03 02:23:47', 1, 0, 'webApp');
|
||||||
INSERT INTO `sys_menu` VALUES (9, 37, '文件中心', '#!files', 'files/files.html', NULL, 'layui-icon-file', 3, '2017-11-17 16:56:59', '2019-01-17 20:18:44', 1, 0, 'webApp');
|
INSERT INTO `sys_menu` VALUES (9, 37, '文件中心', '#!files', 'files/files.html', NULL, 'layui-icon-file', 3, '2017-11-17 16:56:59', '2019-01-17 20:18:44', 1, 0, 'webApp');
|
||||||
INSERT INTO `sys_menu` VALUES (10, 37, '文档中心', '#!swagger', 'http://127.0.0.1:9900/swagger-ui.html', NULL, 'layui-icon-app', 4, '2017-11-17 16:56:59', '2019-01-17 20:18:48', 1, 0, 'webApp');
|
INSERT INTO `sys_menu` VALUES (10, 37, '文档中心', '#!swagger', 'http://127.0.0.1:9900/doc.html', NULL, 'layui-icon-app', 4, '2017-11-17 16:56:59', '2019-01-17 20:18:48', 1, 0, 'webApp');
|
||||||
INSERT INTO `sys_menu` VALUES (11, 12, '我的信息', '#!myInfo', 'system/myInfo.html', NULL, 'layui-icon-login-qq', 10, '2017-11-17 16:56:59', '2018-09-02 06:12:24', 1, 0, 'webApp');
|
INSERT INTO `sys_menu` VALUES (11, 12, '我的信息', '#!myInfo', 'system/myInfo.html', NULL, 'layui-icon-login-qq', 10, '2017-11-17 16:56:59', '2018-09-02 06:12:24', 1, 0, 'webApp');
|
||||||
INSERT INTO `sys_menu` VALUES (12, -1, '认证管理', 'javascript:;', '', NULL, 'layui-icon-set', 1, '2017-11-17 16:56:59', '2018-12-13 15:02:49', 1, 0, 'webApp');
|
INSERT INTO `sys_menu` VALUES (12, -1, '认证管理', 'javascript:;', '', NULL, 'layui-icon-set', 1, '2017-11-17 16:56:59', '2018-12-13 15:02:49', 1, 0, 'webApp');
|
||||||
INSERT INTO `sys_menu` VALUES (35, 12, '应用管理', '#!app', 'attestation/app.html', NULL, 'layui-icon-link', 5, '2017-11-17 16:56:59', '2019-01-14 15:35:15', 1, 0, 'webApp');
|
INSERT INTO `sys_menu` VALUES (35, 12, '应用管理', '#!app', 'attestation/app.html', NULL, 'layui-icon-link', 5, '2017-11-17 16:56:59', '2019-01-14 15:35:15', 1, 0, 'webApp');
|
||||||
|
|||||||
+15
-7
@@ -1,6 +1,8 @@
|
|||||||
package com.central.oauth.service.impl;
|
package com.central.oauth.service.impl;
|
||||||
|
|
||||||
|
import com.central.common.redis.template.RedisRepository;
|
||||||
import org.springframework.data.redis.core.RedisTemplate;
|
import org.springframework.data.redis.core.RedisTemplate;
|
||||||
|
import org.springframework.data.redis.serializer.RedisSerializer;
|
||||||
import org.springframework.security.oauth2.provider.OAuth2Authentication;
|
import org.springframework.security.oauth2.provider.OAuth2Authentication;
|
||||||
import org.springframework.security.oauth2.provider.code.RandomValueAuthorizationCodeServices;
|
import org.springframework.security.oauth2.provider.code.RandomValueAuthorizationCodeServices;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
@@ -8,15 +10,21 @@ import org.springframework.stereotype.Service;
|
|||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author zlt
|
|
||||||
* JdbcAuthorizationCodeServices替换
|
* JdbcAuthorizationCodeServices替换
|
||||||
|
*
|
||||||
|
* @author zlt
|
||||||
|
* <p>
|
||||||
|
* Blog: https://zlt2000.gitee.io
|
||||||
|
* Github: https://github.com/zlt2000
|
||||||
*/
|
*/
|
||||||
@Service
|
@Service
|
||||||
public class RedisAuthorizationCodeServices extends RandomValueAuthorizationCodeServices {
|
public class RedisAuthorizationCodeServices extends RandomValueAuthorizationCodeServices {
|
||||||
private RedisTemplate<String, Object> redisTemplate;
|
private final RedisRepository redisRepository;
|
||||||
|
private final RedisSerializer<Object> valueSerializer;
|
||||||
|
|
||||||
public RedisAuthorizationCodeServices(RedisTemplate<String, Object> redisTemplate) {
|
public RedisAuthorizationCodeServices(RedisRepository redisRepository) {
|
||||||
this.redisTemplate = redisTemplate;
|
this.redisRepository = redisRepository;
|
||||||
|
this.valueSerializer = RedisSerializer.java();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -25,14 +33,14 @@ public class RedisAuthorizationCodeServices extends RandomValueAuthorizationCode
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
protected void store(String code, OAuth2Authentication authentication) {
|
protected void store(String code, OAuth2Authentication authentication) {
|
||||||
redisTemplate.opsForValue().set(redisKey(code), authentication, 10, TimeUnit.MINUTES);
|
redisRepository.setExpire(redisKey(code), authentication, 10, TimeUnit.MINUTES, valueSerializer);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected OAuth2Authentication remove(final String code) {
|
protected OAuth2Authentication remove(final String code) {
|
||||||
String codeKey = redisKey(code);
|
String codeKey = redisKey(code);
|
||||||
OAuth2Authentication token = (OAuth2Authentication) redisTemplate.opsForValue().get(codeKey);
|
OAuth2Authentication token = (OAuth2Authentication) redisRepository.get(codeKey, valueSerializer);
|
||||||
this.redisTemplate.delete(codeKey);
|
redisRepository.del(codeKey);
|
||||||
return token;
|
return token;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -25,6 +25,9 @@ import java.util.Map;
|
|||||||
*
|
*
|
||||||
* @author zlt
|
* @author zlt
|
||||||
* @date 2019/7/12
|
* @date 2019/7/12
|
||||||
|
* <p>
|
||||||
|
* Blog: https://zlt2000.gitee.io
|
||||||
|
* Github: https://github.com/zlt2000
|
||||||
*/
|
*/
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Service
|
@Service
|
||||||
@@ -43,7 +46,6 @@ public class RedisTokensServiceImpl implements ITokensService {
|
|||||||
List<TokenVo> result = new ArrayList<>(limit);
|
List<TokenVo> result = new ArrayList<>(limit);
|
||||||
RedisSerializer<Object> valueSerializer = RedisSerializer.java();
|
RedisSerializer<Object> valueSerializer = RedisSerializer.java();
|
||||||
//查询token集合
|
//查询token集合
|
||||||
//redisRepository.getRedisTemplate().e
|
|
||||||
List<Object> tokenObjs = redisRepository.getList(redisKey, startEnds[0], startEnds[1]-1, valueSerializer);
|
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) {
|
||||||
|
|||||||
Reference in New Issue
Block a user