CaptchaController.java 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package com.ozs.web.common;
  2. import com.google.code.kaptcha.Producer;
  3. import com.ozs.common.config.BaseConfig;
  4. import com.ozs.common.constant.CacheConstants;
  5. import com.ozs.common.constant.Constants;
  6. import com.ozs.common.core.domain.AjaxResult;
  7. import com.ozs.common.core.redis.RedisCache;
  8. import com.ozs.common.utils.sign.Base64;
  9. import com.ozs.common.utils.uuid.IdUtils;
  10. import com.ozs.system.service.ISysConfigService;
  11. import org.springframework.beans.factory.annotation.Autowired;
  12. import org.springframework.util.FastByteArrayOutputStream;
  13. import org.springframework.web.bind.annotation.GetMapping;
  14. import org.springframework.web.bind.annotation.RestController;
  15. import javax.annotation.Resource;
  16. import javax.imageio.ImageIO;
  17. import javax.servlet.http.HttpServletResponse;
  18. import java.awt.image.BufferedImage;
  19. import java.io.IOException;
  20. import java.util.concurrent.TimeUnit;
  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 ISysConfigService configService;
  37. /**
  38. * 生成验证码
  39. */
  40. @GetMapping("/captchaImage")
  41. public AjaxResult getCode(HttpServletResponse response) throws IOException
  42. {
  43. AjaxResult ajax = AjaxResult.success();
  44. boolean captchaEnabled = configService.selectCaptchaEnabled();
  45. ajax.put("captchaEnabled", captchaEnabled);
  46. if (!captchaEnabled) {
  47. return ajax;
  48. }
  49. // 保存验证码信息
  50. String uuid = IdUtils.simpleUUID();
  51. String verifyKey = CacheConstants.CAPTCHA_CODE_KEY + uuid;
  52. String capStr = null, code = null;
  53. BufferedImage image = null;
  54. // 生成验证码
  55. String captchaType = BaseConfig.getCaptchaType();
  56. if ("math".equals(captchaType))
  57. {
  58. String capText = captchaProducerMath.createText();
  59. capStr = capText.substring(0, capText.lastIndexOf("@"));
  60. code = capText.substring(capText.lastIndexOf("@") + 1);
  61. image = captchaProducerMath.createImage(capStr);
  62. }
  63. else if ("char".equals(captchaType))
  64. {
  65. capStr = code = captchaProducer.createText();
  66. image = captchaProducer.createImage(capStr);
  67. }
  68. redisCache.setCacheObject(verifyKey, code, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);
  69. // 转换流信息写出
  70. FastByteArrayOutputStream os = new FastByteArrayOutputStream();
  71. try
  72. {
  73. ImageIO.write(image, "jpg", os);
  74. }
  75. catch (IOException e)
  76. {
  77. return AjaxResult.error(e.getMessage());
  78. }
  79. ajax.put("uuid", uuid);
  80. ajax.put("img", Base64.encode(os.toByteArray()));
  81. return ajax;
  82. }
  83. }