Springboot项目中利用kaptcha完成生成验证码的功能

1.导入依赖

  • 直接上官网即可,如下图
    在这里插入图片描述

2.编写kaptcha配置类

  • 编写KaptchaConfig类进行配置
  • 代码:
package com.nowcoder.community.config;

import com.google.code.kaptcha.Producer;
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;

/**
 * @author : wys
 * @date : 2020-11-22 13:15
 **/
@Configuration
public class KaptchaConfig {
    @Bean
    public Producer kaptchaProducer() {
        //进行验证码的配置
        Properties properties = new Properties();
        //宽度
        properties.setProperty("kaptcha.image.width","100");
        //高度
        properties.setProperty("kaptcha.image.height","40");
        //里面元素的大小
        properties.setProperty("kaptcha.textproducer.font.size","32");
        //颜色
        properties.setProperty("kaptcha.textproducer.font.color","0,0,0");
        //会在哪些字符串选取验证码
        properties.setProperty("kaptcha.textproducer.char.string","0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ");
        //验证码字符个数
        properties.setProperty("kaptcha.textproducer.char.length","4");
        //验证码的干扰项
        properties.setProperty("kaptcha.noise.impl","com.google.code.kaptcha.impl.NoNoise");
        //声明配置
        DefaultKaptcha kaptcha = new DefaultKaptcha();
        Config config = new Config(properties);
        kaptcha.setConfig(config);
        return kaptcha;
    }
}

3.在对应的LoginController中增加使用的方法

@RequestMapping(path = "/kaptcha", method = RequestMethod.GET)
    public void getKaptcha(HttpServletResponse response, HttpSession session){
        // 生成验证码
        String text = kaptchaProducer.createText();
        BufferedImage image = kaptchaProducer.createImage(text);

        // 将验证码存入session
        session.setAttribute("kaptcha", image);

        // 将图片输出给浏览器
        response.setContentType("image/png");
        try {
            OutputStream os = response.getOutputStream();
            ImageIO.write(image, "png", os);
        } catch (IOException e) {
            logger.error("响应验证码失败:" + e.getMessage());
        }
    }

4.修改对应的前端代码

<div class="col-sm-4">
	<img th:src="@{/kaptcha}" id="kaptcha" style="width:100px;height:40px;" class="mr-2"/>
	<a href="javascript:refresh_kaptcha();" class="font-size-12 align-bottom">刷新验证码</a>
</div>
//js代码,实现刷新验证码的功能
<script>
	function refresh_kaptcha() {
		var path = CONTEXT_PATH + "/kaptcha?p=" + Math.random();
		$("#kaptcha").attr("src", path);
	}
</script>

5.实现效果图

  • 第一张为可以直接访问路径时不断刷新,就可以产生不同的验证码图片
    在这里插入图片描述

  • 完全实现后的效果图,每次都可以点击刷新
    在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您提供制作验证码的步骤: 1. 添加依赖 在 pom.xml 文件添加以下依赖: ```xml <dependency> <groupId>com.github.axet</groupId> <artifactId>kaptcha</artifactId> <version>0.0.9</version> </dependency> ``` 2. 配置 Kaptcha 在 application.properties 或 application.yml 文件添加以下配置: ```yaml kaptcha: border: no border.color: black textproducer.font.color: black textproducer.char.length: 4 noise.impl: com.github.axet.kaptcha.impl.DefaultNoise noise.color: black ``` 这些配置项可以根据实际需求进行修改。 3. 生成验证码 在控制器添加以下代码: ```java @GetMapping("/captcha.jpg") public void captcha(HttpServletRequest request, HttpServletResponse response) throws Exception { // 创建 Kaptcha 对象 ConfigurableKaptcha kaptcha = new ConfigurableKaptcha(); // 生成验证码 String code = kaptcha.createText(); // 将验证码存入 Session request.getSession().setAttribute("captcha", code); // 将验证码输出到页面 response.setContentType("image/jpeg"); ServletOutputStream outputStream = response.getOutputStream(); BufferedImage image = kaptcha.createImage(code); ImageIO.write(image, "jpg", outputStream); outputStream.flush(); outputStream.close(); } ``` 这段代码会在 /captcha.jpg 路径下生成验证码图片,并将验证码存入 Session 。 4. 验证验证码 在需要验证验证码的地方,可以使用如下代码: ```java String captcha = request.getParameter("captcha"); String sessionCaptcha = (String) request.getSession().getAttribute("captcha"); if (!captcha.equalsIgnoreCase(sessionCaptcha)) { // 验证码错误 } ``` 这段代码会从请求参数获取验证码,然后和 Session 验证码进行比对,如果不一致则说明验证码错误。 以上就是使用 Kaptcha 制作验证码的步骤,希望对您有帮助!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值