当前位置:   article > 正文

springboot整合Easy-Captcha_spring 项目整合easy-captcha

spring 项目整合easy-captcha

前言

Java图形验证码,支持gif、中文、算术等类型。

引入依赖

 <!-- Java图形验证码 -->
  <dependency>
       <groupId>com.github.whvcse</groupId>
       <artifactId>easy-captcha</artifactId>
       <version>1.6.2</version>
   </dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

配置(application.yml)

#图形验证码 配置
easyCaptcha:
  enabled: true # 开启验证码
  codeType: spec # 验证码类型 查看CaptchaCodeEnum类
  expiration: 2 # 验证码过期时间 单位分钟
  length: 4 # 验证码(内容)长度
  width: 111 # 验证码宽度
  height: 36 # 验证码高度
  fontName:  # 验证码字体
  fontSize: 25 # 验证码字体大小
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

配置(工具类)

1.CaptchaEnum

public enum CaptchaEnum {
    spec, // 字母
    arithmetic, // 算术
    chinese, // 汉字
    chinese_gif, // 中文闪图
    gif, // 闪图
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

EasyCaptchaConfig

/**
 * TODO 图片验证码配置
 */
@Data
@Component
public class EasyCaptchaConfig {
    /**
     * 是否启用验证码
     */
    @Value("${easyCaptcha.enabled}")
    private Boolean enabled = true;

    /**
     * 验证码配置
     */
    @Value("${easyCaptcha.codeType}")
    private CaptchaEnum codeType;
    /**
     * 验证码有效期 分钟
     */
    @Value("${easyCaptcha.expiration}")
    private Long expiration = 2L;
    /**
     * 验证码内容长度
     */
    @Value("${easyCaptcha.length}")
    private int length = 2;
    /**
     * 验证码宽度
     */
    @Value("${easyCaptcha.width}")
    private int width=111;
    /**
     * 验证码高度
     */
    @Value("${easyCaptcha.height}")
    private int height = 36;
    /**
     * 验证码字体
     */
    @Value("${easyCaptcha.fontName}")
    private String fontName;
    /**
     * 字体大小
     */
    @Value("${easyCaptcha.fontSize}")
    private int fontSize = 25;


    public CaptchaEnum getCodeType() {
        return codeType;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53

EasyCaptChaUtil

/**
 * TODO 图形验证码工具类
 */
@Component
public class EasyCaptChaUtil {
    @Autowired
    private EasyCaptchaConfig easyCaptchaConfig; //验证码配置

    /**
     * 获取验证码
     */
    public Captcha getCaptcha() {
        if (Objects.isNull(easyCaptchaConfig)) { //验证码配置信息为空
            easyCaptchaConfig = new EasyCaptchaConfig(); //new EasyCaptchaConfig()
            if (Objects.isNull(easyCaptchaConfig.getCodeType())) { //验证码类型为空
                easyCaptchaConfig.setCodeType(CaptchaEnum.arithmetic);//默认算术类型
            }
        }
        return switchCaptcha(easyCaptchaConfig);//返回验证码
    }

    /**
     * 生成验证码
     *
     * @param easyCaptcha 验证码配置
     * @return Captcha
     */
    private Captcha switchCaptcha(EasyCaptchaConfig easyCaptcha) {
        Captcha captcha;
        synchronized (this) { //一个线程访问一个对象中的synchronized(this)同步代码块时,其他试图访问该对象的线程将被阻塞,直到该线程执行完毕
            switch (easyCaptcha.getCodeType()) {
                case arithmetic:
                    // 算术类型 https://gitee.com/whvse/EasyCaptcha
                    captcha = new FixedArithmeticCaptcha(easyCaptchaConfig.getWidth(), easyCaptchaConfig.getHeight());
                    // 几位数运算,默认是两位
                    captcha.setLen(easyCaptchaConfig.getLength());
                    break;
                case chinese: //中文类型
                    captcha = new ChineseCaptcha(easyCaptchaConfig.getWidth(), easyCaptchaConfig.getHeight());
                    captcha.setLen(easyCaptchaConfig.getLength());
                    break;
                case chinese_gif: //中文gif类型
                    captcha = new ChineseGifCaptcha(easyCaptchaConfig.getWidth(), easyCaptchaConfig.getHeight());
                    captcha.setLen(easyCaptchaConfig.getLength());
                    break;
                case gif://gif类型
                    captcha = new GifCaptcha(easyCaptchaConfig.getWidth(), easyCaptchaConfig.getHeight());
                    captcha.setLen(easyCaptchaConfig.getLength());
                    break;
                case spec://字母
                    captcha = new SpecCaptcha(easyCaptchaConfig.getWidth(), easyCaptchaConfig.getHeight());
                    captcha.setLen(easyCaptchaConfig.getLength());
                    break;
                default:
                    throw new RuntimeException("验证码配置信息错误!正确配置查看 LoginCodeEnum ");
            }
        }
        if(StringUtils.isNotBlank(easyCaptchaConfig.getFontName())){//当字体为空
            // 设置默认字体
            captcha.setFont(new Font(easyCaptchaConfig.getFontName(), Font.PLAIN, easyCaptchaConfig.getFontSize()));
        }
        return captcha;
    }




   public static class FixedArithmeticCaptcha extends ArithmeticCaptcha {
        public FixedArithmeticCaptcha(int width, int height) {
            super(width, height);
        }

        @Override
        protected char[] alphas() {
            // 生成随机数字和运算符
            int n1 = num(1, 10), n2 = num(1, 10);
            int opt = num(3);

            // 计算结果
            int res = new int[]{n1 + n2, n1 - n2, n1 * n2}[opt];
            // 转换为字符运算符
            char optChar = "+-x".charAt(opt);

            this.setArithmeticString(String.format("%s%c%s=?", n1, optChar, n2));
            this.chars = String.valueOf(res);

            return chars.toCharArray();
        }
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91

使用

public ResponseEntity test() {
        Captcha captcha = easyCaptChaUtil.getCaptcha();//获取验证码
        String uuid = UUID.randomUUID().toString();//生成随机数
        String captchaValue = captcha.text(); //获取验证码的值
        //当验证码类型为 arithmetic时且长度 >= 2 时,captcha.text()的结果有几率为浮点型
        if (captcha.getCharType() - 1 == CaptchaEnum.arithmetic.ordinal() && captchaValue.contains(".")) {//验证码类型为算术类型
            captchaValue = captchaValue.split("\\.")[0]; //截取整数部分
        }
        Map map = new HashMap();
        map.put("uuid", uuid);
        map.put("captcha", captcha.toBase64());
        map.put("captchaValue", captchaValue);
        return ResponseModel.result(ResUtil.success(map));
    } 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/46510
推荐阅读
相关标签
  

闽ICP备14008679号