ValidateCodeServiceImpl.java 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package com.ruoyi.gateway.service.impl;
  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 org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.stereotype.Service;
  9. import org.springframework.util.FastByteArrayOutputStream;
  10. import com.google.code.kaptcha.Producer;
  11. import com.ruoyi.common.core.constant.Constants;
  12. import com.ruoyi.common.core.exception.CaptchaException;
  13. import com.ruoyi.common.core.utils.IdUtils;
  14. import com.ruoyi.common.core.utils.StringUtils;
  15. import com.ruoyi.common.core.utils.sign.Base64;
  16. import com.ruoyi.common.core.web.domain.AjaxResult;
  17. import com.ruoyi.common.redis.service.RedisService;
  18. import com.ruoyi.gateway.config.properties.CaptchaProperties;
  19. import com.ruoyi.gateway.service.ValidateCodeService;
  20. /**
  21. * 验证码实现处理
  22. *
  23. * @author ruoyi
  24. */
  25. @Service
  26. public class ValidateCodeServiceImpl implements ValidateCodeService
  27. {
  28. @Resource(name = "captchaProducer")
  29. private Producer captchaProducer;
  30. @Resource(name = "captchaProducerMath")
  31. private Producer captchaProducerMath;
  32. @Autowired
  33. private RedisService redisService;
  34. @Autowired
  35. private CaptchaProperties captchaProperties;
  36. /**
  37. * 生成验证码
  38. */
  39. @Override
  40. public AjaxResult createCaptcha() throws IOException, CaptchaException
  41. {
  42. AjaxResult ajax = AjaxResult.success();
  43. boolean captchaOnOff = captchaProperties.getEnabled();
  44. ajax.put("captchaOnOff", captchaOnOff);
  45. if (!captchaOnOff)
  46. {
  47. return ajax;
  48. }
  49. // 保存验证码信息
  50. String uuid = IdUtils.simpleUUID();
  51. String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;
  52. String capStr = null, code = null;
  53. BufferedImage image = null;
  54. String captchaType = captchaProperties.getType();
  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. redisService.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. /**
  84. * 校验验证码
  85. */
  86. @Override
  87. public void checkCaptcha(String code, String uuid) throws CaptchaException
  88. {
  89. if (StringUtils.isEmpty(code))
  90. {
  91. throw new CaptchaException("验证码不能为空");
  92. }
  93. if (StringUtils.isEmpty(uuid))
  94. {
  95. throw new CaptchaException("验证码已失效");
  96. }
  97. String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;
  98. String captcha = redisService.getCacheObject(verifyKey);
  99. redisService.deleteObject(verifyKey);
  100. if (!code.equalsIgnoreCase(captcha))
  101. {
  102. throw new CaptchaException("验证码错误");
  103. }
  104. }
  105. }