From bcd9f91152de0c2a0e8aed6744f4153760230689 Mon Sep 17 00:00:00 2001 From: Charles7c Date: Sun, 26 Nov 2023 11:43:19 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E4=BC=98=E5=8C=96=20SaToken=20?= =?UTF-8?q?=E8=87=AA=E5=8A=A8=E9=85=8D=E7=BD=AE=EF=BC=88=E8=AE=A4=E8=AF=81?= =?UTF-8?q?=E6=A8=A1=E5=9D=97=EF=BC=89=EF=BC=8C=E6=96=B0=E5=A2=9E=E6=8C=81?= =?UTF-8?q?=E4=B9=85=E5=B1=82=20Redis=20=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../continew-starter-auth-justauth/pom.xml | 6 - .../SaTokenAutoConfiguration.java | 37 +++-- .../SaTokenDaoConfiguration.java | 75 +++++++++ .../SaTokenExtensionProperties.java | 26 ++- .../satoken/enums/SaTokenDaoTypeEnum.java | 36 +++++ .../satoken/impl/SaTokenDaoRedisImpl.java | 152 ++++++++++++++++++ .../properties/SaTokenDaoProperties.java | 41 +++++ .../properties/SaTokenSecurityProperties.java | 34 ++++ continew-starter-auth/pom.xml | 4 +- 9 files changed, 367 insertions(+), 44 deletions(-) create mode 100644 continew-starter-auth/continew-starter-auth-satoken/src/main/java/top/charles7c/continew/starter/auth/satoken/autoconfigure/SaTokenDaoConfiguration.java create mode 100644 continew-starter-auth/continew-starter-auth-satoken/src/main/java/top/charles7c/continew/starter/auth/satoken/enums/SaTokenDaoTypeEnum.java create mode 100644 continew-starter-auth/continew-starter-auth-satoken/src/main/java/top/charles7c/continew/starter/auth/satoken/impl/SaTokenDaoRedisImpl.java create mode 100644 continew-starter-auth/continew-starter-auth-satoken/src/main/java/top/charles7c/continew/starter/auth/satoken/properties/SaTokenDaoProperties.java create mode 100644 continew-starter-auth/continew-starter-auth-satoken/src/main/java/top/charles7c/continew/starter/auth/satoken/properties/SaTokenSecurityProperties.java diff --git a/continew-starter-auth/continew-starter-auth-justauth/pom.xml b/continew-starter-auth/continew-starter-auth-justauth/pom.xml index 6554e264..619e538d 100644 --- a/continew-starter-auth/continew-starter-auth-justauth/pom.xml +++ b/continew-starter-auth/continew-starter-auth-justauth/pom.xml @@ -26,11 +26,5 @@ com.xkcoding.justauth justauth-spring-boot-starter - - - - top.charles7c.continew - continew-starter-cache-redisson - \ No newline at end of file diff --git a/continew-starter-auth/continew-starter-auth-satoken/src/main/java/top/charles7c/continew/starter/auth/satoken/autoconfigure/SaTokenAutoConfiguration.java b/continew-starter-auth/continew-starter-auth-satoken/src/main/java/top/charles7c/continew/starter/auth/satoken/autoconfigure/SaTokenAutoConfiguration.java index 4d990c9f..255915c5 100644 --- a/continew-starter-auth/continew-starter-auth-satoken/src/main/java/top/charles7c/continew/starter/auth/satoken/autoconfigure/SaTokenAutoConfiguration.java +++ b/continew-starter-auth/continew-starter-auth-satoken/src/main/java/top/charles7c/continew/starter/auth/satoken/autoconfigure/SaTokenAutoConfiguration.java @@ -16,7 +16,6 @@ package top.charles7c.continew.starter.auth.satoken.autoconfigure; -import cn.dev33.satoken.dao.SaTokenDao; import cn.dev33.satoken.interceptor.SaInterceptor; import cn.dev33.satoken.jwt.StpLogicJwtForSimple; import cn.dev33.satoken.stp.StpInterface; @@ -31,6 +30,8 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; import org.springframework.context.annotation.PropertySource; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @@ -60,24 +61,7 @@ public class SaTokenAutoConfiguration implements WebMvcConfigurer { } /** - * 整合 JWT(简单模式) - */ - @Bean - public StpLogic stpLogic() { - return new StpLogicJwtForSimple(); - } - - /** - * 自定义缓存实现 - */ - @Bean - @ConditionalOnMissingBean - public SaTokenDao saTokenDao() { - return ReflectUtil.newInstance(properties.getDaoImpl()); - } - - /** - * 权限认证实现 + * 权限认证实现类 */ @Bean @ConditionalOnMissingBean @@ -85,6 +69,21 @@ public class SaTokenAutoConfiguration implements WebMvcConfigurer { return ReflectUtil.newInstance(properties.getPermissionImpl()); } + /** + * 自定义持久层配置 + */ + @Configuration + @Import({SaTokenDaoConfiguration.Redis.class, SaTokenDaoConfiguration.Custom.class}) + protected static class SaTokenDaoAutoConfiguration {} + + /** + * 整合 JWT(简单模式) + */ + @Bean + public StpLogic stpLogic() { + return new StpLogicJwtForSimple(); + } + @PostConstruct public void postConstruct() { log.info("[ContiNew Starter] - Auto Configuration 'SaToken' completed initialization."); diff --git a/continew-starter-auth/continew-starter-auth-satoken/src/main/java/top/charles7c/continew/starter/auth/satoken/autoconfigure/SaTokenDaoConfiguration.java b/continew-starter-auth/continew-starter-auth-satoken/src/main/java/top/charles7c/continew/starter/auth/satoken/autoconfigure/SaTokenDaoConfiguration.java new file mode 100644 index 00000000..aac7b864 --- /dev/null +++ b/continew-starter-auth/continew-starter-auth-satoken/src/main/java/top/charles7c/continew/starter/auth/satoken/autoconfigure/SaTokenDaoConfiguration.java @@ -0,0 +1,75 @@ +/* + * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. + *

+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.gnu.org/licenses/lgpl.html + *

+ * 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.continew.starter.auth.satoken.autoconfigure; + +import cn.dev33.satoken.dao.SaTokenDao; +import cn.hutool.core.util.ReflectUtil; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.redisson.client.RedisClient; +import org.springframework.boot.autoconfigure.AutoConfigureBefore; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.context.annotation.Bean; +import top.charles7c.continew.starter.auth.satoken.impl.SaTokenDaoRedisImpl; +import top.charles7c.continew.starter.cache.redisson.autoconfigure.RedissonAutoConfiguration; + +/** + * SaToken 持久层配置 + * + * @author Charles7c + * @since 1.0.0 + */ +@Slf4j +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public abstract class SaTokenDaoConfiguration { + + /** + * 自定义持久层实现类-Redis + */ + @ConditionalOnClass(RedisClient.class) + @ConditionalOnMissingBean(SaTokenDao.class) + @AutoConfigureBefore(RedissonAutoConfiguration.class) + @ConditionalOnProperty(name = "sa-token.extension.dao.type", havingValue = "redis") + static class Redis { + static { + log.debug("[ContiNew Starter] - Auto Configuration 'SaToken-SaTokenDao-Redis' completed initialization."); + } + + @Bean + public SaTokenDao saTokenDao() { + return new SaTokenDaoRedisImpl(); + } + } + + /** + * 自定义持久层实现类-自定义 + */ + @ConditionalOnProperty(name = "sa-token.extension.dao.type", havingValue = "custom") + static class Custom { + static { + log.debug("[ContiNew Starter] - Auto Configuration 'SaToken-SaTokenDao-Custom' completed initialization."); + } + + @Bean + public SaTokenDao saTokenDao(SaTokenExtensionProperties properties) { + return ReflectUtil.newInstance(properties.getDao().getImpl()); + } + } +} \ No newline at end of file diff --git a/continew-starter-auth/continew-starter-auth-satoken/src/main/java/top/charles7c/continew/starter/auth/satoken/autoconfigure/SaTokenExtensionProperties.java b/continew-starter-auth/continew-starter-auth-satoken/src/main/java/top/charles7c/continew/starter/auth/satoken/autoconfigure/SaTokenExtensionProperties.java index 312e639f..9c4894a5 100644 --- a/continew-starter-auth/continew-starter-auth-satoken/src/main/java/top/charles7c/continew/starter/auth/satoken/autoconfigure/SaTokenExtensionProperties.java +++ b/continew-starter-auth/continew-starter-auth-satoken/src/main/java/top/charles7c/continew/starter/auth/satoken/autoconfigure/SaTokenExtensionProperties.java @@ -16,10 +16,12 @@ package top.charles7c.continew.starter.auth.satoken.autoconfigure; -import cn.dev33.satoken.dao.SaTokenDao; import cn.dev33.satoken.stp.StpInterface; import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.boot.context.properties.NestedConfigurationProperty; +import top.charles7c.continew.starter.auth.satoken.properties.SaTokenDaoProperties; +import top.charles7c.continew.starter.auth.satoken.properties.SaTokenSecurityProperties; /** @@ -37,30 +39,20 @@ public class SaTokenExtensionProperties { */ private boolean enabled = false; - /** - * 自定义缓存实现 - */ - private Class daoImpl; - /** * 权限认证实现 */ private Class permissionImpl; /** - * 安全配置 + * 持久层配置 */ - private SecurityProperties security; + @NestedConfigurationProperty + private SaTokenDaoProperties dao; /** - * 安全配置属性 + * 安全配置 */ - @Data - public static class SecurityProperties { - - /** - * 排除(放行)路径配置 - */ - private String[] excludes = new String[0]; - } + @NestedConfigurationProperty + private SaTokenSecurityProperties security; } diff --git a/continew-starter-auth/continew-starter-auth-satoken/src/main/java/top/charles7c/continew/starter/auth/satoken/enums/SaTokenDaoTypeEnum.java b/continew-starter-auth/continew-starter-auth-satoken/src/main/java/top/charles7c/continew/starter/auth/satoken/enums/SaTokenDaoTypeEnum.java new file mode 100644 index 00000000..cb1e998c --- /dev/null +++ b/continew-starter-auth/continew-starter-auth-satoken/src/main/java/top/charles7c/continew/starter/auth/satoken/enums/SaTokenDaoTypeEnum.java @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. + *

+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.gnu.org/licenses/lgpl.html + *

+ * 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.continew.starter.auth.satoken.enums; + +/** + * SaToken 持久层类型枚举 + * + * @author Charles7c + * @since 1.0.0 + */ +public enum SaTokenDaoTypeEnum { + + /** + * Redis + */ + REDIS, + + /** + * 自定义 + */ + CUSTOM +} diff --git a/continew-starter-auth/continew-starter-auth-satoken/src/main/java/top/charles7c/continew/starter/auth/satoken/impl/SaTokenDaoRedisImpl.java b/continew-starter-auth/continew-starter-auth-satoken/src/main/java/top/charles7c/continew/starter/auth/satoken/impl/SaTokenDaoRedisImpl.java new file mode 100644 index 00000000..9c49c3e1 --- /dev/null +++ b/continew-starter-auth/continew-starter-auth-satoken/src/main/java/top/charles7c/continew/starter/auth/satoken/impl/SaTokenDaoRedisImpl.java @@ -0,0 +1,152 @@ +/* + * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. + *

+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.gnu.org/licenses/lgpl.html + *

+ * 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.continew.starter.auth.satoken.impl; + +import cn.dev33.satoken.dao.SaTokenDao; +import cn.dev33.satoken.util.SaFoxUtil; +import top.charles7c.continew.starter.cache.redisson.util.RedisUtils; + +import java.time.Duration; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +/** + * Sa-Token 持久层 Redis 实现(参考:Sa-Token/sa-token-plugin/sa-token-dao-redisx/SaTokenDaoOfRedis.java) + * + * @author Charles7c + * @since 1.0.0 + */ +public class SaTokenDaoRedisImpl implements SaTokenDao { + + @Override + public String get(String key) { + return RedisUtils.get(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.set(key, value); + } else { + RedisUtils.set(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.delete(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.get(key); + } + + @Override + public void setObject(String key, Object object, long timeout) { + if (0 == timeout || timeout <= SaTokenDao.NOT_VALUE_EXPIRE) { + return; + } + // 判断是否为永不过期 + if (timeout == SaTokenDao.NEVER_EXPIRE) { + RedisUtils.set(key, object); + } else { + RedisUtils.set(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.delete(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(String.format("%s*%s*", prefix, keyword)); + List list = new ArrayList<>(keys); + return SaFoxUtil.searchList(list, start, size, sortType); + } +} diff --git a/continew-starter-auth/continew-starter-auth-satoken/src/main/java/top/charles7c/continew/starter/auth/satoken/properties/SaTokenDaoProperties.java b/continew-starter-auth/continew-starter-auth-satoken/src/main/java/top/charles7c/continew/starter/auth/satoken/properties/SaTokenDaoProperties.java new file mode 100644 index 00000000..533bcbdd --- /dev/null +++ b/continew-starter-auth/continew-starter-auth-satoken/src/main/java/top/charles7c/continew/starter/auth/satoken/properties/SaTokenDaoProperties.java @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. + *

+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.gnu.org/licenses/lgpl.html + *

+ * 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.continew.starter.auth.satoken.properties; + +import cn.dev33.satoken.dao.SaTokenDao; +import lombok.Data; +import top.charles7c.continew.starter.auth.satoken.enums.SaTokenDaoTypeEnum; + +/** + * SaToken 持久层配置属性 + * + * @author Charles7c + * @since 1.0.0 + */ +@Data +public class SaTokenDaoProperties { + + /** + * 持久层类型 + */ + private SaTokenDaoTypeEnum type; + + /** + * 自定义持久层实现类(当 type 为 CUSTOM 时必填) + */ + private Class impl; +} diff --git a/continew-starter-auth/continew-starter-auth-satoken/src/main/java/top/charles7c/continew/starter/auth/satoken/properties/SaTokenSecurityProperties.java b/continew-starter-auth/continew-starter-auth-satoken/src/main/java/top/charles7c/continew/starter/auth/satoken/properties/SaTokenSecurityProperties.java new file mode 100644 index 00000000..4e7002b9 --- /dev/null +++ b/continew-starter-auth/continew-starter-auth-satoken/src/main/java/top/charles7c/continew/starter/auth/satoken/properties/SaTokenSecurityProperties.java @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. + *

+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.gnu.org/licenses/lgpl.html + *

+ * 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.continew.starter.auth.satoken.properties; + +import lombok.Data; + +/** + * SaToken 安全配置属性 + * + * @author Charles7c + * @since 1.0.0 + */ +@Data +public class SaTokenSecurityProperties { + + /** + * 排除(放行)路径配置 + */ + private String[] excludes = new String[0]; +} \ No newline at end of file diff --git a/continew-starter-auth/pom.xml b/continew-starter-auth/pom.xml index 23de50c7..7735e69c 100644 --- a/continew-starter-auth/pom.xml +++ b/continew-starter-auth/pom.xml @@ -21,10 +21,10 @@ - + top.charles7c.continew - continew-starter-core + continew-starter-cache-redisson \ No newline at end of file