Spring-boot生成验证码

该博客介绍了如何在Spring-boot项目中实现验证码的生成。首先,在pom.xml文件中添加相关依赖,然后创建CaptchaController类负责处理验证码的请求,利用CaptProductor类生成验证码图像。最后,配置application.properties文件以设置相关参数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在pom.xml中添加:

        <!-- 验证码 -->
        <dependency>
            <groupId>com.github.axet</groupId>
            <artifactId>kaptcha</artifactId>
            <version>0.0.9</version>
        </dependency>

CaptchaController类:

package com.example.demo;

import com.google.code.kaptcha.Producer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.util.UUID;
@Controller
@RequestMapping("/kaptcha")
public class CaptchaController {
    @Autowired
    Producer defaultKaptcha;

    @RequestMapping(value = "/captcha-image")
    public void getKaptchaImage(HttpServletRequest request,
                                        HttpServletResponse response) throws Exception {
        ServletOutputStream out = null;
        String code = defaultKaptcha.createText();
        try {
            response.setDateHeader("Expires", 0);
            response.setHeader("Cache-Control",
                "no-store, no-cache, must-revalidate");
            response.addHeader("Cache-Control", "post-check=0, pre-check=0");
            response.setHeader("Pragma", "no-cache");
            response.setContentType("image/jpeg");
            request.getSession().setAttribute("checkCode",code);
            Cookie cookie = new Cookie("captchaCode",UUID.randomUUID().toString());
            response.addCookie(cookie);
            BufferedImage bi = defaultKaptcha.createImage(code);
            out = response.getOutputStream();
            ImageIO.write(bi, "jpg", out);
            out.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            out.close();
        }
        return ;
    }

}

CaptProductor:

package com.example.demo;

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 CaptProductor {
     @Bean
    public DefaultKaptcha captchaProducer(){
        DefaultKaptcha captchaProducer =new DefaultKaptcha();
        Properties properties =new Properties();
        properties.setProperty("kaptcha.border","yes");
        properties.setProperty("kaptcha.border.color","105,179,90");
        properties.setProperty("kaptcha.textproducer.font.color","blue");
        properties.setProperty("kaptcha.image.width","125");
        properties.setProperty("kaptcha.image.height","45");
        properties.setProperty("kaptcha.textproducer.font.size","45");
        properties.setProperty("kaptcha.session.key","code");
        properties.setProperty("kaptcha.textproducer.char.length","4");
        properties.setProperty("kaptcha.textproducer.font.names","宋体,楷体,微软雅黑");
        Config config=new Config(properties);
        captchaProducer.setConfig(config);
        return  captchaProducer;
    }
}

application.properties:

spring.kaptcha.properties.kaptcha.textproducer.char.string=0123456789ABCDEFGHIJKLMNPQRSTUVWXYZ
spring.kaptcha.properties.kaptcha.textproducer.char.length=4
spring.kaptcha.properties.kaptcha.border=no
spring.kaptcha.properties.kaptcha.image.width=80
spring.kaptcha.properties.kaptcha.image.height=35
spring.kaptcha.properties.kaptcha.border.color=105,179,90
spring.kaptcha.properties.kaptcha.textproducer.font.color=blue
spring.kaptcha.properties.kaptcha.textproducer.font.size=30
spring.kaptcha.properties.kaptcha.textproducer.font.names=宋体,宋体,宋体,宋体
spring.kaptcha.properties.kaptcha.obscurificator.impl=com.google.code.kaptcha.impl.WaterRipple
spring.kaptcha.properties.kaptcha.session.key=code
spring.kaptcha.properties.kaptcha.noise.color=white
spring.kaptcha.properties.kaptcha.background.clear.from=white
spring.kaptcha.properties.kaptcha.background.clear.to=white
kaptcha-spring-boot-starter是一个用于集成kaptcha验证码功能的Spring Boot启动器。通过添加kaptcha-spring-boot-starter的依赖,你可以方便地在Spring Boot项目中使用kaptcha验证码功能。 同时,你需要在配置文件中进行相应的配置,包括指定redis的主机、密码和端口等信息。例如,你可以使用以下配置: ``` spring.redis.host=192.168.9.187 spring.redis.password=123456 spring.redis.port=6379 ``` 这样,在你的Spring Boot项目中就可以使用kaptcha验证码功能了。通过Java配置,你可以更好地理解和管理配置的bean。因此,无论是在Spring 4.x还是在Spring Boot中,都推荐使用Java配置。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [常用工具类之使用kaptcha-spring-boot-starter生成验证码](https://blog.csdn.net/weixin_53998054/article/details/127819474)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [通过Kaptcha初步学习Spring boot starter的原理](https://blog.csdn.net/u010968350/article/details/122612905)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值