【7.3.2】【cache】优化验证码缓存配置

pull/41/head
fengshuonan 2022-11-08 20:54:33 +08:00
parent 2c4a4a357b
commit 5888956cba
4 changed files with 74 additions and 10 deletions

View File

@ -73,6 +73,14 @@
<version>${roses.version}</version> <version>${roses.version}</version>
</dependency> </dependency>
<!--Redis缓存可选依赖-->
<dependency>
<groupId>cn.stylefeng.roses</groupId>
<artifactId>cache-sdk-redis</artifactId>
<version>${roses.version}</version>
<optional>true</optional>
</dependency>
</dependencies> </dependencies>
</project> </project>

View File

@ -24,17 +24,18 @@
*/ */
package cn.stylefeng.roses.kernel.security.starter; package cn.stylefeng.roses.kernel.security.starter;
import cn.hutool.cache.CacheUtil; import cn.stylefeng.roses.kernel.cache.api.CacheOperatorApi;
import cn.hutool.cache.impl.TimedCache;
import cn.stylefeng.roses.kernel.security.api.DragCaptchaApi; import cn.stylefeng.roses.kernel.security.api.DragCaptchaApi;
import cn.stylefeng.roses.kernel.security.api.ImageCaptchaApi; import cn.stylefeng.roses.kernel.security.api.ImageCaptchaApi;
import cn.stylefeng.roses.kernel.security.captcha.DragCaptchaService; import cn.stylefeng.roses.kernel.security.captcha.DragCaptchaService;
import cn.stylefeng.roses.kernel.security.captcha.ImageCaptchaService; 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.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; 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 * @date 2020/12/1 21:44
*/ */
@Configuration @Configuration
@AutoConfigureAfter(SecurityCacheAutoConfiguration.class)
public class CaptchaAutoConfiguration { public class CaptchaAutoConfiguration {
@Resource(name = "captchaCache")
private CacheOperatorApi<String> captchaCache;
/** /**
* *
* *
@ -54,9 +59,7 @@ public class CaptchaAutoConfiguration {
@ConditionalOnMissingBean(ImageCaptchaApi.class) @ConditionalOnMissingBean(ImageCaptchaApi.class)
public ImageCaptchaApi captchaApi() { public ImageCaptchaApi captchaApi() {
// 验证码过期时间 120秒 // 验证码过期时间 120秒
TimedCache<String, String> timedCache = CacheUtil.newTimedCache(1000 * 120); return new ImageCaptchaService(captchaCache);
CaptchaMemoryCache captchaMemoryCache = new CaptchaMemoryCache(timedCache);
return new ImageCaptchaService(captchaMemoryCache);
} }
/** /**
@ -69,9 +72,7 @@ public class CaptchaAutoConfiguration {
@ConditionalOnMissingBean(DragCaptchaApi.class) @ConditionalOnMissingBean(DragCaptchaApi.class)
public DragCaptchaApi dragCaptchaService() { public DragCaptchaApi dragCaptchaService() {
// 验证码过期时间 120秒 // 验证码过期时间 120秒
TimedCache<String, String> timedCache = CacheUtil.newTimedCache(1000 * 120); return new DragCaptchaService(captchaCache);
CaptchaMemoryCache captchaMemoryCache = new CaptchaMemoryCache(timedCache);
return new DragCaptchaService(captchaMemoryCache);
} }
} }

View File

@ -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<String> captchaMemoryCache() {
// 验证码过期时间 120秒
TimedCache<String, String> 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<String> captchaRedisCache(RedisConnectionFactory redisConnectionFactory) {
// 验证码过期时间 120秒
RedisTemplate<String, String> redisTemplate = CreateRedisTemplateUtil.createString(redisConnectionFactory);
return new CaptchaRedisCache(redisTemplate);
}
}

View File

@ -2,4 +2,5 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
cn.stylefeng.roses.kernel.security.starter.GunsSecurityAutoConfiguration,\ cn.stylefeng.roses.kernel.security.starter.GunsSecurityAutoConfiguration,\
cn.stylefeng.roses.kernel.security.starter.GunsXssAutoConfiguration,\ cn.stylefeng.roses.kernel.security.starter.GunsXssAutoConfiguration,\
cn.stylefeng.roses.kernel.security.starter.CounterAutoConfiguration,\ 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