RuoYi SpringBoot +Vue实现简易滑块验证,并且集成aj-captcha实现滑块图片验证和文字点选验证(超详细,自测可用)
在RuoYi SpringBoot中集成aj-captcha实现简易滑块验证,首先需要在后端添加aj-captcha依赖,然后配置验证服务,并在前端Vue项目中实现滑块验证的用户界面。
后端集成aj-captcha步骤:
- 添加aj-captcha依赖到
pom.xml
中:
<dependency>
<groupId>com.github.bingoohuang</groupId>
<artifactId>aj-captcha</artifactId>
<version>0.1.10</version>
</dependency>
- 在
application.yml
中配置aj-captcha:
aj-captcha:
slideBlock:
width: 200
height: 40
blockSize: 5
slideLen: 5
- 创建Captcha验证控制器,提供验证接口:
@RestController
@RequestMapping("/captcha")
public class CaptchaController {
@Autowired
private CaptchaService captchaService;
@GetMapping("/get")
public AjaxResult getCaptcha() throws IOException {
SpecCaptcha specCaptcha = new SpecCaptcha(200, 40, 5);
String verCode = specCaptcha.text().toLowerCase();
String captchaKey = specCaptcha.getChallengeForSession();
boolean result = captchaService.validate(captchaKey, verCode);
return AjaxResult.success(captchaKey);
}
@PostMapping("/validate")
public AjaxResult validateCaptcha(String verCode, String captchaKey) {
boolean result = captchaService.validate(captchaKey, verCode);
return AjaxResult.success(result);
}
}
前端Vue实现滑块验证:
- 安装aj-captcha的Vue组件:
npm install aj-captcha-vue
- 在Vue组件中使用aj-captcha-vue:
<template>
<div>
<aj-captcha
@success="handleSuccess"
@fail="handleFail"
:width="200"
:height="40"
:blockSize="5"
:slideLen="5"
></aj-captcha>
</div>
</template>
<script>
import AjCaptcha from 'aj-captcha-vue'
export default {
components: {
AjCaptcha
},
methods: {
handleSuccess(token) {
// 验证成功,发送token到服务器进行验证
this.validateToken(token);
},
handleFail() {
// 验证失败的处理
console.log('验证失败');
},
validateToken(token) {
// 发送token到服务端进行验证
this.$http.post('/captcha/validate', { token: token }).then(response => {
if (response.data.success) {
console.log('验证通过');
} else {
console.log('验证失败');
}
});
}
}
}
</script>
以上代码实现了Vue前端集成aj-captcha滑块验证组件,并在成功滑动后通过API与后端进行验证。
评论已关闭