在 spring boot 中,你可以将 hutool 生成验证码的功能集成到 restful api 接口中。
依赖
<dependency>
<groupid>cn.hutool</groupid>
<artifactid>hutool-all</artifactid>
<version>5.8.14</version> <!-- 使用最新版本 -->
</dependency>创建验证码
package com.base.controller;
import cn.hutool.captcha.captchautil;
import cn.hutool.captcha.circlecaptcha;
import cn.hutool.captcha.linecaptcha;
import cn.hutool.captcha.shearcaptcha;
import cn.hutool.captcha.generator.mathgenerator;
import cn.hutool.captcha.generator.randomgenerator;
import io.swagger.annotations.api;
import io.swagger.annotations.apioperation;
import org.springframework.http.httpheaders;
import org.springframework.http.httpstatus;
import org.springframework.http.responseentity;
import org.springframework.web.bind.annotation.getmapping;
import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.bind.annotation.restcontroller;
import javax.servlet.http.httpservletresponse;
import java.io.ioexception;
@restcontroller
@requestmapping("/api/captcha")
@api(tags = "验证码")
public class captchacontroller {
@getmapping("/image")
@apioperation("线段干扰的验证码")
public void getcaptchaimage(httpservletresponse response) throws ioexception {
//定义图形验证码的长和宽
linecaptcha captcha = captchautil.createlinecaptcha(200, 100);
system.out.println("验证码:"+captcha.getcode());
// 设置响应类型为图片
response.setcontenttype("image/png");
// 将验证码图片写入响应
captcha.write(response.getoutputstream());
}
@getmapping("/image2")
@apioperation("圆圈干扰验证码")
public void getcaptchaimage2(httpservletresponse response) throws ioexception {
// 创建验证码对象
//定义图形验证码的长、宽、验证码字符数、干扰元素个数
circlecaptcha captcha = captchautil.createcirclecaptcha(200, 100, 4, 20);
system.out.println("验证码:"+captcha.getcode());
// 设置响应类型为图片
response.setcontenttype("image/png");
// 将验证码图片写入响应
captcha.write(response.getoutputstream());
}
@getmapping("/image3")
@apioperation("扭曲干扰验证码")
public void getcaptchaimage3(httpservletresponse response) throws ioexception {
//定义图形验证码的长、宽、验证码字符数、干扰线宽度
shearcaptcha captcha = captchautil.createshearcaptcha(200, 100, 4, 4);
system.out.println("验证码:"+captcha.getcode());
// 设置响应类型为图片
response.setcontenttype("image/png");
// 将验证码图片写入响应
captcha.write(response.getoutputstream());
}
@getmapping("/image4")
@apioperation("自定义纯数字的验证码")
public void getcaptchaimage4(httpservletresponse response) throws ioexception {
//定义图形验证码的长、宽、验证码字符数、干扰线宽度
// 自定义纯数字的验证码(随机4位数字,可重复)
randomgenerator randomgenerator = new randomgenerator("0123456789", 4);
linecaptcha linecaptcha = captchautil.createlinecaptcha(200, 100);
linecaptcha.setgenerator(randomgenerator);
// 重新生成code
linecaptcha.createcode();
// 设置响应类型为图片
response.setcontenttype("image/png");
// 将验证码图片写入响应
linecaptcha.write(response.getoutputstream());
}
@getmapping("/image5")
@apioperation("加减乘除的验证码")
public void getcaptchaimage5(httpservletresponse response) throws ioexception {
shearcaptcha captcha = captchautil.createshearcaptcha(200, 45, 4, 4);
// 自定义验证码内容为四则运算方式
captcha.setgenerator(new mathgenerator(1));
// 重新生成code
captcha.createcode();
mathgenerator mathgenerator = new mathgenerator();
// 用户输入校验
system.out.println("验证结果:"+mathgenerator.verify(captcha.getcode(), "1"));
// 设置响应类型为图片
response.setcontenttype("image/png");
// 将验证码图片写入响应
captcha.write(response.getoutputstream());
}
}访问验证码接口
上面提到的5种样式,效果如下:





参考资料:文档
到此这篇关于springboot整合hutool验证码的文章就介绍到这了,更多相关springboot hutool验证码内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论