Explorar el Código

验证码类型支持(数组计算、字符验证)

RuoYi hace 5 años
padre
commit
41cf67da6d

+ 45 - 19
ruoyi-gateway/src/main/java/com/ruoyi/gateway/config/CaptchaConfig.java

@@ -5,6 +5,7 @@ import org.springframework.context.annotation.Bean;
5 5
 import org.springframework.context.annotation.Configuration;
6 6
 import com.google.code.kaptcha.impl.DefaultKaptcha;
7 7
 import com.google.code.kaptcha.util.Config;
8
+import static com.google.code.kaptcha.Constants.*;
8 9
 
9 10
 /**
10 11
  * 验证码配置
@@ -14,42 +15,67 @@ import com.google.code.kaptcha.util.Config;
14 15
 @Configuration
15 16
 public class CaptchaConfig
16 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
+    
17 46
     @Bean(name = "captchaProducerMath")
18 47
     public DefaultKaptcha getKaptchaBeanMath()
19 48
     {
20 49
         DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
21 50
         Properties properties = new Properties();
22 51
         // 是否有边框 默认为true 我们可以自己设置yes,no
23
-        properties.setProperty("kaptcha.border", "yes");
52
+        properties.setProperty(KAPTCHA_BORDER, "yes");
24 53
         // 边框颜色 默认为Color.BLACK
25
-        properties.setProperty("kaptcha.border.color", "105,179,90");
54
+        properties.setProperty(KAPTCHA_BORDER_COLOR, "105,179,90");
26 55
         // 验证码文本字符颜色 默认为Color.BLACK
27
-        properties.setProperty("kaptcha.textproducer.font.color", "blue");
56
+        properties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_COLOR, "blue");
28 57
         // 验证码图片宽度 默认为200
29
-        properties.setProperty("kaptcha.image.width", "160");
58
+        properties.setProperty(KAPTCHA_IMAGE_WIDTH, "160");
30 59
         // 验证码图片高度 默认为50
31
-        properties.setProperty("kaptcha.image.height", "60");
60
+        properties.setProperty(KAPTCHA_IMAGE_HEIGHT, "60");
32 61
         // 验证码文本字符大小 默认为40
33
-        properties.setProperty("kaptcha.textproducer.font.size", "35");
62
+        properties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_SIZE, "35");
34 63
         // KAPTCHA_SESSION_KEY
35
-        properties.setProperty("kaptcha.session.key", "kaptchaCodeMath");
64
+        properties.setProperty(KAPTCHA_SESSION_CONFIG_KEY, "kaptchaCodeMath");
36 65
         // 验证码文本生成器
37
-        properties.setProperty("kaptcha.textproducer.impl", "com.ruoyi.gateway.config.KaptchaTextCreator");
66
+        properties.setProperty(KAPTCHA_TEXTPRODUCER_IMPL, "com.ruoyi.gateway.config.KaptchaTextCreator");
38 67
         // 验证码文本字符间距 默认为2
39
-        properties.setProperty("kaptcha.textproducer.char.space", "3");
68
+        properties.setProperty(KAPTCHA_TEXTPRODUCER_CHAR_SPACE, "3");
40 69
         // 验证码文本字符长度 默认为5
41
-        properties.setProperty("kaptcha.textproducer.char.length", "6");
42
-        // 验证码文本字体样式 默认为new Font("Arial", 1, fontSize), new Font("Courier", 1,
43
-        // fontSize)
44
-        properties.setProperty("kaptcha.textproducer.font.names", "Arial,Courier");
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");
45 73
         // 验证码噪点颜色 默认为Color.BLACK
46
-        properties.setProperty("kaptcha.noise.color", "white");
74
+        properties.setProperty(KAPTCHA_NOISE_COLOR, "white");
47 75
         // 干扰实现类
48
-        properties.setProperty("kaptcha.noise.impl", "com.google.code.kaptcha.impl.NoNoise");
49
-        // 图片样式 水纹com.google.code.kaptcha.impl.WaterRipple
50
-        // 鱼眼com.google.code.kaptcha.impl.FishEyeGimpy
51
-        // 阴影com.google.code.kaptcha.impl.ShadowGimpy
52
-        properties.setProperty("kaptcha.obscurificator.impl", "com.google.code.kaptcha.impl.ShadowGimpy");
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");
53 79
         Config config = new Config(properties);
54 80
         defaultKaptcha.setConfig(config);
55 81
         return defaultKaptcha;

+ 27 - 8
ruoyi-gateway/src/main/java/com/ruoyi/gateway/service/impl/ValidateCodeServiceImpl.java

@@ -3,6 +3,7 @@ package com.ruoyi.gateway.service.impl;
3 3
 import java.awt.image.BufferedImage;
4 4
 import java.io.IOException;
5 5
 import java.util.concurrent.TimeUnit;
6
+import javax.annotation.Resource;
6 7
 import javax.imageio.ImageIO;
7 8
 import org.springframework.beans.factory.annotation.Autowired;
8 9
 import org.springframework.stereotype.Service;
@@ -25,28 +26,46 @@ import com.ruoyi.gateway.service.ValidateCodeService;
25 26
 @Service
26 27
 public class ValidateCodeServiceImpl implements ValidateCodeService
27 28
 {
28
-    @Autowired
29
-    private Producer producer;
29
+    @Resource(name = "captchaProducer")
30
+    private Producer captchaProducer;
31
+
32
+    @Resource(name = "captchaProducerMath")
33
+    private Producer captchaProducerMath;
30 34
 
31 35
     @Autowired
32 36
     private RedisService redisService;
33 37
 
38
+    // 验证码类型
39
+    private String captchaType = "math";
40
+
34 41
     /**
35 42
      * 生成验证码
36 43
      */
37 44
     @Override
38 45
     public AjaxResult createCapcha() throws IOException, CaptchaException
39 46
     {
40
-        // 生成验证码
41
-        String capText = producer.createText();
42
-        String capStr = capText.substring(0, capText.lastIndexOf("@"));
43
-        String verifyCode = capText.substring(capText.lastIndexOf("@") + 1);
44
-        BufferedImage image = producer.createImage(capStr);
45 47
         // 保存验证码信息
46 48
         String uuid = IdUtils.simpleUUID();
47 49
         String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;
48 50
 
49
-        redisService.setCacheObject(verifyKey, verifyCode, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);
51
+        String capStr = null, code = null;
52
+        BufferedImage image = null;
53
+
54
+        // 生成验证码
55
+        if ("math".equals(captchaType))
56
+        {
57
+            String capText = captchaProducerMath.createText();
58
+            capStr = capText.substring(0, capText.lastIndexOf("@"));
59
+            code = capText.substring(capText.lastIndexOf("@") + 1);
60
+            image = captchaProducerMath.createImage(capStr);
61
+        }
62
+        else if ("char".equals(captchaType))
63
+        {
64
+            capStr = code = captchaProducer.createText();
65
+            image = captchaProducer.createImage(capStr);
66
+        }
67
+
68
+        redisService.setCacheObject(verifyKey, code, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);
50 69
         // 转换流信息写出
51 70
         FastByteArrayOutputStream os = new FastByteArrayOutputStream();
52 71
         try