CaptchaController.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package com.ruoyi.web.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 com.ruoyi.common.core.constant.CacheConstants;
  8. import com.ruoyi.common.core.constant.Constants;
  9. import com.ruoyi.common.core.domain.R;
  10. import com.ruoyi.common.core.utils.SpringUtils;
  11. import com.ruoyi.common.core.utils.StringUtils;
  12. import com.ruoyi.common.core.utils.reflect.ReflectUtils;
  13. import com.ruoyi.common.redis.utils.RedisUtils;
  14. import com.ruoyi.common.sms.config.properties.SmsProperties;
  15. import com.ruoyi.common.sms.core.SmsTemplate;
  16. import com.ruoyi.common.sms.entity.SmsResult;
  17. import com.ruoyi.common.web.config.properties.CaptchaProperties;
  18. import com.ruoyi.common.web.enums.CaptchaType;
  19. import com.ruoyi.system.service.ISysConfigService;
  20. import lombok.RequiredArgsConstructor;
  21. import lombok.extern.slf4j.Slf4j;
  22. import org.springframework.expression.Expression;
  23. import org.springframework.expression.ExpressionParser;
  24. import org.springframework.expression.spel.standard.SpelExpressionParser;
  25. import org.springframework.validation.annotation.Validated;
  26. import org.springframework.web.bind.annotation.GetMapping;
  27. import org.springframework.web.bind.annotation.RestController;
  28. import jakarta.validation.constraints.NotBlank;
  29. import java.time.Duration;
  30. import java.util.HashMap;
  31. import java.util.Map;
  32. /**
  33. * 验证码操作处理
  34. *
  35. * @author Lion Li
  36. */
  37. @SaIgnore
  38. @Slf4j
  39. @Validated
  40. @RequiredArgsConstructor
  41. @RestController
  42. public class CaptchaController {
  43. private final CaptchaProperties captchaProperties;
  44. private final SmsProperties smsProperties;
  45. private final ISysConfigService configService;
  46. /**
  47. * 短信验证码
  48. *
  49. * @param phonenumber 用户手机号
  50. */
  51. @GetMapping("/captchaSms")
  52. public R<Void> smsCaptcha(@NotBlank(message = "{user.phonenumber.not.blank}")
  53. String phonenumber) {
  54. if (!smsProperties.getEnabled()) {
  55. return R.fail("当前系统没有开启短信功能!");
  56. }
  57. String key = CacheConstants.CAPTCHA_CODE_KEY + phonenumber;
  58. String code = RandomUtil.randomNumbers(4);
  59. RedisUtils.setCacheObject(key, code, Duration.ofMinutes(Constants.CAPTCHA_EXPIRATION));
  60. // 验证码模板id 自行处理 (查数据库或写死均可)
  61. String templateId = "";
  62. Map<String, String> map = new HashMap<>(1);
  63. map.put("code", code);
  64. SmsTemplate smsTemplate = SpringUtils.getBean(SmsTemplate.class);
  65. SmsResult result = smsTemplate.send(phonenumber, templateId, map);
  66. if (!result.isSuccess()) {
  67. log.error("验证码短信发送异常 => {}", result);
  68. return R.fail(result.getMessage());
  69. }
  70. return R.ok();
  71. }
  72. /**
  73. * 生成验证码
  74. */
  75. @GetMapping("/captchaImage")
  76. public R<Map<String, Object>> getCode() {
  77. boolean captchaEnabled = configService.selectCaptchaEnabled();
  78. if (!captchaEnabled) {
  79. return R.ok(Map.of("captchaEnabled", false));
  80. }
  81. // 保存验证码信息
  82. String uuid = IdUtil.simpleUUID();
  83. String verifyKey = CacheConstants.CAPTCHA_CODE_KEY + uuid;
  84. // 生成验证码
  85. CaptchaType captchaType = captchaProperties.getType();
  86. boolean isMath = CaptchaType.MATH == captchaType;
  87. Integer length = isMath ? captchaProperties.getNumberLength() : captchaProperties.getCharLength();
  88. CodeGenerator codeGenerator = ReflectUtils.newInstance(captchaType.getClazz(), length);
  89. AbstractCaptcha captcha = SpringUtils.getBean(captchaProperties.getCategory().getClazz());
  90. captcha.setGenerator(codeGenerator);
  91. captcha.createCode();
  92. String code = captcha.getCode();
  93. if (isMath) {
  94. ExpressionParser parser = new SpelExpressionParser();
  95. Expression exp = parser.parseExpression(StringUtils.remove(code, "="));
  96. code = exp.getValue(String.class);
  97. }
  98. RedisUtils.setCacheObject(verifyKey, code, Duration.ofMinutes(Constants.CAPTCHA_EXPIRATION));
  99. return R.ok(Map.of("uuid", uuid, "img", captcha.getImageBase64()));
  100. }
  101. }