天天看點

springSecurity/springboot生成圖檔驗證碼 4 種方式,實作及比較

springSecurity/springboot生成圖檔驗證碼4種方式,實作及比較

  • ​​寫在前面​​
  • ​​一、JDK原生包中Graphics對象,(java.awt.*),不需額外引包​​
  • ​​1.1、封裝對象 ImageCode​​
  • ​​1.2、生成圖檔驗證碼,構造到上文建立的對象中​​
  • ​​1.3、圖檔驗證碼,如下,具體大小,參數,圖紋,背景等可細調整​​
  • ​​二、google 封裝的開源包,引入相關依賴​​
  • ​​2.1、需要配置類​​
  • ​​2.1、代碼生成,注入SpringBean,測試​​
  • ​​2.3、圖檔驗證碼,如下,也可細調​​
  • ​​三、Hutool 開源包,引入相關依賴​​
  • ​​3.1、代碼生成​​
  • ​​3.2、圖檔驗證碼,如下,也可細調​​
  • ​​四、其他開源包,引入相關依賴​​
  • ​​4.1、代碼生成​​
  • ​​4.2、圖檔驗證碼,如下,也可細調​​
  • ​​五、關于驗證碼的校驗邏輯,已補充​​
  • ​​5.1、已補充,[點選-博文連結](javascript:void(0))​​

寫在前面

這裡寫了三種實作,可根據實際需要,選擇之一,還有這裡隻是生成了驗證碼,具體校驗方式可通過session,Redis等作存儲、校驗,這裡就不介紹太多。

一、JDK原生包中Graphics對象,(java.awt.*),不需額外引包

1.1、封裝對象 ImageCode

@Data
public class ImageCode {

    private BufferedImage image;
    private String code;
    private LocalDateTime expireTime;

    public ImageCode(BufferedImage image, String code, int expireIn) {
        this.image = image;
        this.code = code;
        this.expireTime = LocalDateTime.now().plusSeconds(expireIn);
    }

    public ImageCode(BufferedImage image, String code, LocalDateTime expireTime) {
        this.image = image;
        this.code = code;
        this.expireTime = expireTime;
    }

    public boolean isExpire() {
        return LocalDateTime.now().isAfter(expireTime);
    }

}      

1.2、生成圖檔驗證碼,構造到上文建立的對象中

@Test
    public void t1() throws IOException {
        ImageCode imageCode = createImageCode();

//        寫到控制台亂碼
//        ImageIO.write(imageCode.getImage(), "jpeg", System.out);

        ImageIO.write(imageCode.getImage(), "jpeg", Files.newOutputStream(new File("d:/ee.jpeg").toPath()));
    }

    private ImageCode createImageCode() {

        int width = 120; // 驗證碼圖檔寬度
        int height = 45; // 驗證碼圖檔長度
        int length = 4; // 驗證碼位數
        int expireIn = 60; // 驗證碼有效時間 60s

        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

        Graphics g = image.getGraphics();

        Random random = new Random();

        g.setColor(getRandColor(185, 250));
        g.fillRect(0, 0, width, height);
        g.setFont(new Font("Times New Roman", Font.ITALIC, 35));
        g.setColor(getRandColor(160, 200));
        for (int i = 0; i < 155; i++) {
            int x = random.nextInt(width);
            int y = random.nextInt(height);
            int xl = random.nextInt(12);
            int yl = random.nextInt(12);
            g.drawLine(x, y, x + xl, y + yl);
        }

        StringBuilder sRand = new StringBuilder();
        for (int i = 0; i < length; i++) {
            String rand = String.valueOf(random.nextInt(10));
            sRand.append(rand);
            g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110)));
            g.drawString(rand, 25 * i + 12, 32);
        }

        g.dispose();

        return new ImageCode(image, sRand.toString(), expireIn);
    }

    private Color getRandColor(int fc, int bc) {
        Random random = new Random();
        if (fc > 255)
            fc = 255;

        if (bc > 255)
            bc = 255;
        int r = fc + random.nextInt(bc - fc);
        int g = fc + random.nextInt(bc - fc);
        int b = fc + random.nextInt(bc - fc);
        return new Color(r, g, b);
    }      

1.3、圖檔驗證碼,如下,具體大小,參數,圖紋,背景等可細調整

springSecurity/springboot生成圖檔驗證碼 4 種方式,實作及比較

springSecurity/springboot生成圖檔驗證碼 4 種方式,實作及比較

springSecurity/springboot生成圖檔驗證碼 4 種方式,實作及比較

二、google 封裝的開源包,引入相關依賴

先引入依賴

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

2.1、需要配置類

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

import java.util.Properties;

@Configuration
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.image.height","50");
        kaptchaProperties.put("kaptcha.image.width","150");
        kaptchaProperties.put("kaptcha.obscurificator.impl","com.google.code.kaptcha.impl.ShadowGimpy");
        kaptchaProperties.put("kaptcha.textproducer.font.color","black");
        kaptchaProperties.put("kaptcha.textproducer.font.size","40");
        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","acdefhkmnprtwxy2345678");

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

2.1、代碼生成,注入SpringBean,測試

@Autowired
    private Producer captchaProducer;

 @GetMapping("/captcha")
    public void captcha() throws IOException {

        // create the text for the image  
        String capText = captchaProducer.createText();

        // create the image with the text  
        BufferedImage bi = captchaProducer.createImage(capText);

        ImageIO.write(bi, "jpeg", Files.newOutputStream(new File("d:/google.jpeg").toPath()));
    }      

2.3、圖檔驗證碼,如下,也可細調

springSecurity/springboot生成圖檔驗證碼 4 種方式,實作及比較

|

springSecurity/springboot生成圖檔驗證碼 4 種方式,實作及比較

|

springSecurity/springboot生成圖檔驗證碼 4 種方式,實作及比較

三、Hutool 開源包,引入相關依賴

先引入依賴

<dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.1.4</version>
        </dependency>      

3.1、代碼生成

@Test
    public void t4() throws IOException {
//定義圖形驗證碼的長、寬、驗證碼字元數、幹擾元素個數
        CircleCaptcha captcha = CaptchaUtil.createCircleCaptcha(200, 100, 4, 20);
//CircleCaptcha captcha = new CircleCaptcha(200, 100, 4, 20);
//圖形驗證碼寫出,可以寫出到檔案,也可以寫出到流
        captcha.write(Files.newOutputStream(new File("d:/hutool.jpeg").toPath()));
    }      

3.2、圖檔驗證碼,如下,也可細調

springSecurity/springboot生成圖檔驗證碼 4 種方式,實作及比較

|||

springSecurity/springboot生成圖檔驗證碼 4 種方式,實作及比較

|||

springSecurity/springboot生成圖檔驗證碼 4 種方式,實作及比較

四、其他開源包,引入相關依賴

先引入依賴

<dependency>
            <groupId>com.github.whvcse</groupId>
            <artifactId>easy-captcha</artifactId>
            <version>1.6.2</version>
</dependency>      

4.1、代碼生成

@Test
    public void t2() throws IOException {

        SpecCaptcha specCaptcha = new SpecCaptcha(129, 48, 4);
        // 設定字型
        specCaptcha.setFont(new Font("Times New Roman", Font.ITALIC, 34));
        // 設定類型
        specCaptcha.setCharType(Captcha.TYPE_NUM_AND_UPPER);
        specCaptcha.out(Files.newOutputStream(new File("d:/ff.jpeg").toPath()));
    }      

4.2、圖檔驗證碼,如下,也可細調

springSecurity/springboot生成圖檔驗證碼 4 種方式,實作及比較

|

springSecurity/springboot生成圖檔驗證碼 4 種方式,實作及比較

五、關于驗證碼的校驗邏輯,已補充

5.1、已補充,​​點選-博文連結​​