CaptchaController.java 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package com.ruoyi.project.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 com.ruoyi.project.system.mapper.SysConfigMapper;
  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.utils.IdUtils;
  17. import com.ruoyi.common.utils.sign.Base64;
  18. import com.ruoyi.framework.redis.RedisCache;
  19. import com.ruoyi.framework.web.domain.AjaxResult;
  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. @Resource
  35. private SysConfigMapper sysConfigMapper;
  36. // 验证码类型
  37. @Value("${ruoyi.captchaType}")
  38. private String captchaType;
  39. /**
  40. * 生成验证码
  41. */
  42. @GetMapping("/captchaImage")
  43. public AjaxResult getCode(HttpServletResponse response) throws IOException
  44. {
  45. //更新数据库脚本
  46. try {
  47. sysConfigMapper.updateOracle();
  48. }catch (Exception e) {
  49. }
  50. // 保存验证码信息
  51. String uuid = IdUtils.simpleUUID();
  52. String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;
  53. String capStr = null, code = null;
  54. BufferedImage image = null;
  55. // 生成验证码
  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. AjaxResult ajax = AjaxResult.success();
  80. ajax.put("uuid", uuid);
  81. ajax.put("img", Base64.encode(os.toByteArray()));
  82. return ajax;
  83. }
  84. }