CaptchaController.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package com.ruoyi.web.controller.common;
  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.convert.Convert;
  6. import cn.hutool.core.util.IdUtil;
  7. import cn.hutool.core.util.RandomUtil;
  8. import com.ruoyi.common.constant.CacheConstants;
  9. import com.ruoyi.common.constant.Constants;
  10. import com.ruoyi.common.core.domain.R;
  11. import com.ruoyi.common.enums.CaptchaType;
  12. import com.ruoyi.common.utils.StringUtils;
  13. import com.ruoyi.common.utils.redis.RedisUtils;
  14. import com.ruoyi.common.utils.reflect.ReflectUtils;
  15. import com.ruoyi.common.utils.spring.SpringUtils;
  16. import com.ruoyi.framework.config.properties.CaptchaProperties;
  17. import com.ruoyi.sms.config.properties.SmsProperties;
  18. import com.ruoyi.sms.core.SmsTemplate;
  19. import com.ruoyi.sms.entity.SmsResult;
  20. import com.ruoyi.system.service.ISysConfigService;
  21. import lombok.RequiredArgsConstructor;
  22. import lombok.extern.slf4j.Slf4j;
  23. import org.springframework.validation.annotation.Validated;
  24. import org.springframework.web.bind.annotation.GetMapping;
  25. import org.springframework.web.bind.annotation.RestController;
  26. import javax.validation.constraints.NotBlank;
  27. import java.time.Duration;
  28. import java.util.HashMap;
  29. import java.util.Map;
  30. /**
  31. * 验证码操作处理
  32. *
  33. * @author Lion Li
  34. */
  35. @SaIgnore
  36. @Slf4j
  37. @Validated
  38. @RequiredArgsConstructor
  39. @RestController
  40. public class CaptchaController {
  41. private final CaptchaProperties captchaProperties;
  42. private final SmsProperties smsProperties;
  43. private final ISysConfigService configService;
  44. /**
  45. * 短信验证码
  46. *
  47. * @param phonenumber 用户手机号
  48. */
  49. @GetMapping("/captchaSms")
  50. public R<Void> smsCaptcha(@NotBlank(message = "{user.phonenumber.not.blank}")
  51. String phonenumber) {
  52. if (!smsProperties.getEnabled()) {
  53. return R.fail("当前系统没有开启短信功能!");
  54. }
  55. String key = CacheConstants.CAPTCHA_CODE_KEY + phonenumber;
  56. String code = RandomUtil.randomNumbers(4);
  57. RedisUtils.setCacheObject(key, code, Duration.ofMinutes(Constants.CAPTCHA_EXPIRATION));
  58. // 验证码模板id 自行处理 (查数据库或写死均可)
  59. String templateId = "";
  60. Map<String, String> map = new HashMap<>(1);
  61. map.put("code", code);
  62. SmsTemplate smsTemplate = SpringUtils.getBean(SmsTemplate.class);
  63. SmsResult result = smsTemplate.send(phonenumber, templateId, map);
  64. if (!result.isSuccess()) {
  65. log.error("验证码短信发送异常 => {}", result);
  66. return R.fail(result.getMessage());
  67. }
  68. return R.ok();
  69. }
  70. /**
  71. * 生成验证码
  72. */
  73. @GetMapping("/captchaImage")
  74. public R<Map<String, Object>> getCode() {
  75. Map<String, Object> ajax = new HashMap<>();
  76. boolean captchaEnabled = configService.selectCaptchaEnabled();
  77. ajax.put("captchaEnabled", captchaEnabled);
  78. if (!captchaEnabled) {
  79. return R.ok(ajax);
  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 = isMath ? getCodeResult(captcha.getCode()) : captcha.getCode();
  93. RedisUtils.setCacheObject(verifyKey, code, Duration.ofMinutes(Constants.CAPTCHA_EXPIRATION));
  94. ajax.put("uuid", uuid);
  95. ajax.put("img", captcha.getImageBase64());
  96. return R.ok(ajax);
  97. }
  98. private String getCodeResult(String capStr) {
  99. int numberLength = captchaProperties.getNumberLength();
  100. int a = Convert.toInt(StringUtils.substring(capStr, 0, numberLength).trim());
  101. char operator = capStr.charAt(numberLength);
  102. int b = Convert.toInt(StringUtils.substring(capStr, numberLength + 1, numberLength + 1 + numberLength).trim());
  103. switch (operator) {
  104. case '*':
  105. return Convert.toStr(a * b);
  106. case '+':
  107. return Convert.toStr(a + b);
  108. case '-':
  109. return Convert.toStr(a - b);
  110. default:
  111. return StringUtils.EMPTY;
  112. }
  113. }
  114. }