天天看點

SpringBoot 內建kaptcha驗證碼

  1. 引入jar包
<!-- 驗證碼-->
        <dependency>
            <groupId>com.github.penggle</groupId>
            <artifactId>kaptcha</artifactId>
            <version>2.3.2</version>
        </dependency>
           
  1. 驗證碼樣式調整(圖檔二維碼的樣式設定)
    • 編寫一個config配置類,在springboot中注入
/**
 * 驗證碼樣式調整
 *
 * @atuhor libo
 * @date 2020/06/24
 **/
@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", "no");
        // 邊框顔色
        properties.setProperty("kaptcha.border.color", "black");
        //邊框厚度
        properties.setProperty("kaptcha.border.thickness", "1");
        // 圖檔寬
        properties.setProperty("kaptcha.image.width", "200");
        // 圖檔高
        properties.setProperty("kaptcha.image.height", "50");
        //圖檔實作類
        properties.setProperty("kaptcha.producer.impl", "com.google.code.kaptcha.impl.DefaultKaptcha");
        //文本實作類
        properties.setProperty("kaptcha.textproducer.impl", "com.google.code.kaptcha.text.impl.DefaultTextCreator");
        //文本集合,驗證碼值從此集合中擷取
        properties.setProperty("kaptcha.textproducer.char.string", "01234567890");
        //驗證碼長度
        properties.setProperty("kaptcha.textproducer.char.length", "4");
        //字型
        properties.setProperty("kaptcha.textproducer.font.names", "宋體");
        //字型顔色
        properties.setProperty("kaptcha.textproducer.font.color", "black");
        //文字間隔
        properties.setProperty("kaptcha.textproducer.char.space", "5");
        //幹擾實作類
        properties.setProperty("kaptcha.noise.impl", "com.google.code.kaptcha.impl.DefaultNoise");
        //幹擾顔色
        properties.setProperty("kaptcha.noise.color", "blue");
        //幹擾圖檔樣式
        properties.setProperty("kaptcha.obscurificator.impl", "com.google.code.kaptcha.impl.WaterRipple");
        //背景實作類
        properties.setProperty("kaptcha.background.impl", "com.google.code.kaptcha.impl.DefaultBackground");
        //背景顔色漸變,結束顔色
        properties.setProperty("kaptcha.background.clear.to", "white");
        //文字渲染器
        properties.setProperty("kaptcha.word.impl", "com.google.code.kaptcha.text.impl.DefaultWordRenderer");
        Config config = new Config(properties);
        defaultKaptcha.setConfig(config);
        return defaultKaptcha;
    }
}

           
  1. 編寫ValidCode工具類(配置圖形驗證碼傳回基本格式)
public class ValidCodeUtil {
    /**
     * 生成驗證碼圖檔
     * @param request 設定session
     * @param response 轉成圖檔
     * @param captchaProducer 生成圖檔方法類
     * @param validateSessionKey session名稱
     * @throws Exception
     */
    public static void validateCode(HttpServletRequest request, HttpServletResponse response, DefaultKaptcha captchaProducer, String validateSessionKey) throws Exception{
        // Set to expire far in the past.
        response.setDateHeader("Expires", 30);
        // Set standard HTTP/1.1 no-cache headers.
        response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
        // Set IE extended HTTP/1.1 no-cache headers (use addHeader).
        response.addHeader("Cache-Control", "post-check=0, pre-check=0");
        // Set standard HTTP/1.0 no-cache header.
        response.setHeader("Pragma", "no-cache");
        // return a jpeg
        response.setContentType("image/jpeg");
        // create the text for the image
        String capText = captchaProducer.createText();
        // store the text in the session
       request.getSession().setAttribute(validateSessionKey, capText);
        // create the image with the text
        BufferedImage bi = captchaProducer.createImage(capText);
        ServletOutputStream out = response.getOutputStream();
        // write the data out
        ImageIO.write(bi, "jpg", out);
        try {
            out.flush();
        } finally {
            out.close();
        }
    }
}

           

繼續閱讀