Spring boot 2.3.3集成kaptcha验证码

实验环境:
  • spring boot 2.3.3
  • idea 2019.2.3
  • jdk 1.8
引入依赖:
        <!--验证码依赖-->
        <dependency>
            <groupId>com.github.penggle</groupId>
            <artifactId>kaptcha</artifactId>
            <version>2.3.2</version>
        </dependency>
编写验证码配置文件:

文件路径,config/KaptchaConfig.java

@Configuration
public class KaptchaConfig {
    @Bean
    public DefaultKaptcha getKaptcha() {
        Properties properties = new Properties();
        // 图片边框:yes,no
        properties.setProperty("kaptcha.border", "yes");
        // 图片边框颜色:r,g,b
        properties.setProperty("kaptcha.border.color", "105,179,90");
        // 图片宽
        properties.setProperty("kaptcha.image.width", "110");
        // 图片高
        properties.setProperty("kaptcha.image.height", "40");
        // 字体
        properties.setProperty("kaptcha.textproducer.font.names", "宋体,楷体,微软雅黑");
        // 字体大小
        properties.setProperty("kaptcha.textproducer.font.size", "30");
        // 字体颜色
        properties.setProperty("kaptcha.textproducer.font.color", "red");
        // 字符个数
        properties.setProperty("kaptcha.textproducer.char.length", "6");
        // 字符取值范围
        properties.setProperty("kaptcha.textproducer.char.string", "0123456789ABCEFGHIJKLMNOPQRSTUVWXYZ");
        // 验证码保存在session的key
        properties.setProperty("kaptcha.session.key", "code");
        Config config = new Config(properties);
        DefaultKaptcha kaptcha = new DefaultKaptcha();
        kaptcha.setConfig(config);
        return kaptcha;
    }
}
编写生成验证码控制类:

文件路径,controller/KaptchaController.java

@Controller
public class KaptchaController {
    @Resource
    DefaultKaptcha kaptcha;

    @RequestMapping("/getKaptcha")
    public void getKaptcha(HttpServletRequest request, HttpServletResponse response) throws IOException {
        byte[] b;
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        try {
            // 生产验证码并保存到session中
            String createText = kaptcha.createText();
            request.getSession().setAttribute("rightCode", createText);
            // 使用生成的验证码返回一个BufferedImage对象并写入到字节数组流outputStream中
            BufferedImage image = kaptcha.createImage(createText);
            ImageIO.write(image, "jpg", outputStream);
        } catch (Exception e) {
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }
        b = outputStream.toByteArray();
        response.setHeader("Cache-Control", "no-store");
        response.setHeader("Pragma", "no-cache");
        response.setDateHeader("Expires", 0);
        response.setContentType("image/jpeg");
        ServletOutputStream stream = response.getOutputStream();
        stream.write(b);
        stream.flush();
        stream.close();
    }
}
查看验证码:

在浏览器中输入http://localhost:8090/getKaptcha,如下图:

在这里插入图片描述

编写校验验证码控制类:

文件路径,controller/KaptchaController.java

    @ResponseBody
    @RequestMapping("/checkKaptcha")
    public String checkKaptcha(HttpServletRequest request) {
        String rightCode = (String) request.getSession().getAttribute("rightCode");
        String inputCode = request.getParameter("inputCode");
        if (!rightCode.equalsIgnoreCase(inputCode)) {
            return "验证码输入错误!";
        }
        return "验证码输入正确!";
    }
检验验证码:

在浏览器中输入http://localhost:8090/checkKaptcha?inputCode=opy58c,如下图:

在这里插入图片描述

最后:对你有所帮助的话,记得点赞哦!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值