1、验证码的生成及验证
1.1、生成
Kaptcha 是一个可高度配置的实用验证码生成工具
支持两种类型的验证码:字符类型和算术类型
/**
* 图片验证码(支持算术形式)
*
* @author ruoyi
*/
@Controller
@RequestMapping("/captcha")
public class SysCaptchaController extends BaseController
{
@Resource(name = "captchaProducer")
private Producer captchaProducer;
@Resource(name = "captchaProducerMath")
private Producer captchaProducerMath;
/**
* 验证码生成
*/
@GetMapping(value = "/captchaImage")
public ModelAndView getKaptchaImage(HttpServletRequest request, HttpServletResponse response)
{
ServletOutputStream out = null;
try
{
HttpSession session = request.getSession();
response.setDateHeader("Expires", 0);
response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
response.addHeader("Cache-Control", "post-check=0, pre-check=0");
response.setHeader("Pragma", "no-cache");
response.setContentType("image/jpeg");
// 获取前端传送过来的验证码类型
String type = request.getParameter("type");
String capStr = null; // 算术表达式
String code = null; // 算术结果,放到session中传递给前端,登录的时候连同账号密码传递给后端
BufferedImage bi = null; // 生成的验证码图片
if ("math".equals(type))
{
String capText = captchaProducerMath.createText();
capStr = capText.substring(0, capText.lastIndexOf("@"));
code = capText.substring(capText.lastIndexOf("@") + 1);
bi = captchaProducerMath.createImage(capStr);
}
else if ("char".equals(type))
{
capStr = code =

本文介绍Kaptcha工具生成验证码的方法,并演示如何在Java Web应用中利用Shiro框架进行验证码验证。涵盖验证码生成配置、自定义过滤器以及Shiro拦截器的使用。
最低0.47元/天 解锁文章
1107

被折叠的 条评论
为什么被折叠?



