懒羊羊
2024-01-31 e57a8990ae56f657a59c435a0613c5f7a8728003
提交 | 用户 | 时间
e57a89 1 package com.jcdm.framework.config;
2
3 import java.util.Properties;
4 import org.springframework.context.annotation.Bean;
5 import org.springframework.context.annotation.Configuration;
6 import com.google.code.kaptcha.impl.DefaultKaptcha;
7 import com.google.code.kaptcha.util.Config;
8 import static com.google.code.kaptcha.Constants.*;
9
10 /**
11  * 验证码配置
12  * 
13  * @author jc
14  */
15 @Configuration
16 public class CaptchaConfig
17 {
18     @Bean(name = "captchaProducer")
19     public DefaultKaptcha getKaptchaBean()
20     {
21         DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
22         Properties properties = new Properties();
23         // 是否有边框 默认为true 我们可以自己设置yes,no
24         properties.setProperty(KAPTCHA_BORDER, "yes");
25         // 验证码文本字符颜色 默认为Color.BLACK
26         properties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_COLOR, "black");
27         // 验证码图片宽度 默认为200
28         properties.setProperty(KAPTCHA_IMAGE_WIDTH, "160");
29         // 验证码图片高度 默认为50
30         properties.setProperty(KAPTCHA_IMAGE_HEIGHT, "60");
31         // 验证码文本字符大小 默认为40
32         properties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_SIZE, "38");
33         // KAPTCHA_SESSION_KEY
34         properties.setProperty(KAPTCHA_SESSION_CONFIG_KEY, "kaptchaCode");
35         // 验证码文本字符长度 默认为5
36         properties.setProperty(KAPTCHA_TEXTPRODUCER_CHAR_LENGTH, "4");
37         // 验证码文本字体样式 默认为new Font("Arial", 1, fontSize), new Font("Courier", 1, fontSize)
38         properties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_NAMES, "Arial,Courier");
39         // 图片样式 水纹com.google.code.kaptcha.impl.WaterRipple 鱼眼com.google.code.kaptcha.impl.FishEyeGimpy 阴影com.google.code.kaptcha.impl.ShadowGimpy
40         properties.setProperty(KAPTCHA_OBSCURIFICATOR_IMPL, "com.google.code.kaptcha.impl.ShadowGimpy");
41         Config config = new Config(properties);
42         defaultKaptcha.setConfig(config);
43         return defaultKaptcha;
44     }
45
46     @Bean(name = "captchaProducerMath")
47     public DefaultKaptcha getKaptchaBeanMath()
48     {
49         DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
50         Properties properties = new Properties();
51         // 是否有边框 默认为true 我们可以自己设置yes,no
52         properties.setProperty(KAPTCHA_BORDER, "yes");
53         // 边框颜色 默认为Color.BLACK
54         properties.setProperty(KAPTCHA_BORDER_COLOR, "105,179,90");
55         // 验证码文本字符颜色 默认为Color.BLACK
56         properties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_COLOR, "blue");
57         // 验证码图片宽度 默认为200
58         properties.setProperty(KAPTCHA_IMAGE_WIDTH, "160");
59         // 验证码图片高度 默认为50
60         properties.setProperty(KAPTCHA_IMAGE_HEIGHT, "60");
61         // 验证码文本字符大小 默认为40
62         properties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_SIZE, "35");
63         // KAPTCHA_SESSION_KEY
64         properties.setProperty(KAPTCHA_SESSION_CONFIG_KEY, "kaptchaCodeMath");
65         // 验证码文本生成器
66         properties.setProperty(KAPTCHA_TEXTPRODUCER_IMPL, "com.jcdm.framework.config.KaptchaTextCreator");
67         // 验证码文本字符间距 默认为2
68         properties.setProperty(KAPTCHA_TEXTPRODUCER_CHAR_SPACE, "3");
69         // 验证码文本字符长度 默认为5
70         properties.setProperty(KAPTCHA_TEXTPRODUCER_CHAR_LENGTH, "6");
71         // 验证码文本字体样式 默认为new Font("Arial", 1, fontSize), new Font("Courier", 1, fontSize)
72         properties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_NAMES, "Arial,Courier");
73         // 验证码噪点颜色 默认为Color.BLACK
74         properties.setProperty(KAPTCHA_NOISE_COLOR, "white");
75         // 干扰实现类
76         properties.setProperty(KAPTCHA_NOISE_IMPL, "com.google.code.kaptcha.impl.NoNoise");
77         // 图片样式 水纹com.google.code.kaptcha.impl.WaterRipple 鱼眼com.google.code.kaptcha.impl.FishEyeGimpy 阴影com.google.code.kaptcha.impl.ShadowGimpy
78         properties.setProperty(KAPTCHA_OBSCURIFICATOR_IMPL, "com.google.code.kaptcha.impl.ShadowGimpy");
79         Config config = new Config(properties);
80         defaultKaptcha.setConfig(config);
81         return defaultKaptcha;
82     }
83 }