From c8b75419e53197ed9cb29635c4e8b699825afdf5 Mon Sep 17 00:00:00 2001 From: zlt2000 Date: Sun, 4 Oct 2020 01:26:30 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dredis=E5=BA=8F=E5=88=97?= =?UTF-8?q?=E5=8C=96=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../redis/template/RedisRepository.java | 48 ++++++++++++++++--- zlt-doc/sql/user-center.sql | 2 +- .../impl/RedisAuthorizationCodeServices.java | 22 ++++++--- .../service/impl/RedisTokensServiceImpl.java | 4 +- 4 files changed, 61 insertions(+), 15 deletions(-) diff --git a/zlt-commons/zlt-redis-spring-boot-starter/src/main/java/com/central/common/redis/template/RedisRepository.java b/zlt-commons/zlt-redis-spring-boot-starter/src/main/java/com/central/common/redis/template/RedisRepository.java index d083ece6..d6c1162d 100644 --- a/zlt-commons/zlt-redis-spring-boot-starter/src/main/java/com/central/common/redis/template/RedisRepository.java +++ b/zlt-commons/zlt-redis-spring-boot-starter/src/main/java/com/central/common/redis/template/RedisRepository.java @@ -1,13 +1,12 @@ package com.central.common.redis.template; import lombok.extern.slf4j.Slf4j; +import org.springframework.dao.DataAccessException; 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.RedisServerCommands; -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.core.*; import org.springframework.data.redis.serializer.RedisSerializer; import org.springframework.data.redis.serializer.SerializationUtils; import org.springframework.util.Assert; @@ -77,10 +76,40 @@ public class RedisRepository { * * @param key redis主键 * @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) { - 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 valueSerializer) { + byte[] rawKey = rawKey(key); + byte[] rawValue = rawValue(value, valueSerializer); + + redisTemplate.execute(new RedisCallback() { + @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 redisSerializer = (RedisSerializer)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 rawValues, RedisSerializer valueSerializer) { if (valueSerializer == null) { diff --git a/zlt-doc/sql/user-center.sql b/zlt-doc/sql/user-center.sql index f908c667..14dce545 100644 --- a/zlt-doc/sql/user-center.sql +++ b/zlt-doc/sql/user-center.sql @@ -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 (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 (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 (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'); diff --git a/zlt-uaa/src/main/java/com/central/oauth/service/impl/RedisAuthorizationCodeServices.java b/zlt-uaa/src/main/java/com/central/oauth/service/impl/RedisAuthorizationCodeServices.java index 4dd83f30..59a2409f 100644 --- a/zlt-uaa/src/main/java/com/central/oauth/service/impl/RedisAuthorizationCodeServices.java +++ b/zlt-uaa/src/main/java/com/central/oauth/service/impl/RedisAuthorizationCodeServices.java @@ -1,6 +1,8 @@ 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.serializer.RedisSerializer; import org.springframework.security.oauth2.provider.OAuth2Authentication; import org.springframework.security.oauth2.provider.code.RandomValueAuthorizationCodeServices; import org.springframework.stereotype.Service; @@ -8,15 +10,21 @@ import org.springframework.stereotype.Service; import java.util.concurrent.TimeUnit; /** - * @author zlt * JdbcAuthorizationCodeServices替换 + * + * @author zlt + *

+ * Blog: https://zlt2000.gitee.io + * Github: https://github.com/zlt2000 */ @Service public class RedisAuthorizationCodeServices extends RandomValueAuthorizationCodeServices { - private RedisTemplate redisTemplate; + private final RedisRepository redisRepository; + private final RedisSerializer valueSerializer; - public RedisAuthorizationCodeServices(RedisTemplate redisTemplate) { - this.redisTemplate = redisTemplate; + public RedisAuthorizationCodeServices(RedisRepository redisRepository) { + this.redisRepository = redisRepository; + this.valueSerializer = RedisSerializer.java(); } /** @@ -25,14 +33,14 @@ public class RedisAuthorizationCodeServices extends RandomValueAuthorizationCode */ @Override 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 protected OAuth2Authentication remove(final String code) { String codeKey = redisKey(code); - OAuth2Authentication token = (OAuth2Authentication) redisTemplate.opsForValue().get(codeKey); - this.redisTemplate.delete(codeKey); + OAuth2Authentication token = (OAuth2Authentication) redisRepository.get(codeKey, valueSerializer); + redisRepository.del(codeKey); return token; } diff --git a/zlt-uaa/src/main/java/com/central/oauth/service/impl/RedisTokensServiceImpl.java b/zlt-uaa/src/main/java/com/central/oauth/service/impl/RedisTokensServiceImpl.java index 9aa95426..3f65b800 100644 --- a/zlt-uaa/src/main/java/com/central/oauth/service/impl/RedisTokensServiceImpl.java +++ b/zlt-uaa/src/main/java/com/central/oauth/service/impl/RedisTokensServiceImpl.java @@ -25,6 +25,9 @@ import java.util.Map; * * @author zlt * @date 2019/7/12 + *

+ * Blog: https://zlt2000.gitee.io + * Github: https://github.com/zlt2000 */ @Slf4j @Service @@ -43,7 +46,6 @@ public class RedisTokensServiceImpl implements ITokensService { List result = new ArrayList<>(limit); RedisSerializer valueSerializer = RedisSerializer.java(); //查询token集合 - //redisRepository.getRedisTemplate().e List tokenObjs = redisRepository.getList(redisKey, startEnds[0], startEnds[1]-1, valueSerializer); if (tokenObjs != null) { for (Object obj : tokenObjs) {