CaptchaController.java 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. package org.ruoyi.controller;
  2. import cn.dev33.satoken.annotation.SaIgnore;
  3. import cn.hutool.captcha.AbstractCaptcha;
  4. import cn.hutool.captcha.generator.CodeGenerator;
  5. import cn.hutool.core.util.IdUtil;
  6. import cn.hutool.core.util.RandomUtil;
  7. import org.ruoyi.common.core.constant.Constants;
  8. import org.ruoyi.common.core.constant.GlobalConstants;
  9. import org.ruoyi.common.core.domain.R;
  10. import org.ruoyi.common.core.exception.ServiceException;
  11. import org.ruoyi.common.core.service.ConfigService;
  12. import org.ruoyi.common.core.utils.SpringUtils;
  13. import org.ruoyi.common.core.utils.StringUtils;
  14. import org.ruoyi.common.core.utils.reflect.ReflectUtils;
  15. import org.ruoyi.common.mail.utils.MailUtils;
  16. import org.ruoyi.common.redis.utils.RedisUtils;
  17. import org.ruoyi.common.sms.config.properties.SmsProperties;
  18. import org.ruoyi.common.sms.core.SmsTemplate;
  19. import org.ruoyi.common.sms.entity.SmsResult;
  20. import org.ruoyi.common.web.config.properties.CaptchaProperties;
  21. import org.ruoyi.common.web.enums.CaptchaType;
  22. import org.ruoyi.system.domain.request.EmailRequest;
  23. import org.ruoyi.system.domain.vo.CaptchaVo;
  24. import jakarta.validation.Valid;
  25. import jakarta.validation.constraints.NotBlank;
  26. import lombok.RequiredArgsConstructor;
  27. import lombok.extern.slf4j.Slf4j;
  28. import org.springframework.expression.Expression;
  29. import org.springframework.expression.ExpressionParser;
  30. import org.springframework.expression.spel.standard.SpelExpressionParser;
  31. import org.springframework.validation.annotation.Validated;
  32. import org.springframework.web.bind.annotation.GetMapping;
  33. import org.springframework.web.bind.annotation.PostMapping;
  34. import org.springframework.web.bind.annotation.RequestBody;
  35. import org.springframework.web.bind.annotation.RestController;
  36. import java.time.Duration;
  37. import java.util.*;
  38. /**
  39. * 验证码操作处理
  40. *
  41. * @author Lion Li
  42. */
  43. @SaIgnore
  44. @Slf4j
  45. @Validated
  46. @RequiredArgsConstructor
  47. @RestController
  48. public class CaptchaController {
  49. private final CaptchaProperties captchaProperties;
  50. private final SmsProperties smsProperties;
  51. private final ConfigService configService;
  52. /**
  53. * 短信验证码
  54. *
  55. * @param phonenumber 用户手机号
  56. */
  57. @GetMapping("/resource/sms/code")
  58. public R<Void> smsCode(@NotBlank(message = "{user.phonenumber.not.blank}") String phonenumber) {
  59. if (!smsProperties.getEnabled()) {
  60. return R.fail("当前系统没有开启短信功能!");
  61. }
  62. String key = GlobalConstants.CAPTCHA_CODE_KEY + phonenumber;
  63. String code = RandomUtil.randomNumbers(4);
  64. RedisUtils.setCacheObject(key, code, Duration.ofMinutes(Constants.CAPTCHA_EXPIRATION));
  65. // 验证码模板id 自行处理 (查数据库或写死均可)
  66. String templateId = "";
  67. Map<String, String> map = new HashMap<>(1);
  68. map.put("code", code);
  69. SmsTemplate smsTemplate = SpringUtils.getBean(SmsTemplate.class);
  70. SmsResult result = smsTemplate.send(phonenumber, templateId, map);
  71. if (!result.isSuccess()) {
  72. log.error("验证码短信发送异常 => {}", result);
  73. return R.fail(result.getMessage());
  74. }
  75. return R.ok();
  76. }
  77. /**
  78. * 邮箱验证码
  79. *
  80. * @param emailRequest 用户邮箱
  81. */
  82. @PostMapping("/resource/email/code")
  83. public R<Void> emailCode(@RequestBody @Valid EmailRequest emailRequest) {
  84. String key = GlobalConstants.CAPTCHA_CODE_KEY + emailRequest.getUsername();
  85. String code = RandomUtil.randomNumbers(4);
  86. RedisUtils.setCacheObject(key, code, Duration.ofMinutes(Constants.CAPTCHA_EXPIRATION));
  87. // 检验邮箱后缀
  88. String suffix = configService.getConfigValue("mail", "suffix");
  89. String prompt = configService.getConfigValue("mail", "prompt");
  90. if(StringUtils.isNotEmpty(suffix)){
  91. // 动态的域名列表
  92. String[] invalidDomains = suffix.split(",");
  93. for (String domain : invalidDomains) {
  94. if (emailRequest.getUsername().endsWith(domain)) {
  95. throw new ServiceException(prompt);
  96. }
  97. }
  98. }
  99. // 自定义邮箱模板
  100. String model = configService.getConfigValue("mail", "mailModel");
  101. String mailTitle = configService.getConfigValue("mail", "mailTitle");
  102. String replacedModel = model.replace("{code}", code);
  103. try {
  104. MailUtils.sendHtml(emailRequest.getUsername(), mailTitle, replacedModel);
  105. } catch (Exception e) {
  106. log.error("邮箱验证码发送异常 => {}", e.getMessage());
  107. return R.fail(e.getMessage());
  108. }
  109. return R.ok();
  110. }
  111. /**
  112. * 生成验证码
  113. */
  114. @GetMapping("/auth/code")
  115. public R<CaptchaVo> getCode() {
  116. CaptchaVo captchaVo = new CaptchaVo();
  117. boolean captchaEnabled = captchaProperties.getEnable();
  118. if (!captchaEnabled) {
  119. captchaVo.setCaptchaEnabled(false);
  120. return R.ok(captchaVo);
  121. }
  122. // 保存验证码信息
  123. String uuid = IdUtil.simpleUUID();
  124. String verifyKey = GlobalConstants.CAPTCHA_CODE_KEY + uuid;
  125. // 生成验证码
  126. CaptchaType captchaType = captchaProperties.getType();
  127. boolean isMath = CaptchaType.MATH == captchaType;
  128. Integer length = isMath ? captchaProperties.getNumberLength() : captchaProperties.getCharLength();
  129. CodeGenerator codeGenerator = ReflectUtils.newInstance(captchaType.getClazz(), length);
  130. AbstractCaptcha captcha = SpringUtils.getBean(captchaProperties.getCategory().getClazz());
  131. captcha.setGenerator(codeGenerator);
  132. captcha.createCode();
  133. String code = captcha.getCode();
  134. if (isMath) {
  135. ExpressionParser parser = new SpelExpressionParser();
  136. Expression exp = parser.parseExpression(StringUtils.remove(code, "="));
  137. code = exp.getValue(String.class);
  138. }
  139. RedisUtils.setCacheObject(verifyKey, code, Duration.ofMinutes(Constants.CAPTCHA_EXPIRATION));
  140. captchaVo.setUuid(uuid);
  141. captchaVo.setImg(captcha.getImageBase64());
  142. return R.ok(captchaVo);
  143. }
  144. }