【7.3.2】【security】更新security模块的缓存配置

pull/41/head
fengshuonan 2022-11-08 21:39:24 +08:00
parent 26ca14cb97
commit ae84064c95
3 changed files with 108 additions and 29 deletions

View File

@ -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<String> blackListCache;
@Resource(name = "whiteListCache")
private CacheOperatorApi<String> whiteListCache;
@Resource(name = "countValidateCache")
private CacheOperatorApi<Long> countValidateCache;
/**
*
*
@ -57,25 +66,8 @@ public class CounterAutoConfiguration {
* @date 2020/12/1 21:18
*/
@Bean
@ConditionalOnMissingBean(BlackListApi.class)
public BlackListApi blackListApi() {
TimedCache<String, String> 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<String, Long> 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<String, String> 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);
}
}

View File

@ -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<String> blackListMemoryCache() {
TimedCache<String, String> timedCache = CacheUtil.newTimedCache(CacheConstants.NONE_EXPIRED_TIME);
return new BlackListMemoryCache(timedCache);
}
/**
*
*
* @author fengshuonan
* @date 2022/11/8 21:24
*/
@Bean("whiteListCache")
public CacheOperatorApi<String> whiteListMemoryCache() {
TimedCache<String, String> timedCache = CacheUtil.newTimedCache(CacheConstants.NONE_EXPIRED_TIME);
return new WhiteListMemoryCache(timedCache);
}
/**
*
*
* @author fengshuonan
* @date 2022/11/8 21:24
*/
@Bean("countValidateCache")
public CacheOperatorApi<Long> countValidateMemoryCache() {
TimedCache<String, Long> timedCache = CacheUtil.newTimedCache(CacheConstants.NONE_EXPIRED_TIME);
return new CountValidateMemoryCache(timedCache);
}
}

View File

@ -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<String> blackListRedisCache(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<String, String> redisTemplate = CreateRedisTemplateUtil.createString(redisConnectionFactory);
return new BlackListRedisCache(redisTemplate);
}
/**
* Redis
*
* @author fengshuonan
* @date 2022/11/8 21:24
*/
@Bean("whiteListCache")
public CacheOperatorApi<String> whiteListRedisCache(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<String, String> redisTemplate = CreateRedisTemplateUtil.createString(redisConnectionFactory);
return new WhiteListRedisCache(redisTemplate);
}
/**
* Redis
*
* @author fengshuonan
* @date 2022/11/8 21:24
*/
@Bean("countValidateCache")
public CacheOperatorApi<Long> countValidateRedisCache(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<String, Long> redisTemplate = CreateRedisTemplateUtil.createObject(redisConnectionFactory);
return new CountValidateRedisCache(redisTemplate);
}
}