SpringBoot利用Kaptcha生成验证码

话不多说直接上代码吧


首先引入Kaptcha的依赖

        <dependency>
            <groupId>com.github.penggle</groupId>
            <artifactId>kaptcha</artifactId>
            <version>2.3.2</version>
        </dependency>

然后这里通过java配置Kaptcha

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

import java.util.Properties;

/**
 *
 - @author IBean
 - @date 2018/8/9
 **/
@Component
public class KaptchaConfig {
    @Bean
    public DefaultKaptcha getDefaultKaptcha() {
        com.google.code.kaptcha.impl.DefaultKaptcha defaultKaptcha = new com.google.code.kaptcha.impl.DefaultKaptcha();
        Properties properties = new Properties();
        properties.setProperty("kaptcha.border", "yes");
        properties.setProperty("kaptcha.border.color", "105,179,90");
        properties.setProperty("kaptcha.image.width", "125");
        properties.setProperty("kaptcha.image.height", "38");
        properties.setProperty("kaptcha.textproducer.char.length", "4");
        properties.setProperty("kaptcha.textproducer.font.size", "32");
        properties.setProperty("kaptcha.textproducer.font.names", "宋体,楷体,微软雅黑");
        properties.setProperty("kaptcha.textproducer.font.color", "white");
        properties.setProperty("kaptcha.noise.color", "black");
        properties.setProperty("kaptcha.background.clear.from", "102,153,153");
        properties.setProperty("kaptcha.background.clear.to", "183,234,112");
        properties.setProperty("kaptcha.obscurificator.impl", "com.google.code.kaptcha.impl.ShadowGimpy");

        Config config = new Config(properties);
        defaultKaptcha.setConfig(config);

        return defaultKaptcha;
    }
}
  • 首先别忘了@Component,还有@Bean,这样后续才能通过@Autowired进行注入
  • 了解一下配置吧
    1.kaptcha.border 边框相关,就是图片有没有边
    2.kaptcha.border.color 边框颜色
    3.kaptcha.image.width 生成的图片宽度
    4.kaptcha.image.height 生成的图片高度
    5.kaptcha.textproducer.char.length 生成的验证码里字符的个数
    6.啊不想写了,我再写最后一个
    7.kaptcha.obscurificator.impl生成验证码,我理解的就是样式,有三种,水纹,鱼眼,阴影com.google.code.kaptcha.impl.ShadowGimpy,我用的就是这个阴影,感觉这个显示效果最好
  • 其余配置emmm百度一下吧


    接下来是获取验证码的部分

    private DefaultKaptcha defaultKaptcha; //上边那个那个配置
    @Autowired
    public KaptchaController(DefaultKaptcha defaultKaptcha) {
        this.defaultKaptcha = defaultKaptcha;
    }
    /**
     * 获取验证码 的 请求路径
     *
     * @throws Exception
     */
    @RequestMapping("/vaildCode")
    @ResponseBody
    public void defaultKaptcha() {
        ByteArrayOutputStream jpegOutputStream = new ByteArrayOutputStream();
        OutputStream out = null;
        try {
            //生产验证码字符串并保存到session中
            String createText = defaultKaptcha.createText();
            //这个createText其实可以随便写然后下面createImage都会显示在验证码的图片里,不过我没试过中文。
            //当然写个数学表达式或者其他骚操作都行啊,然后把答案放到session(vrifyCode)里就好啦
            request.getSession().setAttribute("vrifyCode", createText);
            //使用生产的验证码字符串返回一个BufferedImage对象并转为byte写入到byte数组中
            BufferedImage challenge = defaultKaptcha.createImage(createText);
            ImageIO.write(challenge, "jpg", jpegOutputStream);
            //定义response输出类型为image/jpeg类型,使用response输出流输出图片的byte数组
            byte[] captchaChallengeAsJpeg = jpegOutputStream.toByteArray();

            response.setHeader("Cache-Control", "no-store");
            response.setHeader("Pragma", "no-cache");
            response.setDateHeader("Expires", 0);
            response.setContentType("image/jpeg");
            out = response.getOutputStream();
            out.write(captchaChallengeAsJpeg);
            out.flush();
        } catch (Exception e) {
            logger.error(e.getMessage());
        } finally {
            try {
                jpegOutputStream.close();
                if (out != null) {
                    out.close();
                }
                System.gc();
            } catch (IOException e) {
                logger.error(e.getMessage());
            }
        }
    }

@ResponseBody这个必须加哦,某篇博文我简单的解释了下

前端部分
<img class="vaildImg" alt="验证码" style="cursor:pointer;">

//初始化的时候加载验证码
$('.vaildImg').attr('src', '/vaildCode?d=' + (new Date()).getTime());
//点击图片切换验证码
$('.vaildImg').click(function () {
    $(this).attr('src', '/vaildCode?d=' + (new Date()).getTime());
});

然后就好了
然后就没了
然后我也不知道该写啥了
不出意外这个就好使了
话说只有没钱才会本地生成验证码吧
不过能用就好啦b( ̄▽ ̄)d 

猜你喜欢

转载自blog.csdn.net/github_36887863/article/details/81564794