CaptchaController.java 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. package com.ruoyi.web.controller.common;
  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.beans.factory.annotation.Value;
  11. import org.springframework.util.FastByteArrayOutputStream;
  12. import org.springframework.web.bind.annotation.GetMapping;
  13. import org.springframework.web.bind.annotation.RestController;
  14. import com.google.code.kaptcha.Producer;
  15. import com.ruoyi.common.constant.Constants;
  16. import com.ruoyi.common.core.domain.AjaxResult;
  17. import com.ruoyi.common.utils.sign.Base64;
  18. import com.ruoyi.common.utils.uuid.IdUtils;
  19. */
  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. */
  38. /**
  39. * 生成验证码
  40. *//*
  41. @GetMapping("/captchaImage")
  42. public AjaxResult getCode(HttpServletResponse response) throws IOException
  43. {
  44. // 保存验证码信息
  45. String uuid = IdUtils.simpleUUID();
  46. String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;
  47. String capStr = null, code = null;
  48. BufferedImage image = null;
  49. // 生成验证码
  50. if ("math".equals(captchaType))
  51. {
  52. String capText = captchaProducerMath.createText();
  53. capStr = capText.substring(0, capText.lastIndexOf("@"));
  54. code = capText.substring(capText.lastIndexOf("@") + 1);
  55. image = captchaProducerMath.createImage(capStr);
  56. }
  57. else if ("char".equals(captchaType))
  58. {
  59. capStr = code = captchaProducer.createText();
  60. image = captchaProducer.createImage(capStr);
  61. }
  62. redisCache.setCacheObject(verifyKey, code, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);
  63. // 转换流信息写出
  64. FastByteArrayOutputStream os = new FastByteArrayOutputStream();
  65. try
  66. {
  67. ImageIO.write(image, "jpg", os);
  68. }
  69. catch (IOException e)
  70. {
  71. return AjaxResult.error(e.getMessage());
  72. }
  73. AjaxResult ajax = AjaxResult.success();
  74. ajax.put("uuid", uuid);
  75. ajax.put("img", Base64.encode(os.toByteArray()));
  76. return ajax;
  77. }
  78. }
  79. */