CaptchaController.java 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. @Resource(name = "captchaProducer")
  29. private Producer captchaProducer;
  30. @Resource(name = "captchaProducerMath")
  31. private Producer captchaProducerMath;
  32. @Autowired
  33. private RedisCache redisCache;
  34. @Autowired
  35. private PurchaseConfig purchaseConfig;
  36. @Autowired
  37. private ISysConfigService configService;
  38. /**
  39. * 生成验证码
  40. */
  41. @GetMapping("/captchaImage")
  42. public AjaxResult getCode(HttpServletResponse response) throws IOException {
  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 = purchaseConfig.getCaptchaType();
  56. if ("math".equals(captchaType)) {
  57. String capText = captchaProducerMath.createText();
  58. capStr = capText.substring(0, capText.lastIndexOf("@"));
  59. code = capText.substring(capText.lastIndexOf("@") + 1);
  60. image = captchaProducerMath.createImage(capStr);
  61. } else if ("char".equals(captchaType)) {
  62. capStr = code = captchaProducer.createText();
  63. image = captchaProducer.createImage(capStr);
  64. }
  65. redisCache.setCacheObject(verifyKey, code, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);
  66. // 转换流信息写出
  67. FastByteArrayOutputStream os = new FastByteArrayOutputStream();
  68. try {
  69. ImageIO.write(image, "jpg", os);
  70. } catch (IOException e) {
  71. return AjaxResult.error(e.getMessage());
  72. }
  73. ajax.put("uuid", uuid);
  74. ajax.put("img", Base64.encode(os.toByteArray()));
  75. return ajax;
  76. }
  77. }