ValidateCodeServiceImpl.java 3.8 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.CacheConstants;
  12. import com.ruoyi.common.core.constant.Constants;
  13. import com.ruoyi.common.core.exception.CaptchaException;
  14. import com.ruoyi.common.core.utils.StringUtils;
  15. import com.ruoyi.common.core.utils.sign.Base64;
  16. import com.ruoyi.common.core.utils.uuid.IdUtils;
  17. import com.ruoyi.common.core.web.domain.AjaxResult;
  18. import com.ruoyi.common.redis.service.RedisService;
  19. import com.ruoyi.gateway.config.properties.CaptchaProperties;
  20. import com.ruoyi.gateway.service.ValidateCodeService;
  21. /**
  22. * 验证码实现处理
  23. *
  24. * @author ruoyi
  25. */
  26. @Service
  27. public class ValidateCodeServiceImpl implements ValidateCodeService
  28. {
  29. @Resource(name = "captchaProducer")
  30. private Producer captchaProducer;
  31. @Resource(name = "captchaProducerMath")
  32. private Producer captchaProducerMath;
  33. @Autowired
  34. private RedisService redisService;
  35. @Autowired
  36. private CaptchaProperties captchaProperties;
  37. /**
  38. * 生成验证码
  39. */
  40. @Override
  41. public AjaxResult createCaptcha() throws IOException, CaptchaException
  42. {
  43. AjaxResult ajax = AjaxResult.success();
  44. boolean captchaEnabled = captchaProperties.getEnabled();
  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. String captchaType = captchaProperties.getType();
  56. // 生成验证码
  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. redisService.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. /**
  85. * 校验验证码
  86. */
  87. @Override
  88. public void checkCaptcha(String code, String uuid) throws CaptchaException
  89. {
  90. if (StringUtils.isEmpty(code))
  91. {
  92. throw new CaptchaException("验证码不能为空");
  93. }
  94. String verifyKey = CacheConstants.CAPTCHA_CODE_KEY + StringUtils.nvl(uuid, "");
  95. String captcha = redisService.getCacheObject(verifyKey);
  96. if (captcha == null)
  97. {
  98. throw new CaptchaException("验证码已失效");
  99. }
  100. redisService.deleteObject(verifyKey);
  101. if (!code.equalsIgnoreCase(captcha))
  102. {
  103. throw new CaptchaException("验证码错误");
  104. }
  105. }
  106. }