现在的系统中对于登录验证的方式越来越多,推荐一个很好的滑动块验证码的方案。
附上该开源项目的gitee地址:https://gitee.com/anji-plus/captcha
该项目中详细说明了支持Spring和Spring Boot项目接入的方案,可以点击上方的gitee地址去学习,本文的介绍只根据自身的SpringBoot项目进行示范。
以下具体集成的步骤:
1、在对应的项目中引入pom:
<dependency>
<groupId>com.anji-plus</groupId>
<artifactId>spring-boot-starter-captcha</artifactId>
<version>1.3.0</version>
</dependency>
并将静态资源如图片,滑块的底图放在自己项目对应的/resources目录
2、自己实现CaptchaCacheService 接口,将滑动块的信息存入redis中。
@Override
public String type() {
return "redis";
}
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Override
public void set(String key, String value, long expiresInSeconds) {
stringRedisTemplate.opsForValue().set(key, value, expiresInSeconds, TimeUnit.SECONDS);
}
3、上面的具体实现类通过spi机制暴露
4、application下的一些设置的配置项目(可选)
aj:
captcha:
# 滑动验证,底图路径,不配置将使用默认图片
# 支持全路径
# 支持项目路径,以classpath:开头,取resource目录下路径,例:classpath:images/jigsaw
jigsaw: classpath:images/jigsaw
#滑动验证,底图路径,不配置将使用默认图片
##支持全路径
# 支持项目路径,以classpath:开头,取resource目录下路径,例:classpath:images/pic-click
pic-click: classpath:images/pic-click
# 缓存local/redis...
cache-type: redis
# 验证码类型default两种都实例化。
type: default
# 右下角水印文字(我的水印)
water-mark:
# 校验滑动拼图允许误差偏移量(默认5像素)
slip-offset: 5
# aes加密坐标开启或者禁用(true|false)
aes-status: true
# 滑动干扰项(0/1/2)
interference-options: 2
#点选字体样式 默认Font.BOLD
font-style: 2
#点选字体字体大小
font-size: 12
history-data-clear-enable: false
# 接口请求次数一分钟限制是否开启 true|false
req-frequency-limit-enable: false
# get接口一分钟内请求数限制
req-get-lock-limit: 5
# 验证失败后,锁定时间间隔,s# 验证失败后,锁定时间间隔,s
req-get-lock-seconds: 360
# get接口一分钟内请求数限制
req-get-minute-limit: 30
# check接口一分钟内请求数限制
req-check-minute-limit: 30
# verify接口一分钟内请求数限制
req-verify-minute-limit: 60
5、在自己项目的授权认证处中增加对滑动块验证的支持,获取token,实现登录。
HttpServletRequest request = WebUtil.getRequest();
String captchaVerification = tokenParameter.getArgs().getStr("captchaVerification");
if (Func.isNotBlank(captchaVerification)) {
CaptchaVO captchaVO = new CaptchaVO();
captchaVO.setCaptchaVerification(captchaVerification);
captchaService.verification(captchaVO);}