CaptchaController.java 3.0 KB

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