前言
今天给大家展示的是springboot使用图形验证码的两种方式,第一种基于hutool来实现,第二种方式基于axet实现。现在我们来谈一谈为什么要学习验证码
防止恶意攻击:验证码是一种常用的安全措施,它可以有效地防止恶意攻击,如暴力破解、恶意注册、恶意登录等。通过要求用户输入验证码,可以降低被机器人或恶意程序攻击的风险,因此验证码验证也是人机验证的方式之一。
第一种方式(推荐使用)
相比于第二种方式,这种方式比较简单,它封装了大部分源码,只需调用相关接口就可以。
Maven引入hutool包
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.16</version>
</dependency>
创建控制类LoginControoler
@RestController
public class LoginControoler
{
@RequestMapping("/captcha")
public void Captcha(){
}
}
方法的介绍
验证码功能位于cn.hutool.captcha包中,核心接口为ICaptcha,此接口定义了以下方法:
- createCode 创建验证码,实现类需同时生成随机验证码字符串和验证码图片
- getCode 获取验证码的文字内容
- verify验证验证码是否正确,建议忽略大小写
- write 将验证码写出到目标流中
其中write方法只有一个OutputStream,ICaptcha实现类可以根据这个方法封装写出到文件等方法。
AbstractCaptcha为一个ICaptcha抽象实现类,此类实现了验证码文本生成、非大小写敏感的验证、写出到流和文件等方法,通过继承此抽象类只需实现createImage方法定义图形生成规则即可。
LineCaptcha线段干扰的验证码
把下面代码放到刚刚创建的captcha()方法里面
//定义图形验证码的长和宽
LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 100);
//图形验证码写出,可以写出到文件,也可以写出到流
lineCaptcha.write("d:/line.png");
//输出code
Console.log(lineCaptcha.getCode());
//验证图形验证码的有效性,返回boolean值
lineCaptcha.verify("1234");
//重新生成验证码
lineCaptcha.createCode();
lineCaptcha.write("d:/line.png");
//新的验证码
Console.log(lineCaptcha.getCode());
//验证图形验证码的有效性,返回boolean值
lineCaptcha.verify("1234");
效果如下
CircleCaptcha 圆圈干扰验证码
//定义图形验证码的长、宽、验证码字符数、干扰元素个数
CircleCaptcha captcha = CaptchaUtil.createCircleCaptcha(200, 100, 4, 20);
//图形验证码写出,可以写出到文件,也可以写出到流
captcha.write("d:/circle.png");
//验证图形验证码的有效性,返回boolean值
captcha.verify("1234");
效果
ShearCaptcha 扭曲干扰验证码
//定义图形验证码的长、宽、验证码字符数、干扰线宽度
ShearCaptcha captcha = CaptchaUtil.createShearCaptcha(200, 100, 4, 4);
//ShearCaptcha captcha = new ShearCaptcha(200, 100, 4, 4);
//图形验证码写出,可以写出到文件,也可以写出到流
captcha.write("d:/shear.png");
//验证图形验证码的有效性,返回boolean值
captcha.verify("1234");
效果
自定义验证码
// 自定义纯数字的验证码(随机4位数字,可重复)
RandomGenerator randomGenerator = new RandomGenerator("0123456789", 4);
LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 100);
lineCaptcha.setGenerator(randomGenerator);
// 重新生成code
lineCaptcha.createCode();
效果
在浏览器中输出(Servlet输出)
在Captcha方法里添加HttpServletResponse 参数
public void Captcha(HttpServletResponse response){
// 自定义纯数字的验证码(随机4位数字,可重复)
RandomGenerator randomGenerator = new RandomGenerator("0123456789", 4);
LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 100);
lineCaptcha.setGenerator(randomGenerator);
// 重新生成code
lineCaptcha.createCode();
//使用try-with-resources语句自动关闭OutputStream
try (OutputStream outputStream = response.getOutputStream()) {
//输出内容到浏览器
lineCaptcha.write(outputStream);
outputStream.flush();
} catch (IOException e) {
//抛出异常
throw new RuntimeException(e);
}
效果
第二种方式
Maven引入axet包
<!-- 验证码 -->
<dependency>
<groupId>com.github.axet</groupId>
<artifactId>kaptcha</artifactId>
<version>0.0.9</version>
</dependency>
创建验证码配置类KaptchaCondig
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.Properties;
@Configuration
public class KaptchaCondig {
@Bean
public DefaultKaptcha producer(){
Properties properties = new Properties();
properties.put("kaptcha.border", "no");
properties.put("kaptcha.border.color", "105,179,90");
properties.put("kaptcha.textproducer.char.color", "blue");
properties.put("kaptcha.image.width", "100"); // 验证码图片宽度
properties.put("kaptcha.image.height", "40"); // 验证码图片高度
properties.put("kaptcha.textproducer.font.size", "30");
properties.put("kaptcha.textproducer.char.length", "4");
properties.put("kaptcha.textproducer.font.names", "宋体,楷体,微软雅黑");
properties.put("kaptcha.textproducer.char.string", "0123456789"); // 纯数字验证码
properties.put("kaptcha.noise.color", "black");
properties.put("kaptcha.noise.impl", "com.google.code.kaptcha.impl.DefaultNoise"); //干状线
properties.put("kaptcha.background.clear.from", "232,240,254");//渐变色
properties.put("kaptcha.background.clear.to", "232,240,254");
properties.put("kaptcha.textproducer.char.space", "5");
Config config = new Config(properties);
DefaultKaptcha defaultKaptcha=new DefaultKaptcha();
defaultKaptcha.setConfig(config);
return defaultKaptcha;
}
}
在控制类引入
@Autowired
private Producer producer;
创建session类
//存放session
public class SessionUtil{
public static final String KAPTCHA_SESSION_KEY = "kaptcha"; //验证码的内容
}
编写captcha()方法
在这里使用的session保存,在正式项目里面推荐使用redis
@GetMapping("/captcha.jpg")
public void captcha(HttpServletResponse response, HttpServletRequest request) throws IOException {
response.setHeader("Cache-Control","no-store,no-cache");
response.setContentType("image/jpeg");
//验证码内容
String text = producer.createText();
//得到验证码图片
BufferedImage image = producer.createImage(text);
HttpSession session = request.getSession();
//把文本促存到session中
session.setAttribute(Conversation.KAPTCHA_SESSION_KEY,text);
//设置过期时间60秒
session.setMaxInactiveInterval(60);
//把图片写到返回的远程对象中
ServletOutputStream out=response.getOutputStream();
ImageIO.write(image,"jpg",out);
IOUtils.closeQuietly(out);
}
效果
视频链接
快手:这里
B站:这里
总结
以上图形验证码的两种方式,个人推荐使用第一种,下一期给大家分享异常或者是登录方面的知识,关于图形验证码这一方面知识哪里说的有问题欢迎大家在评论区讨论,想要获取源代码与资料的同学请加入QQ群,有任何问题大家也可以在QQ群里发信息。