一、pom.xml加载该依赖
<dependency>
    <groupId>com.github.axet</groupId>
    <artifactId>kaptcha</artifactId>
    <version>0.0.9</version>
</dependency>
二、RestFul风格,在这里写一个调用验证码的接口:
@GetMapping(value = "/captcha.jpg")
    public void captcha(HttpServletResponse response, HttpServletRequest request) throws Exception {
        response.setHeader("Cache-Control", "no-store,no-cache");
        response.setContentType("image/jpeg");
        /**
         * 生成文字验证码
         */
        String text = producer.createText();
        /**
         * 生成图片验证码
         */
        BufferedImage image = producer.createImage(text);
        /**
         * 保存验证码到Session
         */
        request.getSession().setAttribute(Constants.KAPTCHA_SESSION_KEY,text);
        ServletOutputStream out = response.getOutputStream();
        ImageIO.write(image,"jpg",out);
        IOUtils.closeQuietly(out);
    }
三、写一个KaptchaConfig配置类
@Configuration
public class KaptchaConfig {
    @Bean
    public DefaultKaptcha produer(){
        Properties properties = new Properties();
        /**
         * 验证码有无边框
         */
        properties.put("kapcha.border","no");
        /**
         * 验证码样式
         */
        properties.put("kaptcha.textproducer.font.color","black");
        /**
         * 表示展示几个数字
         */
        properties.put("kaptcha.textproducer.char.space","5");
        Config config = new Config(properties);
        DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
        defaultKaptcha.setConfig(config);
        return defaultKaptcha;
    }
}
四、测试一下

SpringBoot项目之Kaptcha实现登录验证码_spring boot


可喜可贺 哈哈哈哈