From d9a48f7a952a00e87610a1ba5d173c747c92081c Mon Sep 17 00:00:00 2001 From: Charles7c Date: Wed, 28 Dec 2022 23:32:05 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=EF=BC=9A=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E7=94=9F=E4=BA=A7=E7=8E=AF=E5=A2=83=E6=8A=A5=20java.lang.NoCla?= =?UTF-8?q?ssDefFoundError:=20org/springframework/data/redis/connection/zs?= =?UTF-8?q?et/Tuple=20=E7=9A=84=E9=94=99=E8=AF=AF=E9=97=AE=E9=A2=98?= =?UTF-8?q?=EF=BC=88=E7=A7=BB=E9=99=A4=20Sa-Token=20=E9=9B=86=E6=88=90=20R?= =?UTF-8?q?edis=20=E4=BE=9D=E8=B5=96=EF=BC=8C=E6=94=B9=E4=B8=BA=E6=9C=AC?= =?UTF-8?q?=E5=9C=B0=20Redis=20=E9=80=82=E9=85=8D=20SaTokenDao=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- continew-admin-common/pom.xml | 6 - .../common/config/RedissonConfiguration.java | 50 ++++++ .../cnadmin/common/util/RedisUtils.java | 2 +- .../config/satoken/SaTokenRedisDaoImpl.java | 156 ++++++++++++++++++ pom.xml | 7 - 5 files changed, 207 insertions(+), 14 deletions(-) create mode 100644 continew-admin-common/src/main/java/top/charles7c/cnadmin/common/config/RedissonConfiguration.java create mode 100644 continew-admin-system/src/main/java/top/charles7c/cnadmin/auth/config/satoken/SaTokenRedisDaoImpl.java diff --git a/continew-admin-common/pom.xml b/continew-admin-common/pom.xml index ceb54cef..565242f9 100644 --- a/continew-admin-common/pom.xml +++ b/continew-admin-common/pom.xml @@ -78,12 +78,6 @@ limitations under the License. sa-token-jwt - - - cn.dev33 - sa-token-dao-redis-jackson - - diff --git a/continew-admin-common/src/main/java/top/charles7c/cnadmin/common/config/RedissonConfiguration.java b/continew-admin-common/src/main/java/top/charles7c/cnadmin/common/config/RedissonConfiguration.java new file mode 100644 index 00000000..e56a3f88 --- /dev/null +++ b/continew-admin-common/src/main/java/top/charles7c/cnadmin/common/config/RedissonConfiguration.java @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package top.charles7c.cnadmin.common.config; + +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; + +import org.redisson.codec.JsonJacksonCodec; +import org.redisson.spring.starter.RedissonAutoConfigurationCustomizer; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import com.fasterxml.jackson.databind.ObjectMapper; + +/** + * Redisson 配置 + * + * @author Charles7c + * @since 2022/12/28 23:17 + */ +@Slf4j +@Configuration +@RequiredArgsConstructor +public class RedissonConfiguration { + + private final ObjectMapper objectMapper; + + /** + * Redisson 自定义配置 + */ + @Bean + public RedissonAutoConfigurationCustomizer redissonCustomizer() { + // 解决序列化乱码问题 + return config -> config.setCodec(new JsonJacksonCodec(objectMapper)); + } +} diff --git a/continew-admin-common/src/main/java/top/charles7c/cnadmin/common/util/RedisUtils.java b/continew-admin-common/src/main/java/top/charles7c/cnadmin/common/util/RedisUtils.java index bdacad19..8e8cf5b6 100644 --- a/continew-admin-common/src/main/java/top/charles7c/cnadmin/common/util/RedisUtils.java +++ b/continew-admin-common/src/main/java/top/charles7c/cnadmin/common/util/RedisUtils.java @@ -71,7 +71,7 @@ public class RedisUtils { * * @param key * 缓存键 - * @return 剩余存活时间 + * @return 剩余存活时间(单位:毫秒) */ public static long getTimeToLive(final String key) { RBucket rBucket = REDISSON_CLIENT.getBucket(key); diff --git a/continew-admin-system/src/main/java/top/charles7c/cnadmin/auth/config/satoken/SaTokenRedisDaoImpl.java b/continew-admin-system/src/main/java/top/charles7c/cnadmin/auth/config/satoken/SaTokenRedisDaoImpl.java new file mode 100644 index 00000000..50f55743 --- /dev/null +++ b/continew-admin-system/src/main/java/top/charles7c/cnadmin/auth/config/satoken/SaTokenRedisDaoImpl.java @@ -0,0 +1,156 @@ +/* + * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package top.charles7c.cnadmin.auth.config.satoken; + +import java.time.Duration; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +import org.springframework.stereotype.Component; + +import cn.dev33.satoken.dao.SaTokenDao; +import cn.dev33.satoken.util.SaFoxUtil; + +import top.charles7c.cnadmin.common.util.RedisUtils; + +/** + * SaTokenDao 的本地 Redis 适配(参考:Sa-Token/sa-token-plugin/sa-token-dao-redisx/SaTokenDaoOfRedis.java) + * + * @author Charles7c + * @since 2022/12/28 22:55 + */ +@Component +public class SaTokenRedisDaoImpl implements SaTokenDao { + + @Override + public String get(String key) { + return RedisUtils.getCacheObject(key); + } + + @Override + public void set(String key, String value, long timeout) { + if (timeout == 0 || timeout <= SaTokenDao.NOT_VALUE_EXPIRE) { + return; + } + // 判断是否为永不过期 + if (timeout == SaTokenDao.NEVER_EXPIRE) { + RedisUtils.setCacheObject(key, value); + } else { + RedisUtils.setCacheObject(key, value, Duration.ofSeconds(timeout)); + } + } + + @Override + public void update(String key, String value) { + long expire = getTimeout(key); + // -2:无此键 + if (expire == SaTokenDao.NOT_VALUE_EXPIRE) { + return; + } + this.set(key, value, expire); + } + + @Override + public void delete(String key) { + RedisUtils.deleteCacheObject(key); + } + + @Override + public long getTimeout(String key) { + long timeout = RedisUtils.getTimeToLive(key); + return timeout < 0 ? timeout : timeout / 1000; + } + + @Override + public void updateTimeout(String key, long timeout) { + // 判断是否想要设置为永久 + if (timeout == SaTokenDao.NEVER_EXPIRE) { + long expire = getTimeout(key); + if (expire == SaTokenDao.NEVER_EXPIRE) { + // 如果其已经被设置为永久,则不作任何处理 + } else { + // 如果尚未被设置为永久,那么再次 set 一次 + this.set(key, this.get(key), timeout); + } + return; + } + RedisUtils.expire(key, Duration.ofSeconds(timeout)); + } + + @Override + public Object getObject(String key) { + return RedisUtils.getCacheObject(key); + } + + @Override + public void setObject(String key, Object object, long timeout) { + if (timeout == 0 || timeout <= SaTokenDao.NOT_VALUE_EXPIRE) { + return; + } + // 判断是否为永不过期 + if (timeout == SaTokenDao.NEVER_EXPIRE) { + RedisUtils.setCacheObject(key, object); + } else { + RedisUtils.setCacheObject(key, object, Duration.ofSeconds(timeout)); + } + } + + @Override + public void updateObject(String key, Object object) { + long expire = getObjectTimeout(key); + // -2:无此键 + if (expire == SaTokenDao.NOT_VALUE_EXPIRE) { + return; + } + this.setObject(key, object, expire); + } + + @Override + public void deleteObject(String key) { + RedisUtils.deleteCacheObject(key); + } + + @Override + public long getObjectTimeout(String key) { + long timeout = RedisUtils.getTimeToLive(key); + return timeout < 0 ? timeout : timeout / 1000; + } + + @Override + public void updateObjectTimeout(String key, long timeout) { + // 判断是否想要设置为永久 + if (timeout == SaTokenDao.NEVER_EXPIRE) { + long expire = getObjectTimeout(key); + if (expire == SaTokenDao.NEVER_EXPIRE) { + // 如果其已经被设置为永久,则不作任何处理 + } else { + // 如果尚未被设置为永久,那么再次 set 一次 + this.setObject(key, this.getObject(key), timeout); + } + return; + } + RedisUtils.expire(key, Duration.ofSeconds(timeout)); + } + + @Override + public List searchData(String prefix, String keyword, int start, int size, boolean sortType) { + Collection keys = RedisUtils.keys(prefix + "*" + keyword + "*"); + List list = new ArrayList<>(keys); + return SaFoxUtil.searchList(list, start, size, sortType); + } +} diff --git a/pom.xml b/pom.xml index 989f3919..6dc2a794 100644 --- a/pom.xml +++ b/pom.xml @@ -90,13 +90,6 @@ limitations under the License. - - - cn.dev33 - sa-token-dao-redis-jackson - ${sa-token.version} - -