CaptchaController.java 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package com.ruoyi.web.controller.common;
  2. import com.google.code.kaptcha.Producer;
  3. import com.ruoyi.common.annotation.Anonymous;
  4. import com.ruoyi.common.config.RuoYiConfig;
  5. import com.ruoyi.common.constant.CacheConstants;
  6. import com.ruoyi.common.constant.Constants;
  7. import com.ruoyi.common.core.domain.AjaxResult;
  8. import com.ruoyi.common.core.redis.RedisCache;
  9. import com.ruoyi.common.utils.StringUtils;
  10. import com.ruoyi.common.utils.sign.Base64;
  11. import com.ruoyi.common.utils.uuid.IdUtils;
  12. import com.ruoyi.generator.util.SendSms;
  13. import com.ruoyi.generator.util.SendSmsTencent;
  14. import com.ruoyi.system.service.ISysConfigService;
  15. import org.apache.logging.log4j.util.Strings;
  16. import org.springframework.beans.factory.annotation.Autowired;
  17. import org.springframework.util.FastByteArrayOutputStream;
  18. import org.springframework.web.bind.annotation.GetMapping;
  19. import org.springframework.web.bind.annotation.RestController;
  20. import javax.annotation.Resource;
  21. import javax.imageio.ImageIO;
  22. import javax.servlet.http.HttpServletResponse;
  23. import java.awt.image.BufferedImage;
  24. import java.io.IOException;
  25. import java.util.Random;
  26. import java.util.concurrent.ExecutionException;
  27. import java.util.concurrent.TimeUnit;
  28. /**
  29. * 验证码操作处理
  30. *
  31. * @author ruoyi
  32. */
  33. @RestController
  34. public class CaptchaController {
  35. @Resource(name = "captchaProducer")
  36. private Producer captchaProducer;
  37. @Resource(name = "captchaProducerMath")
  38. private Producer captchaProducerMath;
  39. @Autowired
  40. private RedisCache redisCache;
  41. @Autowired
  42. private ISysConfigService configService;
  43. @Autowired
  44. private SendSmsTencent sendSmsTencent;
  45. /**
  46. * 生成验证码
  47. */
  48. @GetMapping("/captchaImage")
  49. public AjaxResult getCode(HttpServletResponse response) throws IOException {
  50. AjaxResult ajax = AjaxResult.success();
  51. boolean captchaEnabled = configService.selectCaptchaEnabled();
  52. ajax.put("captchaEnabled", captchaEnabled);
  53. if (!captchaEnabled) {
  54. return ajax;
  55. }
  56. // 保存验证码信息
  57. String uuid = IdUtils.simpleUUID();
  58. String verifyKey = CacheConstants.CAPTCHA_CODE_KEY + uuid;
  59. String capStr = null, code = null;
  60. BufferedImage image = null;
  61. // 生成验证码
  62. String captchaType = RuoYiConfig.getCaptchaType();
  63. if ("math".equals(captchaType)) {
  64. String capText = captchaProducerMath.createText();
  65. capStr = capText.substring(0, capText.lastIndexOf("@"));
  66. code = capText.substring(capText.lastIndexOf("@") + 1);
  67. image = captchaProducerMath.createImage(capStr);
  68. } else if ("char".equals(captchaType)) {
  69. capStr = code = captchaProducer.createText();
  70. image = captchaProducer.createImage(capStr);
  71. }
  72. redisCache.setCacheObject(verifyKey, code, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);
  73. // 转换流信息写出
  74. FastByteArrayOutputStream os = new FastByteArrayOutputStream();
  75. try {
  76. ImageIO.write(image, "jpg", os);
  77. } catch (IOException e) {
  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. @GetMapping("/captchaCode")
  85. @Anonymous
  86. public AjaxResult getSmsCode(String phoneNumber, String smsType) throws ExecutionException, InterruptedException {
  87. //校验手机号
  88. boolean isPhone = StringUtils.checkPhoneNumber(phoneNumber);
  89. if (!isPhone) {
  90. return AjaxResult.error("手机号不正确!");
  91. }
  92. if (StringUtils.isBlank(smsType)) {
  93. return AjaxResult.error("参数异常!");
  94. }
  95. //1.生成随机四位验证码
  96. Random random = new Random();
  97. int code = 1000 + random.nextInt(9000);
  98. String verifyKey = "";
  99. switch (smsType) {
  100. case "login":
  101. verifyKey = CacheConstants.SMS_LOGIN_CODE_KEY + phoneNumber;
  102. break;
  103. case "register":
  104. verifyKey = CacheConstants.SMS_REGISTER_CODE_KEY + phoneNumber;
  105. break;
  106. case "update":
  107. verifyKey = CacheConstants.SMS_UPDATE_PASSWORD_CODE_KEY + phoneNumber;
  108. break;
  109. case "verify":
  110. verifyKey = CacheConstants.SMS_VERIFY_CODE_KEY + phoneNumber;
  111. break;
  112. }
  113. if (Strings.isBlank(verifyKey)) {
  114. return AjaxResult.error("参数异常!");
  115. }
  116. //2.发送验证码
  117. SendSms.sendMsg("次元时代", "SMS_491395317", phoneNumber, String.valueOf(code));
  118. //sendSmsTencent.sendMsg("次元时代", "SMS_491395317", phoneNumber, String.valueOf(code));
  119. //3.存入redis
  120. redisCache.setCacheObject(verifyKey, code, 120, TimeUnit.MINUTES);
  121. return AjaxResult.success("短信验证码发送成功!");
  122. }
  123. }