CaptchaController.java 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package com.schoolinout.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.schoolinout.common.config.RuoYiConfig;
  14. import com.schoolinout.common.constant.CacheConstants;
  15. import com.schoolinout.common.constant.Constants;
  16. import com.schoolinout.common.core.domain.AjaxResult;
  17. import com.schoolinout.common.core.redis.RedisCache;
  18. import com.schoolinout.common.utils.sign.Base64;
  19. import com.schoolinout.common.utils.uuid.IdUtils;
  20. import com.schoolinout.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 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. {
  48. return ajax;
  49. }
  50. // 保存验证码信息
  51. String uuid = IdUtils.simpleUUID();
  52. String verifyKey = CacheConstants.CAPTCHA_CODE_KEY + uuid;
  53. String capStr = null, code = null;
  54. BufferedImage image = null;
  55. // 生成验证码
  56. String captchaType = RuoYiConfig.getCaptchaType();
  57. if ("math".equals(captchaType))
  58. {
  59. String capText = captchaProducerMath.createText();
  60. capStr = capText.substring(0, capText.lastIndexOf("@"));
  61. code = capText.substring(capText.lastIndexOf("@") + 1);
  62. image = captchaProducerMath.createImage(capStr);
  63. }
  64. else if ("char".equals(captchaType))
  65. {
  66. capStr = code = captchaProducer.createText();
  67. image = captchaProducer.createImage(capStr);
  68. }
  69. redisCache.setCacheObject(verifyKey, code, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);
  70. // 转换流信息写出
  71. FastByteArrayOutputStream os = new FastByteArrayOutputStream();
  72. try
  73. {
  74. ImageIO.write(image, "jpg", os);
  75. }
  76. catch (IOException e)
  77. {
  78. return AjaxResult.error(e.getMessage());
  79. }
  80. ajax.put("uuid", uuid);
  81. ajax.put("img", Base64.encode(os.toByteArray()));
  82. return ajax;
  83. }
  84. }