springboot整合captcha.超简单的验证码
springboot整合captcha.支持数字字母,算术(加,减,乘法),中文,gif动态中文------(一款超简单的验证码生成)
发现一款超简单的验证码生成,还挺好玩的.还有中文验证码,动态验证码.
1.添加pom依赖
<!--验证码-->
<dependency>
<groupId>com.github.whvcse</groupId>
<artifactId>easy-captcha</artifactId>
<version>1.6.2</version>
</dependency>
2直接在代码中使用就可以
这里是使用postman测试,并没有弄前端的代码.可以理解是前后端分离
package com.example.demo.code;
import com.wf.captcha.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
@RestController
@RequestMapping("/code")
public class CodeController {
/**
* 生成验证码
* @return
* @throws Exception
*/
@GetMapping("/getCode")
public void getCode(HttpServletResponse response) throws Exception {
ServletOutputStream outputStream = response.getOutputStream();
//算术验证码 数字加减乘除. 建议2位运算就行:captcha.setLen(2);
// ArithmeticCaptcha captcha = new ArithmeticCaptcha(120, 40);
// 中文验证码
// ChineseCaptcha captcha = new ChineseCaptcha(120, 40);
// 英文与数字验证码
// SpecCaptcha captcha = new SpecCaptcha(120, 40);
//英文与数字动态验证码
// GifCaptcha captcha = new GifCaptcha(120, 40);
// 中文动态验证码
ChineseGifCaptcha captcha = new ChineseGifCaptcha(120, 40);
// 几位数运算,默认是两位
captcha.setLen(2);
// 获取运算的结果
String result = captcha.text();
System.out.println(result);
captcha.out(outputStream);
}
}
可以设置很多参数.宽高.字体,type等:
3测试使用
1中文动态验证码
2英文与数字动态验证码
后台输出(刷新了好几下,就是想看下有没有数字.果然是有的.在postman里是大写,到了后台就是小写和大写了,有点乱…)
3英文与数字验证码
后台输出:
4中文验证码
后台输出:
5运算验证码
1乘法
后台输出:
2减法
后台输出:
3加法
后台输出:
没有除法.刷新了好多次没有刷出除法了
欢迎大佬们留言评论,共同学习!!!感谢!!!
上一篇:
通过多线程提高代码的执行效率例子
