springboot使用kaptcha框架验证码

1.maven依赖

 <!--验证码框架 -->
        <dependency>
            <groupId>com.github.penggle</groupId>
            <artifactId>kaptcha</artifactId>
            <version>2.3.2</version>
        </dependency>

2.控制器 宽度和高度还有颜色可以调节

package com.example.development.config;

import com.google.code.kaptcha.Producer;
import com.google.code.kaptcha.util.Config;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.util.Properties;

/**
 * Created with IDEA
 * author:QinWei
 * Date:2019/4/23
 * Time:16:39
 * @Description Kaptcha验证码配置
 */
@Component
public class KaptchaConfig {

    @Bean
    public Producer KaptchaProducer() {
        Properties kaptchaProperties = new Properties();
        kaptchaProperties.put("kaptcha.border", "no");
        kaptchaProperties.put("kaptcha.textproducer.char.length", "4");
        kaptchaProperties.put("kaptcha.textproducer.char.space", "6");
        kaptchaProperties.put("kaptcha.image.height", "80");
        // kaptchaProperties.put("kaptcha.image.width","125");
        kaptchaProperties.put("kaptcha.obscurificator.impl", "com.google.code.kaptcha.impl.WaterRipple");
        kaptchaProperties.put("kaptcha.textproducer.font.color", "blue");
        kaptchaProperties.put("kaptcha.textproducer.font.size", "60");
        //kaptchaProperties.put("kaptcha.noise.impl","com.google.code.kaptcha.impl.NoNoise");
        kaptchaProperties.put("kaptcha.noise.impl", "com.google.code.kaptcha.impl.DefaultNoise");
        kaptchaProperties.put("kaptcha.textproducer.char.string", "123456789");

        Config config = new Config(kaptchaProperties);
        return config.getProducerImpl();
    }
}

3.controller

  @Autowired
        private Producer producer;

 @ApiOperation(value = "验证码")
        @GetMapping("/captcha.jpg")
        public void kaptcha(HttpServletResponse response, HttpSession session) throws         IOException {
            //生成文字验证码
            String code = producer.createText();
            //redisService.set(uuid, code);
            session.setAttribute("code",code);
            response.setHeader("Cache-Control", "no-store,no-cache");
            response.setContentType("image/jpeg");

            BufferedImage image = producer.createImage(code);
            ServletOutputStream outputStream = response.getOutputStream();
            ImageIO.write(image, "jpg", outputStream);
            IOUtils.closeQuietly(outputStream);
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值