Introduction of Google kaptcha for springboot2.x graphic verification code development

1 Introduction

The Kaptcha framework introduces a highly configurable and practical verification code generator open sourced by Google

  Verification code font/size/color
  Verification code content range (numbers, letters, Chinese characters!)
  Verification code picture size, border, border thickness, border color
  Verification code interference line
  verification code style (fisheye style, 3D , Normal blur)

2.pom.xml add

 <!--kaptcha依赖包-->
            <dependency>
                <groupId>com.baomidou</groupId>
                <artifactId>kaptcha-spring-boot-starter</artifactId>
                <version>1.1.0</version>
            </dependency>

3. Configuration class

package net.wnn.config;

import com.google.code.kaptcha.Constants;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Properties;

@Configuration
public class CaptchaConfig {

    @Bean
    @Qualifier("captchaProducer")
    public DefaultKaptcha defaultKaptcha(){

        DefaultKaptcha kaptcha = new DefaultKaptcha();
        Properties properties = new Properties();
//        properties.setProperty(Constants.KAPTCHA_BORDER, "yes");
//        properties.setProperty(Constants.KAPTCHA_BORDER_COLOR, "220,220,220");
//        //properties.setProperty(Constants.KAPTCHA_TEXTPRODUCER_FONT_COLOR, "38,29,12");
//        properties.setProperty(Constants.KAPTCHA_IMAGE_WIDTH, "147");
//        properties.setProperty(Constants.KAPTCHA_IMAGE_HEIGHT, "34");
//        properties.setProperty(Constants.KAPTCHA_TEXTPRODUCER_FONT_SIZE, "25");
//        //properties.setProperty(Constants.KAPTCHA_SESSION_KEY, "code");

        //Number of verification codes
        properties.setProperty(Constants.KAPTCHA_TEXTPRODUCER_CHAR_LENGTH, "4");
// properties.setProperty(Constants.KAPTCHA_TEXTPRODUCER_FONT_NAMES, "Courier");
        //Font interval
        properties.setProperty(Constants.KAPTCHA_T) ;
        //Interference line color
// properties.setProperty(Constants.KAPTCHA_NOISE_COLOR, "white");
        //Interference implementation class
        properties.setProperty(Constants.KAPTCHA_NOISE_IMPL, "com.google.code.kaptcha.impl.NoNoise");
        / /Picture style
        properties.setProperty(Constants.KAPTCHA_OBSCURIFICATOR_IMPL, "com.google.code.kaptcha.impl.WaterRipple");

        //文字来源
        properties.setProperty(Constants.KAPTCHA_TEXTPRODUCER_CHAR_STRING, "0123456789");
        Config config = new Config(properties);
        kaptcha.setConfig(config);
        return kaptcha;

    }

}

4. Develop a Controller usage test

import com.google.code.kaptcha.Producer;
import org.springframework.data.redis.core.StringRedisTemplate;

   @Autowired
    private Producer captchaProducer;

    @Autowired
    private StringRedisTemplate redisTemplate;

    /*The
     
graphic verification code is valid for 10 minutes
     /
    private static final long CAPTCHA_CODE_EXPIRED = 60
1000 * 10;

    /*
     
Get graphic verification code
     @param request
     
@param response
     */
    @ApiOperation("Get graphic verification code")
    @GetMapping("captcha")
    public void getCaptcha(HttpServletRequest request, HttpServletResponse response){

        String captchaText = captchaProducer.createText();
        log.info("Graphic verification code:{}",captchaText);

        //存储redis
        redisTemplate.opsForValue().set(getCaptchaKey(request),captchaText,CAPTCHA_CODE_EXPIRED,TimeUnit.MILLISECONDS);
        BufferedImage bufferedImage = captchaProducer.createImage(captchaText);
        ServletOutputStream outputStream = null;
        try {
            outputStream = response.getOutputStream();
            ImageIO.write(bufferedImage,"jpg",outputStream);
            outputStream.flush();
            outputStream.close();
        } catch (IOException e) {
            log.error("获取图形验证码异常:{}",e);
        }

    }

5.Introduction of Google kaptcha for springboot2.x graphic verification code development

Guess you like

Origin blog.51cto.com/13779430/2641539