ValidateCodeServiceImpl.java 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package com.boman.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.boman.domain.constant.Constants;
  12. import com.boman.common.core.exception.CaptchaException;
  13. import com.boman.common.core.utils.IdUtils;
  14. import com.boman.common.core.utils.StringUtils;
  15. import com.boman.common.core.utils.sign.Base64;
  16. import com.boman.domain.dto.AjaxResult;
  17. import com.boman.common.redis.service.RedisService;
  18. import com.boman.gateway.service.ValidateCodeService;
  19. /**
  20. * 验证码实现处理
  21. *
  22. * @author ruoyi
  23. */
  24. @Service
  25. public class ValidateCodeServiceImpl implements ValidateCodeService
  26. {
  27. @Resource(name = "captchaProducer")
  28. private Producer captchaProducer;
  29. @Resource(name = "captchaProducerMath")
  30. private Producer captchaProducerMath;
  31. @Autowired
  32. private RedisService redisService;
  33. // 验证码类型
  34. private String captchaType = "math";
  35. /**
  36. * 生成验证码
  37. */
  38. @Override
  39. public AjaxResult createCapcha() throws IOException, CaptchaException
  40. {
  41. // 保存验证码信息
  42. String uuid = IdUtils.simpleUUID();
  43. String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;
  44. String capStr = null, code = null;
  45. BufferedImage image = null;
  46. // 生成验证码
  47. if ("math".equals(captchaType))
  48. {
  49. String capText = captchaProducerMath.createText();
  50. capStr = capText.substring(0, capText.lastIndexOf("@"));
  51. code = capText.substring(capText.lastIndexOf("@") + 1);
  52. image = captchaProducerMath.createImage(capStr);
  53. }
  54. else if ("char".equals(captchaType))
  55. {
  56. capStr = code = captchaProducer.createText();
  57. image = captchaProducer.createImage(capStr);
  58. }
  59. redisService.setCacheObject(verifyKey, code, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);
  60. // 转换流信息写出
  61. FastByteArrayOutputStream os = new FastByteArrayOutputStream();
  62. try
  63. {
  64. ImageIO.write(image, "jpg", os);
  65. }
  66. catch (IOException e)
  67. {
  68. return AjaxResult.error(e.getMessage());
  69. }
  70. //设置登录模式 0 密码 1扫码 2 混合
  71. String cacheObject = redisService.getCacheObject("sys_config:login_mode");
  72. if (StringUtils.isBlank(cacheObject)){
  73. cacheObject = "2";
  74. }
  75. AjaxResult ajax = AjaxResult.success();
  76. ajax.put("uuid", uuid);
  77. ajax.put("img", Base64.encode(os.toByteArray()));
  78. ajax.put("login_mode",cacheObject);
  79. return ajax;
  80. }
  81. /**
  82. * 校验验证码
  83. */
  84. @Override
  85. public void checkCapcha(String code, String uuid) throws CaptchaException
  86. {
  87. if (StringUtils.isEmpty(code))
  88. {
  89. throw new CaptchaException("验证码不能为空");
  90. }
  91. if (StringUtils.isEmpty(uuid))
  92. {
  93. throw new CaptchaException("验证码已失效");
  94. }
  95. String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;
  96. String captcha = redisService.getCacheObject(verifyKey);
  97. redisService.deleteObject(verifyKey);
  98. if (!code.equalsIgnoreCase(captcha))
  99. {
  100. throw new CaptchaException("验证码错误");
  101. }
  102. }
  103. }