From 5888956cba01fcfc15618f20ae5399c9b0667599 Mon Sep 17 00:00:00 2001 From: fengshuonan Date: Tue, 8 Nov 2022 20:54:33 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=907.3.2=E3=80=91=E3=80=90cache=E3=80=91?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E9=AA=8C=E8=AF=81=E7=A0=81=E7=BC=93=E5=AD=98?= =?UTF-8?q?=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../security-spring-boot-starter/pom.xml | 8 +++ .../starter/CaptchaAutoConfiguration.java | 19 +++---- .../SecurityCacheAutoConfiguration.java | 54 +++++++++++++++++++ .../main/resources/META-INF/spring.factories | 3 +- 4 files changed, 74 insertions(+), 10 deletions(-) create mode 100644 kernel-d-security/security-spring-boot-starter/src/main/java/cn/stylefeng/roses/kernel/security/starter/SecurityCacheAutoConfiguration.java diff --git a/kernel-d-security/security-spring-boot-starter/pom.xml b/kernel-d-security/security-spring-boot-starter/pom.xml index b5b0fb484..f0ddf73e2 100644 --- a/kernel-d-security/security-spring-boot-starter/pom.xml +++ b/kernel-d-security/security-spring-boot-starter/pom.xml @@ -73,6 +73,14 @@ ${roses.version} + + + cn.stylefeng.roses + cache-sdk-redis + ${roses.version} + true + + diff --git a/kernel-d-security/security-spring-boot-starter/src/main/java/cn/stylefeng/roses/kernel/security/starter/CaptchaAutoConfiguration.java b/kernel-d-security/security-spring-boot-starter/src/main/java/cn/stylefeng/roses/kernel/security/starter/CaptchaAutoConfiguration.java index 68f939f41..33f60110b 100644 --- a/kernel-d-security/security-spring-boot-starter/src/main/java/cn/stylefeng/roses/kernel/security/starter/CaptchaAutoConfiguration.java +++ b/kernel-d-security/security-spring-boot-starter/src/main/java/cn/stylefeng/roses/kernel/security/starter/CaptchaAutoConfiguration.java @@ -24,17 +24,18 @@ */ package cn.stylefeng.roses.kernel.security.starter; -import cn.hutool.cache.CacheUtil; -import cn.hutool.cache.impl.TimedCache; +import cn.stylefeng.roses.kernel.cache.api.CacheOperatorApi; import cn.stylefeng.roses.kernel.security.api.DragCaptchaApi; import cn.stylefeng.roses.kernel.security.api.ImageCaptchaApi; import cn.stylefeng.roses.kernel.security.captcha.DragCaptchaService; import cn.stylefeng.roses.kernel.security.captcha.ImageCaptchaService; -import cn.stylefeng.roses.kernel.security.captcha.cache.CaptchaMemoryCache; +import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import javax.annotation.Resource; + /** * 图形验证码自动配置 * @@ -42,8 +43,12 @@ import org.springframework.context.annotation.Configuration; * @date 2020/12/1 21:44 */ @Configuration +@AutoConfigureAfter(SecurityCacheAutoConfiguration.class) public class CaptchaAutoConfiguration { + @Resource(name = "captchaCache") + private CacheOperatorApi captchaCache; + /** * 图形验证码 * @@ -54,9 +59,7 @@ public class CaptchaAutoConfiguration { @ConditionalOnMissingBean(ImageCaptchaApi.class) public ImageCaptchaApi captchaApi() { // 验证码过期时间 120秒 - TimedCache timedCache = CacheUtil.newTimedCache(1000 * 120); - CaptchaMemoryCache captchaMemoryCache = new CaptchaMemoryCache(timedCache); - return new ImageCaptchaService(captchaMemoryCache); + return new ImageCaptchaService(captchaCache); } /** @@ -69,9 +72,7 @@ public class CaptchaAutoConfiguration { @ConditionalOnMissingBean(DragCaptchaApi.class) public DragCaptchaApi dragCaptchaService() { // 验证码过期时间 120秒 - TimedCache timedCache = CacheUtil.newTimedCache(1000 * 120); - CaptchaMemoryCache captchaMemoryCache = new CaptchaMemoryCache(timedCache); - return new DragCaptchaService(captchaMemoryCache); + return new DragCaptchaService(captchaCache); } } diff --git a/kernel-d-security/security-spring-boot-starter/src/main/java/cn/stylefeng/roses/kernel/security/starter/SecurityCacheAutoConfiguration.java b/kernel-d-security/security-spring-boot-starter/src/main/java/cn/stylefeng/roses/kernel/security/starter/SecurityCacheAutoConfiguration.java new file mode 100644 index 000000000..7fee7b985 --- /dev/null +++ b/kernel-d-security/security-spring-boot-starter/src/main/java/cn/stylefeng/roses/kernel/security/starter/SecurityCacheAutoConfiguration.java @@ -0,0 +1,54 @@ +package cn.stylefeng.roses.kernel.security.starter; + + +import cn.hutool.cache.CacheUtil; +import cn.hutool.cache.impl.TimedCache; +import cn.stylefeng.roses.kernel.cache.api.CacheOperatorApi; +import cn.stylefeng.roses.kernel.cache.redis.util.CreateRedisTemplateUtil; +import cn.stylefeng.roses.kernel.security.captcha.cache.CaptchaMemoryCache; +import cn.stylefeng.roses.kernel.security.captcha.cache.CaptchaRedisCache; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.redis.connection.RedisConnectionFactory; +import org.springframework.data.redis.core.RedisTemplate; + +/** + * 安全模块,缓存的依赖 + * + * @author fengshuonan + * @date 2022/11/8 9:57 + */ +@Configuration +public class SecurityCacheAutoConfiguration { + + /** + * 验证码相关的缓存,内存缓存 + * + * @author fengshuonan + * @date 2022/11/8 20:44 + */ + @Bean("captchaCache") + @ConditionalOnMissingClass("org.springframework.data.redis.core.RedisTemplate") + public CacheOperatorApi captchaMemoryCache() { + // 验证码过期时间 120秒 + TimedCache timedCache = CacheUtil.newTimedCache(1000 * 120); + return new CaptchaMemoryCache(timedCache); + } + + /** + * 验证码相关的缓存,Redis缓存 + * + * @author fengshuonan + * @date 2022/11/8 20:44 + */ + @Bean("captchaCache") + @ConditionalOnClass(name = "org.springframework.data.redis.core.RedisTemplate") + public CacheOperatorApi captchaRedisCache(RedisConnectionFactory redisConnectionFactory) { + // 验证码过期时间 120秒 + RedisTemplate redisTemplate = CreateRedisTemplateUtil.createString(redisConnectionFactory); + return new CaptchaRedisCache(redisTemplate); + } + +} diff --git a/kernel-d-security/security-spring-boot-starter/src/main/resources/META-INF/spring.factories b/kernel-d-security/security-spring-boot-starter/src/main/resources/META-INF/spring.factories index 8e7e1e4fb..429baf5e1 100644 --- a/kernel-d-security/security-spring-boot-starter/src/main/resources/META-INF/spring.factories +++ b/kernel-d-security/security-spring-boot-starter/src/main/resources/META-INF/spring.factories @@ -2,4 +2,5 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ cn.stylefeng.roses.kernel.security.starter.GunsSecurityAutoConfiguration,\ cn.stylefeng.roses.kernel.security.starter.GunsXssAutoConfiguration,\ cn.stylefeng.roses.kernel.security.starter.CounterAutoConfiguration,\ - cn.stylefeng.roses.kernel.security.starter.CaptchaAutoConfiguration + cn.stylefeng.roses.kernel.security.starter.CaptchaAutoConfiguration,\ + cn.stylefeng.roses.kernel.security.starter.SecurityCacheAutoConfiguration