springboot 整合Kaptcha验证工具详解

Kaptcha 详细配置信息

配置参数:kaptcha.border

描述:图片边框,合法值:yes , no

默认值:yes


配置参数:kaptcha.border.color

描述:边框颜色,合法值: r,g,b (and optional alpha) 或者 white,black,blue.

默认值:black


配置参数:kaptcha.image.width

描述:图片宽

默认值:200


配置参数:kaptcha.image.height

描述:图片高

默认值:50


配置参数:kaptcha.producer.impl

描述:图片实现类

默认值:com.google.code.kaptcha.impl.DefaultKaptcha


配置参数:kaptcha.textproducer.impl

描述:文本实现类

默认值:com.google.code.kaptcha.text.impl.DefaultTextCreator


配置参数:kaptcha.textproducer.char.string

描述:文本集合,验证码值从此集合中获取

默认值:abcde2345678gfynmnpwx


配置参数:kaptcha.textproducer.char.length

描述:验证码长度

默认值:5


配置参数:kaptcha.textproducer.font.names

描述:字体

默认值:Arial, Courier


配置参数:kaptcha.textproducer.font.size

描述:字体大小

默认值:40px.


配置参数:kaptcha.textproducer.font.color

描述:字体颜色,合法值: r,g,b  或者 white,black,blue.

默认值:black


配置参数:kaptcha.textproducer.char.space

描述:文字间隔

默认值:2


配置参数:kaptcha.noise.impl

描述:干扰实现类

默认值:com.google.code.kaptcha.impl.DefaultNoise


配置参数:kaptcha.noise.color

描述:干扰 颜色,合法值: r,g,b 或者 white,black,blue.

默认值:black


配置参数:kaptcha.obscurificator.impl

描述:图片样式,

   水纹 com.google.code.kaptcha.impl.WaterRipple 

   鱼眼 com.google.code.kaptcha.impl.FishEyeGimpy 

   阴影 com.google.code.kaptcha.impl.ShadowGimpy

默认值:com.google.code.kaptcha.impl.WaterRipple


配置参数:kaptcha.background.impl

描述:背景实现类

默认值:com.google.code.kaptcha.impl.DefaultBackground


配置参数:kaptcha.background.clear.from

描述:背景颜色渐变,开始颜色

默认值:light grey


配置参数:kaptcha.background.clear.to

描述:背景颜色渐变, 结束颜色

默认值:white


配置参数:kaptcha.word.impl

描述:文字渲染器

默认值:com.google.code.kaptcha.text.impl.DefaultWordRenderer


配置参数:kaptcha.session.key

描述:session key

默认值:KAPTCHA_SESSION_KEY


配置参数:kaptcha.session.date

描述:session date

默认值:KAPTCHA_SESSION_DATE


SpringBoot 整合 Kaptcha 使用傈子:

1、pom.xml文件中引入

     <!-- 验证码 -->

     <dependency>

      <groupId>com.github.penggle</groupId>

      <artifactId>kaptcha</artifactId>

      <version>2.3.2</version>

     </dependency>

2、添加配置类

/**

*  验证码配置

*/

@Configuration

public class CaptchaConfig {

 @Bean

 public DefaultKaptcha getDefaultKaptcha(){

        DefaultKaptcha defaultKaptcha = new DefaultKaptcha();

        Properties properties = new Properties();

        properties.put("kaptcha.border", "yes");

        properties.put("kaptcha.border.color","105,179,90");

        properties.put("kaptcha.textproducer.font.color","blue");

        properties.put("kaptcha.image.width","125");

        properties.put("kaptcha.image.height","45");

        properties.put("kaptcha.textproducer.font.size","45");

        properties.put("kaptcha.session.key","code");

        properties.put("kaptcha.textproducer.char.length","4");

        properties.put("kaptcha.textproducer.font.names","宋体,楷体,微软雅黑");

        Config config = new Config(properties );

        defaultKaptcha .setConfig(config);

        return defaultKaptcha ;

}

}


3、接口实现

package com.demo.web.controller.common;


import java.awt.image.BufferedImage;

import java.io.IOException;

import javax.annotation.Resource;

import javax.imageio.ImageIO;

import javax.servlet.ServletOutputStream;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.servlet.ModelAndView;

import com.google.code.kaptcha.Constants;

import com.google.code.kaptcha.Producer;


@Controller

public class KaptchaController {


   @Autowired

    private DefaultKaptcha captchaProducer;


    @RequestMapping(value = "/captchaImage", method = RequestMethod.GET)

    public ModelAndView verification(HttpServletRequest request, HttpServletResponse response) throws IOException {

        response.setDateHeader("Expires", 0);

        response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");

        response.setHeader("Pragma", "no-cache");

        response.setContentType("image/jpeg");

        // 文字验证码

        String capText = captchaProducer.createText();

        // 保存文字验证码到 session

        request.getSession().setAttribute(Constants.KAPTCHA_SESSION_KEY, capText);

        // 图片验证码

        BufferedImage image = captchaProducer.createImage(capText);

        ServletOutputStream out = response.getOutputStream();

        ImageIO.write(image, "jpg", out);

        try {

            out.flush();

        } finally {

            out.close();

        }

        return null;

    }

}

4、访问http://localhost:8080/captchaImage

腾讯云推出云产品限时特惠抢购活动:2C2G云服务器7.9元/月起
本文链接:https://www.jhelp.net/p/iuSWcZjGQdRnsxWL (转载请保留)。
关注下面的标签,发现更多相似文章