CaptchaController.java 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package com.ozs.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.util.FastByteArrayOutputStream;
  10. import org.springframework.web.bind.annotation.GetMapping;
  11. import org.springframework.web.bind.annotation.RestController;
  12. import com.google.code.kaptcha.Producer;
  13. import com.ozs.common.config.PurchaseConfig;
  14. import com.ozs.common.constant.CacheConstants;
  15. import com.ozs.common.constant.Constants;
  16. import com.ozs.common.core.domain.AjaxResult;
  17. import com.ozs.common.core.redis.RedisCache;
  18. import com.ozs.common.utils.sign.Base64;
  19. import com.ozs.common.utils.uuid.IdUtils;
  20. import com.ozs.system.service.ISysConfigService;
  21. /**
  22. * 验证码操作处理
  23. *
  24. * @author ruoyi
  25. */
  26. @RestController
  27. public class CaptchaController
  28. {
  29. @Resource(name = "captchaProducer")
  30. private Producer captchaProducer;
  31. @Resource(name = "captchaProducerMath")
  32. private Producer captchaProducerMath;
  33. @Autowired
  34. private RedisCache redisCache;
  35. @Autowired
  36. private PurchaseConfig purchaseConfig;
  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 captchaEnabled = configService.selectCaptchaEnabled();
  47. ajax.put("captchaEnabled", captchaEnabled);
  48. if (!captchaEnabled)
  49. {
  50. return ajax;
  51. }
  52. // 保存验证码信息
  53. String uuid = IdUtils.simpleUUID();
  54. String verifyKey = CacheConstants.CAPTCHA_CODE_KEY + uuid;
  55. String capStr = null, code = null;
  56. BufferedImage image = null;
  57. // 生成验证码
  58. String captchaType = purchaseConfig.getCaptchaType();
  59. if ("math".equals(captchaType))
  60. {
  61. String capText = captchaProducerMath.createText();
  62. capStr = capText.substring(0, capText.lastIndexOf("@"));
  63. code = capText.substring(capText.lastIndexOf("@") + 1);
  64. image = captchaProducerMath.createImage(capStr);
  65. }
  66. else if ("char".equals(captchaType))
  67. {
  68. capStr = code = captchaProducer.createText();
  69. image = captchaProducer.createImage(capStr);
  70. }
  71. redisCache.setCacheObject(verifyKey, code, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);
  72. // 转换流信息写出
  73. FastByteArrayOutputStream os = new FastByteArrayOutputStream();
  74. try
  75. {
  76. ImageIO.write(image, "jpg", os);
  77. }
  78. catch (IOException e)
  79. {
  80. return AjaxResult.error(e.getMessage());
  81. }
  82. ajax.put("uuid", uuid);
  83. ajax.put("img", Base64.encode(os.toByteArray()));
  84. return ajax;
  85. }
  86. }