懒羊羊
2024-01-31 e57a8990ae56f657a59c435a0613c5f7a8728003
提交 | 用户 | 时间
e57a89 1 package com.jcdm.web.controller.common;
2
3 import java.awt.image.BufferedImage;
4 import java.io.IOException;
5 import java.util.concurrent.TimeUnit;
6 import javax.annotation.Resource;
7 import javax.imageio.ImageIO;
8 import javax.servlet.http.HttpServletResponse;
9 import org.springframework.beans.factory.annotation.Autowired;
10 import org.springframework.util.FastByteArrayOutputStream;
11 import org.springframework.web.bind.annotation.GetMapping;
12 import org.springframework.web.bind.annotation.RestController;
13 import com.google.code.kaptcha.Producer;
14 import com.jcdm.common.config.MesConfig;
15 import com.jcdm.common.constant.CacheConstants;
16 import com.jcdm.common.constant.Constants;
17 import com.jcdm.common.core.domain.AjaxResult;
18 import com.jcdm.common.core.redis.RedisCache;
19 import com.jcdm.common.utils.sign.Base64;
20 import com.jcdm.common.utils.uuid.IdUtils;
21 import com.jcdm.system.service.ISysConfigService;
22
23 /**
24  * 验证码操作处理
25  * 
26  * @author jc
27  */
28 @RestController
29 public class CaptchaController
30 {
31     @Resource(name = "captchaProducer")
32     private Producer captchaProducer;
33
34     @Resource(name = "captchaProducerMath")
35     private Producer captchaProducerMath;
36
37     @Autowired
38     private RedisCache redisCache;
39     
40     @Autowired
41     private ISysConfigService configService;
42     /**
43      * 生成验证码
44      */
45     @GetMapping("/captchaImage")
46     public AjaxResult getCode(HttpServletResponse response) throws IOException
47     {
48         AjaxResult ajax = AjaxResult.success();
49         boolean captchaEnabled = configService.selectCaptchaEnabled();
50         ajax.put("captchaEnabled", captchaEnabled);
51         if (!captchaEnabled)
52         {
53             return ajax;
54         }
55
56         // 保存验证码信息
57         String uuid = IdUtils.simpleUUID();
58         String verifyKey = CacheConstants.CAPTCHA_CODE_KEY + uuid;
59
60         String capStr = null, code = null;
61         BufferedImage image = null;
62
63         // 生成验证码
64         String captchaType = MesConfig.getCaptchaType();
65         if ("math".equals(captchaType))
66         {
67             String capText = captchaProducerMath.createText();
68             capStr = capText.substring(0, capText.lastIndexOf("@"));
69             code = capText.substring(capText.lastIndexOf("@") + 1);
70             image = captchaProducerMath.createImage(capStr);
71         }
72         else if ("char".equals(captchaType))
73         {
74             capStr = code = captchaProducer.createText();
75             image = captchaProducer.createImage(capStr);
76         }
77
78         redisCache.setCacheObject(verifyKey, code, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);
79         // 转换流信息写出
80         FastByteArrayOutputStream os = new FastByteArrayOutputStream();
81         try
82         {
83             ImageIO.write(image, "jpg", os);
84         }
85         catch (IOException e)
86         {
87             return AjaxResult.error(e.getMessage());
88         }
89
90         ajax.put("uuid", uuid);
91         ajax.put("img", Base64.encode(os.toByteArray()));
92         return ajax;
93     }
94 }