Blame view

ruoyi-admin/src/main/java/com/ruoyi/web/controller/common/CaptchaController.java 2.72 KB
cee572f2   RuoYi   若依 3.0
1
  package com.ruoyi.web.controller.common;

46444bd0   RuoYi   RuoYi-Vue 1.0
2
  

df3ef54b   RuoYi   验证码类型支持(数组计算、字符验证)
3
  import java.awt.image.BufferedImage;

46444bd0   RuoYi   RuoYi-Vue 1.0
4
5
  import java.io.IOException;

  import java.util.concurrent.TimeUnit;

df3ef54b   RuoYi   验证码类型支持(数组计算、字符验证)
6
7
  import javax.annotation.Resource;

  import javax.imageio.ImageIO;

46444bd0   RuoYi   RuoYi-Vue 1.0
8
9
  import javax.servlet.http.HttpServletResponse;

  import org.springframework.beans.factory.annotation.Autowired;

df3ef54b   RuoYi   验证码类型支持(数组计算、字符验证)
10
11
  import org.springframework.beans.factory.annotation.Value;

  import org.springframework.util.FastByteArrayOutputStream;

46444bd0   RuoYi   RuoYi-Vue 1.0
12
13
  import org.springframework.web.bind.annotation.GetMapping;

  import org.springframework.web.bind.annotation.RestController;

df3ef54b   RuoYi   验证码类型支持(数组计算、字符验证)
14
  import com.google.code.kaptcha.Producer;

46444bd0   RuoYi   RuoYi-Vue 1.0
15
  import com.ruoyi.common.constant.Constants;

cee572f2   RuoYi   若依 3.0
16
17
  import com.ruoyi.common.core.domain.AjaxResult;

  import com.ruoyi.common.core.redis.RedisCache;

46444bd0   RuoYi   RuoYi-Vue 1.0
18
  import com.ruoyi.common.utils.sign.Base64;

cee572f2   RuoYi   若依 3.0
19
  import com.ruoyi.common.utils.uuid.IdUtils;

46444bd0   RuoYi   RuoYi-Vue 1.0
20
21
22
23
24
25
26
27
28
  

  /**

   * 验证码操作处理

   * 

   * @author ruoyi

   */

  @RestController

  public class CaptchaController

  {

df3ef54b   RuoYi   验证码类型支持(数组计算、字符验证)
29
30
31
32
33
34
      @Resource(name = "captchaProducer")

      private Producer captchaProducer;

  

      @Resource(name = "captchaProducerMath")

      private Producer captchaProducerMath;

  

46444bd0   RuoYi   RuoYi-Vue 1.0
35
36
      @Autowired

      private RedisCache redisCache;

df3ef54b   RuoYi   验证码类型支持(数组计算、字符验证)
37
38
39
40
      

      // 验证码类型

      @Value("${ruoyi.captchaType}")

      private String captchaType;

46444bd0   RuoYi   RuoYi-Vue 1.0
41
42
43
44
45
46
47
  

      /**

       * 生成验证码

       */

      @GetMapping("/captchaImage")

      public AjaxResult getCode(HttpServletResponse response) throws IOException

      {

df3ef54b   RuoYi   验证码类型支持(数组计算、字符验证)
48
          // 保存验证码信息

46444bd0   RuoYi   RuoYi-Vue 1.0
49
50
51
          String uuid = IdUtils.simpleUUID();

          String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;

  

df3ef54b   RuoYi   验证码类型支持(数组计算、字符验证)
52
53
54
55
56
          String capStr = null, code = null;

          BufferedImage image = null;

  

          // 生成验证码

          if ("math".equals(captchaType))

46444bd0   RuoYi   RuoYi-Vue 1.0
57
          {

df3ef54b   RuoYi   验证码类型支持(数组计算、字符验证)
58
59
60
61
              String capText = captchaProducerMath.createText();

              capStr = capText.substring(0, capText.lastIndexOf("@"));

              code = capText.substring(capText.lastIndexOf("@") + 1);

              image = captchaProducerMath.createImage(capStr);

46444bd0   RuoYi   RuoYi-Vue 1.0
62
          }

df3ef54b   RuoYi   验证码类型支持(数组计算、字符验证)
63
          else if ("char".equals(captchaType))

46444bd0   RuoYi   RuoYi-Vue 1.0
64
          {

df3ef54b   RuoYi   验证码类型支持(数组计算、字符验证)
65
66
              capStr = code = captchaProducer.createText();

              image = captchaProducer.createImage(capStr);

46444bd0   RuoYi   RuoYi-Vue 1.0
67
          }

df3ef54b   RuoYi   验证码类型支持(数组计算、字符验证)
68
69
70
71
72
  

          redisCache.setCacheObject(verifyKey, code, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);

          // 转换流信息写出

          FastByteArrayOutputStream os = new FastByteArrayOutputStream();

          try

46444bd0   RuoYi   RuoYi-Vue 1.0
73
          {

df3ef54b   RuoYi   验证码类型支持(数组计算、字符验证)
74
              ImageIO.write(image, "jpg", os);

46444bd0   RuoYi   RuoYi-Vue 1.0
75
          }

df3ef54b   RuoYi   验证码类型支持(数组计算、字符验证)
76
77
78
79
80
81
82
83
84
          catch (IOException e)

          {

              return AjaxResult.error(e.getMessage());

          }

  

          AjaxResult ajax = AjaxResult.success();

          ajax.put("uuid", uuid);

          ajax.put("img", Base64.encode(os.toByteArray()));

          return ajax;

46444bd0   RuoYi   RuoYi-Vue 1.0
85
86
      }

  }