From ae84064c95446c4a91b4cf30f31be881b929d93b Mon Sep 17 00:00:00 2001 From: fengshuonan Date: Tue, 8 Nov 2022 21:39:24 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=907.3.2=E3=80=91=E3=80=90security?= =?UTF-8?q?=E3=80=91=E6=9B=B4=E6=96=B0security=E6=A8=A1=E5=9D=97=E7=9A=84?= =?UTF-8?q?=E7=BC=93=E5=AD=98=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../starter/CounterAutoConfiguration.java | 58 +++++++++---------- .../SecurityMemoryCacheAutoConfiguration.java | 40 +++++++++++++ .../SecurityRedisCacheAutoConfiguration.java | 39 +++++++++++++ 3 files changed, 108 insertions(+), 29 deletions(-) diff --git a/kernel-d-security/security-spring-boot-starter/src/main/java/cn/stylefeng/roses/kernel/security/starter/CounterAutoConfiguration.java b/kernel-d-security/security-spring-boot-starter/src/main/java/cn/stylefeng/roses/kernel/security/starter/CounterAutoConfiguration.java index a40298637..fcdd76071 100644 --- a/kernel-d-security/security-spring-boot-starter/src/main/java/cn/stylefeng/roses/kernel/security/starter/CounterAutoConfiguration.java +++ b/kernel-d-security/security-spring-boot-starter/src/main/java/cn/stylefeng/roses/kernel/security/starter/CounterAutoConfiguration.java @@ -24,22 +24,21 @@ */ 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.constants.CacheConstants; +import cn.stylefeng.roses.kernel.cache.api.CacheOperatorApi; import cn.stylefeng.roses.kernel.security.api.BlackListApi; import cn.stylefeng.roses.kernel.security.api.CountValidatorApi; import cn.stylefeng.roses.kernel.security.api.WhiteListApi; import cn.stylefeng.roses.kernel.security.blackwhite.BlackListService; import cn.stylefeng.roses.kernel.security.blackwhite.WhiteListService; -import cn.stylefeng.roses.kernel.security.blackwhite.cache.BlackListMemoryCache; -import cn.stylefeng.roses.kernel.security.blackwhite.cache.WhiteListMemoryCache; import cn.stylefeng.roses.kernel.security.count.DefaultCountValidator; -import cn.stylefeng.roses.kernel.security.count.cache.DefaultCountValidateCache; -import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import cn.stylefeng.roses.kernel.security.starter.cache.SecurityMemoryCacheAutoConfiguration; +import cn.stylefeng.roses.kernel.security.starter.cache.SecurityRedisCacheAutoConfiguration; +import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import javax.annotation.Resource; + /** * 计数器和黑白名单自动配置 @@ -48,8 +47,18 @@ import org.springframework.context.annotation.Configuration; * @date 2020/12/1 21:44 */ @Configuration +@AutoConfigureAfter({SecurityMemoryCacheAutoConfiguration.class, SecurityRedisCacheAutoConfiguration.class}) public class CounterAutoConfiguration { + @Resource(name = "blackListCache") + private CacheOperatorApi blackListCache; + + @Resource(name = "whiteListCache") + private CacheOperatorApi whiteListCache; + + @Resource(name = "countValidateCache") + private CacheOperatorApi countValidateCache; + /** * 黑名单校验 * @@ -57,25 +66,8 @@ public class CounterAutoConfiguration { * @date 2020/12/1 21:18 */ @Bean - @ConditionalOnMissingBean(BlackListApi.class) public BlackListApi blackListApi() { - TimedCache timedCache = CacheUtil.newTimedCache(CacheConstants.NONE_EXPIRED_TIME); - BlackListMemoryCache blackListMemoryCache = new BlackListMemoryCache(timedCache); - return new BlackListService(blackListMemoryCache); - } - - /** - * 计数校验器 - * - * @author fengshuonan - * @date 2020/12/1 21:18 - */ - @Bean - @ConditionalOnMissingBean(CountValidatorApi.class) - public CountValidatorApi countValidatorApi() { - TimedCache timedCache = CacheUtil.newTimedCache(CacheConstants.NONE_EXPIRED_TIME); - DefaultCountValidateCache defaultCountValidateCache = new DefaultCountValidateCache(timedCache); - return new DefaultCountValidator(defaultCountValidateCache); + return new BlackListService(blackListCache); } /** @@ -85,11 +77,19 @@ public class CounterAutoConfiguration { * @date 2020/12/1 21:18 */ @Bean - @ConditionalOnMissingBean(WhiteListApi.class) public WhiteListApi whiteListApi() { - TimedCache timedCache = CacheUtil.newTimedCache(CacheConstants.NONE_EXPIRED_TIME); - WhiteListMemoryCache whiteListMemoryCache = new WhiteListMemoryCache(timedCache); - return new WhiteListService(whiteListMemoryCache); + return new WhiteListService(whiteListCache); + } + + /** + * 计数校验器 + * + * @author fengshuonan + * @date 2020/12/1 21:18 + */ + @Bean + public CountValidatorApi countValidatorApi() { + return new DefaultCountValidator(countValidateCache); } } diff --git a/kernel-d-security/security-spring-boot-starter/src/main/java/cn/stylefeng/roses/kernel/security/starter/cache/SecurityMemoryCacheAutoConfiguration.java b/kernel-d-security/security-spring-boot-starter/src/main/java/cn/stylefeng/roses/kernel/security/starter/cache/SecurityMemoryCacheAutoConfiguration.java index 23714a28b..32f95898b 100644 --- a/kernel-d-security/security-spring-boot-starter/src/main/java/cn/stylefeng/roses/kernel/security/starter/cache/SecurityMemoryCacheAutoConfiguration.java +++ b/kernel-d-security/security-spring-boot-starter/src/main/java/cn/stylefeng/roses/kernel/security/starter/cache/SecurityMemoryCacheAutoConfiguration.java @@ -4,7 +4,11 @@ package cn.stylefeng.roses.kernel.security.starter.cache; 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.api.constants.CacheConstants; +import cn.stylefeng.roses.kernel.security.blackwhite.cache.BlackListMemoryCache; +import cn.stylefeng.roses.kernel.security.blackwhite.cache.WhiteListMemoryCache; import cn.stylefeng.roses.kernel.security.captcha.cache.CaptchaMemoryCache; +import cn.stylefeng.roses.kernel.security.count.cache.CountValidateMemoryCache; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -32,4 +36,40 @@ public class SecurityMemoryCacheAutoConfiguration { return new CaptchaMemoryCache(timedCache); } + /** + * 黑名单的缓存 + * + * @author fengshuonan + * @date 2022/11/8 21:24 + */ + @Bean("blackListCache") + public CacheOperatorApi blackListMemoryCache() { + TimedCache timedCache = CacheUtil.newTimedCache(CacheConstants.NONE_EXPIRED_TIME); + return new BlackListMemoryCache(timedCache); + } + + /** + * 白名单的缓存 + * + * @author fengshuonan + * @date 2022/11/8 21:24 + */ + @Bean("whiteListCache") + public CacheOperatorApi whiteListMemoryCache() { + TimedCache timedCache = CacheUtil.newTimedCache(CacheConstants.NONE_EXPIRED_TIME); + return new WhiteListMemoryCache(timedCache); + } + + /** + * 计数缓存 + * + * @author fengshuonan + * @date 2022/11/8 21:24 + */ + @Bean("countValidateCache") + public CacheOperatorApi countValidateMemoryCache() { + TimedCache timedCache = CacheUtil.newTimedCache(CacheConstants.NONE_EXPIRED_TIME); + return new CountValidateMemoryCache(timedCache); + } + } diff --git a/kernel-d-security/security-spring-boot-starter/src/main/java/cn/stylefeng/roses/kernel/security/starter/cache/SecurityRedisCacheAutoConfiguration.java b/kernel-d-security/security-spring-boot-starter/src/main/java/cn/stylefeng/roses/kernel/security/starter/cache/SecurityRedisCacheAutoConfiguration.java index 6ca8c5011..daea13ef5 100644 --- a/kernel-d-security/security-spring-boot-starter/src/main/java/cn/stylefeng/roses/kernel/security/starter/cache/SecurityRedisCacheAutoConfiguration.java +++ b/kernel-d-security/security-spring-boot-starter/src/main/java/cn/stylefeng/roses/kernel/security/starter/cache/SecurityRedisCacheAutoConfiguration.java @@ -3,7 +3,10 @@ package cn.stylefeng.roses.kernel.security.starter.cache; import cn.stylefeng.roses.kernel.cache.api.CacheOperatorApi; import cn.stylefeng.roses.kernel.cache.redis.util.CreateRedisTemplateUtil; +import cn.stylefeng.roses.kernel.security.blackwhite.cache.BlackListRedisCache; +import cn.stylefeng.roses.kernel.security.blackwhite.cache.WhiteListRedisCache; import cn.stylefeng.roses.kernel.security.captcha.cache.CaptchaRedisCache; +import cn.stylefeng.roses.kernel.security.count.cache.CountValidateRedisCache; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -33,4 +36,40 @@ public class SecurityRedisCacheAutoConfiguration { return new CaptchaRedisCache(redisTemplate); } + /** + * 黑名单的缓存,Redis缓存 + * + * @author fengshuonan + * @date 2022/11/8 21:24 + */ + @Bean("blackListCache") + public CacheOperatorApi blackListRedisCache(RedisConnectionFactory redisConnectionFactory) { + RedisTemplate redisTemplate = CreateRedisTemplateUtil.createString(redisConnectionFactory); + return new BlackListRedisCache(redisTemplate); + } + + /** + * 白名单的缓存,Redis缓存 + * + * @author fengshuonan + * @date 2022/11/8 21:24 + */ + @Bean("whiteListCache") + public CacheOperatorApi whiteListRedisCache(RedisConnectionFactory redisConnectionFactory) { + RedisTemplate redisTemplate = CreateRedisTemplateUtil.createString(redisConnectionFactory); + return new WhiteListRedisCache(redisTemplate); + } + + /** + * 计数缓存,Redis缓存 + * + * @author fengshuonan + * @date 2022/11/8 21:24 + */ + @Bean("countValidateCache") + public CacheOperatorApi countValidateRedisCache(RedisConnectionFactory redisConnectionFactory) { + RedisTemplate redisTemplate = CreateRedisTemplateUtil.createObject(redisConnectionFactory); + return new CountValidateRedisCache(redisTemplate); + } + }