DefaultKaptcha生成验证码

第一步:导包(maven环境下)

        <dependency>
            <groupId>com.github.penggle</groupId>
            <artifactId>kaptcha</artifactId>
            <version>2.3.2</version>
        </dependency>        
第二步:使用DefaultKaptcha类需先对验证码样式进行配置
@Configuration
public class KaptchaConfiguration {

    @Bean
    public DefaultKaptcha getDefaultKaptcha() {
        com.google.code.kaptcha.impl.DefaultKaptcha defaultKaptcha = new com.google.code.kaptcha.impl.DefaultKaptcha();
        Properties properties = new Properties();
//--------------------------------------------------------------------------------------
        properties.put("kaptcha.textproducer.char.string", "0123456789");
        properties.put("kaptcha.border", "no");
        properties.put("kaptcha.textproducer.font.color", "black");
        properties.put("kaptcha.textproducer.char.length","4");
        properties.put("kaptcha.image.height","40");
        properties.put("kaptcha.image.width","150");
        properties.put("kaptcha.textproducer.font.size","30");
        properties.put("kaptcha.session.key", "verifyCode");
        properties.put("kaptcha.textproducer.char.space", "5");
        properties.setProperty("kaptcha.image.width", "164");
        properties.setProperty("kaptcha.image.height", "64");
        properties.put("kaptcha.noise.impl","com.google.code.kaptcha.impl.NoNoise");
//--------------------------------------------------------------------------------------
        Config config = new Config(properties);
        defaultKaptcha.setConfig(config);

        return defaultKaptcha;
    }
}

注释内的部分为验证码相关配置,可不设置,默认即可

但是Properties 和 Config 需new ,否则运行报空指针

第三步:生成验证码

@RestController
@RequestMapping("/a")
public class Kaptcha {
    @Autowired
    private DefaultKaptcha defaultKaptcha;
    @RequestMapping("/verifyCode")
    public void defaultKaptcha (HttpServletRequest request , HttpServletResponse response) throws IOException {
        //生成验证码
        String verifyCode = defaultKaptcha.createText();
        System.out.println(verifyCode);
        //写到session中,后续和前端传来的验证码比对
        HttpSession session = request.getSession();
        session.setAttribute("verifyCode",verifyCode);
        //生成验证码图片流
        BufferedImage image = defaultKaptcha.createImage(verifyCode);
        //设置响应头
        response.setHeader("Cache-Control" , "no-store");
        response.setHeader("Pragme" , "no-cache");
        response.setDateHeader("Expires",0);
        response.setContentType("image/jpeg");
        //写到响应输出流中
        ServletOutputStream outputStream = response.getOutputStream();
        ImageIO.write(image,"jpg",outputStream);
        outputStream.flush();
        outputStream.close();
    }
}

Properties属性(官网或者百度即可)

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要在 Spring Boot 中生成验证码,可以使用 Google 的开源项目 Kaptcha。以下是使用 Kaptcha 生成验证码的步骤: 1. 在 pom.xml 中添加以下依赖: ```xml <dependency> <groupId>com.github.penggle</groupId> <artifactId>kaptcha</artifactId> <version>2.3.2</version> </dependency> ``` 2. 创建一个 Controller 类,添加一个生成验证码的接口,代码如下: ```java @RestController public class CaptchaController { @RequestMapping("/captcha") public void captcha(HttpServletRequest request, HttpServletResponse response) throws Exception { // 创建 Kaptcha 对象 DefaultKaptcha kaptcha = new DefaultKaptcha(); // 设置配置 Properties properties = new Properties(); // 图片边框,合法值:yes、no properties.setProperty("kaptcha.border", "yes"); // 边框颜色,合法值:r,g,b (and optional alpha) 或者 white、black、blue properties.setProperty("kaptcha.border.color", "105,179,90"); // 字体颜色,合法值:r,g,b 或者 white、black、blue properties.setProperty("kaptcha.textproducer.font.color", "blue"); // 图片宽 properties.setProperty("kaptcha.image.width", "110"); // 图片高 properties.setProperty("kaptcha.image.height", "40"); // 字体大小 properties.setProperty("kaptcha.textproducer.font.size", "30"); // session key properties.setProperty("kaptcha.session.key", "code"); // 验证码长度 properties.setProperty("kaptcha.textproducer.char.length", "4"); // 字体 properties.setProperty("kaptcha.textproducer.font.names", "宋体,楷体,微软雅黑"); Config config = new Config(properties); kaptcha.setConfig(config); // 生成验证码 String text = kaptcha.createText(); // 将验证码存入 session request.getSession().setAttribute("code", text); // 生成图片 BufferedImage image = kaptcha.createImage(text); // 将图片输出给客户端 ImageIO.write(image, "jpg", response.getOutputStream()); } } ``` 3. 在前端页面中,使用 Uniapp 的 `image` 组件来显示验证码图片,代码如下: ```html <template> <view> <image :src="captchaUrl" @click="refreshCaptcha" mode="widthFix"></image> <input type="text" v-model="code" placeholder="请输入验证码"> </view> </template> <script> export default { data() { return { captchaUrl: '/captcha', code: '' }; }, methods: { refreshCaptcha() { this.captchaUrl = '/captcha?t=' + new Date().getTime(); } } }; </script> ``` 在这个例子中,我们使用了 `t` 参数来强制刷新验证码图片,以确保每次刷新都会生成新的验证码。当用户输入验证码后,我们可以将其与后端生成验证码进行比较,以进行验证
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值