zhubaomin
2024-09-11 a4636611b429e84fc6abef44dcd005cee69b1858
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package com.dy.pipIrrGlobal.config;
 
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
 
import java.util.Properties;
 
/**
 * @author ZhuBaoMin
 * @date 2024-09-10 11:17
 * @LastEditTime 2024-09-10 11:17
 * @Description Google验证码配置类,配置三种类型的验证码生成器:简单文本验证码、一位数加减乘除验证码、两位数加减乘除验证码
 */
 
@Configuration
public class KaptchaConfig {
    private static KaptchaConfig instance;
 
    /**
     * 验证码配置默认配置
     * @return
     */
    @Bean(name = "captchaProducer")
    public DefaultKaptcha getKaptchaBean() {
        DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
        Properties properties = new Properties();
        // 是否有边框。默认true,可选:yes,no
        properties.setProperty("kaptcha.border", "yes");
        //设置图片边框颜色
        properties.setProperty("kaptcha.border.color", "green");
        // 验证码文本字符颜色。默认Color.BLACK
        properties.setProperty("kaptcha.textproducer.font.color", "black");
        // 文字间隔
        properties.put("kaptcha.textproducer.char.space", "10");
        // 验证码图片宽度。默认200
        properties.setProperty("kaptcha.image.width", "160");
        // 验证码图片高度。默认50
        properties.setProperty("Kkaptcha.image.height", "60");
        // 验证码文本字符大小。默认40
        properties.setProperty("kaptcha.textproducer.font.size", "38");
        // KAPTCHA_SESSION_KEY
        properties.setProperty("Kkaptcha.session.key", "kaptchaCode");
        // 验证码文本字符长度。默认5
        properties.setProperty("kaptcha.textproducer.char.length", "4");
        // 验证码文本字体样式。默认:new Font("Arial", 1, fontSize), new Font("Courier", 1, fontSize)
        properties.setProperty("kaptcha.textproducer.font.names", "Arial,Courier");
        // 干扰实现类
        properties.setProperty("kaptcha.noise.impl", "com.google.code.kaptcha.impl.NoNoise");
        // 图片样式。
        // 水纹:com.google.code.kaptcha.impl.WaterRipple
        // 鱼眼:com.google.code.kaptcha.impl.FishEyeGimpy
        // 阴影:com.google.code.kaptcha.impl.ShadowGimpy
        properties.setProperty("kaptcha.obscurificator.impl", "com.google.code.kaptcha.impl.WaterRipple");
        Config config = new Config(properties);
        defaultKaptcha.setConfig(config);
        return defaultKaptcha;
    }
 
    /**
     * 验证码数学题类配置(一位数加减乘除)
     * @return 配置信息
     */
    @Bean(name = "captchaProducerMathOne")
    public DefaultKaptcha getKaptchaBeanMathOne() {
        DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
        Properties properties = commonConfig("com.dy.pipIrrGlobal.config.KaptchaMathOneTextCreator");
        Config config = new Config(properties);
        defaultKaptcha.setConfig(config);
        return defaultKaptcha;
    }
 
    /**
     * 验证码数学题类配置(两位数的加减乘除)
     * @return 配置信息
     */
    @Bean(name = "captchaProducerMathTwo")
    public DefaultKaptcha getKaptchaBeanMathTwo() {
        DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
        Properties properties = commonConfig("com.dy.pipIrrGlobal.config.KaptchaMathTwoTextCreator");
        Config config = new Config(properties);
        defaultKaptcha.setConfig(config);
        return defaultKaptcha;
    }
 
    /**
     * 算式运算配置的公共配置类
     * @param textImpl 验证码文本生成器
     * @return 配置类
     */
    protected static Properties commonConfig(String textImpl) {
        Properties properties = new Properties();
        // 是否有边框。默认为true,可设置:yes,no
        properties.setProperty("kaptcha.border", "yes");
        // 边框颜色。默认:Color.BLACK
        properties.setProperty("kaptcha.border.color", "105,179,90");
        // 验证码文本字符颜色。默认:Color.BLACK
        properties.setProperty("kaptcha.textproducer.font.color", "blue");
        // 验证码图片宽度。默认:200
        properties.setProperty("kaptcha.image.width", "160");
        // 验证码图片高度。默认:50
        properties.setProperty("kaptcha.image.height", "60");
        // 验证码文本字符大小。默认:40
        properties.setProperty("kaptcha.textproducer.font.size", "35");
        // KAPTCHA_SESSION_KEY
        properties.setProperty("kaptcha.session.key", "kaptchaCodeMath");
        // 验证码文本生成器
        properties.setProperty("kaptcha.textproducer.impl", textImpl);
        // 验证码文本字符间距。默认:2
        properties.setProperty("kaptcha.textproducer.char.space", "6");
        // 验证码文本字符长度。默认:5
        properties.setProperty("kaptcha.textproducer.char.length", "6");
        // 验证码文本字体样式。默认:new Font("Arial", 1, fontSize), new Font("Courier", 1, fontSize)
        properties.setProperty("kaptcha.textproducer.font.names", "Arial,Courier");
        // 验证码噪点颜色。默认:Color.BLACK
        properties.setProperty("kaptcha.noise.color", "white");
        // 干扰实现类
        properties.setProperty("kaptcha.noise.impl", "com.google.code.kaptcha.impl.NoNoise");
        // 图片样式。
        // 水纹:com.google.code.kaptcha.impl.WaterRipple
        // 鱼眼:com.google.code.kaptcha.impl.FishEyeGimpy
        // 阴影:com.google.code.kaptcha.impl.ShadowGimpy
        properties.setProperty("kaptcha.obscurificator.impl", "com.google.code.kaptcha.impl.WaterRipple");
        // 返回生成的配置类
        return properties;
    }
}