教你快速使用springboot整合图形验证码的两种方式

本文介绍了在SpringBoot中使用Hutool和Axet两种方式实现图形验证码,探讨了验证码在防止恶意攻击中的作用,并提供了代码示例和配置。作者推荐使用Hutool,后续将分享更多相关知识和资源获取途径。
摘要由CSDN通过智能技术生成

前言

今天给大家展示的是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群里发信息。
在这里插入图片描述

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

忆梦九洲

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值