mirror of https://gitee.com/stylefeng/roses
【7.3.2】【cache】优化验证码缓存配置
parent
2c4a4a357b
commit
5888956cba
|
@ -73,6 +73,14 @@
|
|||
<version>${roses.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!--Redis缓存,可选依赖-->
|
||||
<dependency>
|
||||
<groupId>cn.stylefeng.roses</groupId>
|
||||
<artifactId>cache-sdk-redis</artifactId>
|
||||
<version>${roses.version}</version>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -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<String> captchaCache;
|
||||
|
||||
/**
|
||||
* 图形验证码
|
||||
*
|
||||
|
@ -54,9 +59,7 @@ public class CaptchaAutoConfiguration {
|
|||
@ConditionalOnMissingBean(ImageCaptchaApi.class)
|
||||
public ImageCaptchaApi captchaApi() {
|
||||
// 验证码过期时间 120秒
|
||||
TimedCache<String, String> 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<String, String> timedCache = CacheUtil.newTimedCache(1000 * 120);
|
||||
CaptchaMemoryCache captchaMemoryCache = new CaptchaMemoryCache(timedCache);
|
||||
return new DragCaptchaService(captchaMemoryCache);
|
||||
return new DragCaptchaService(captchaCache);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue