CaptchaController.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package com.ruoyi.web.controller.common;
  2. import cn.hutool.captcha.AbstractCaptcha;
  3. import cn.hutool.captcha.generator.CodeGenerator;
  4. import cn.hutool.core.convert.Convert;
  5. import cn.hutool.core.util.IdUtil;
  6. import com.ruoyi.common.constant.Constants;
  7. import com.ruoyi.common.core.domain.AjaxResult;
  8. import com.ruoyi.common.enums.CaptchaType;
  9. import com.ruoyi.common.utils.redis.RedisUtils;
  10. import com.ruoyi.common.utils.StringUtils;
  11. import com.ruoyi.common.utils.reflect.ReflectUtils;
  12. import com.ruoyi.common.utils.spring.SpringUtils;
  13. import com.ruoyi.framework.config.properties.CaptchaProperties;
  14. import com.ruoyi.system.service.ISysConfigService;
  15. import io.swagger.annotations.Api;
  16. import io.swagger.annotations.ApiOperation;
  17. import lombok.RequiredArgsConstructor;
  18. import org.springframework.beans.factory.annotation.Autowired;
  19. import org.springframework.web.bind.annotation.GetMapping;
  20. import org.springframework.web.bind.annotation.RestController;
  21. import java.util.HashMap;
  22. import java.util.Map;
  23. import java.util.concurrent.TimeUnit;
  24. /**
  25. * 验证码操作处理
  26. *
  27. * @author Lion Li
  28. */
  29. @Api(value = "验证码操作处理", tags = {"验证码管理"})
  30. @RequiredArgsConstructor(onConstructor_ = @Autowired)
  31. @RestController
  32. public class CaptchaController {
  33. private final CaptchaProperties captchaProperties;
  34. private final ISysConfigService configService;
  35. /**
  36. * 生成验证码
  37. */
  38. @ApiOperation("生成验证码")
  39. @GetMapping("/captchaImage")
  40. public AjaxResult<Map<String, Object>> getCode() {
  41. Map<String, Object> ajax = new HashMap<>();
  42. boolean captchaOnOff = configService.selectCaptchaOnOff();
  43. ajax.put("captchaOnOff", captchaOnOff);
  44. if (!captchaOnOff) {
  45. return AjaxResult.success(ajax);
  46. }
  47. // 保存验证码信息
  48. String uuid = IdUtil.simpleUUID();
  49. String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;
  50. // 生成验证码
  51. CaptchaType captchaType = captchaProperties.getType();
  52. boolean isMath = CaptchaType.MATH == captchaType;
  53. Integer length = isMath ? captchaProperties.getNumberLength() : captchaProperties.getCharLength();
  54. CodeGenerator codeGenerator = ReflectUtils.newInstance(captchaType.getClazz(), length);
  55. AbstractCaptcha captcha = SpringUtils.getBean(captchaProperties.getCategory().getClazz());
  56. captcha.setGenerator(codeGenerator);
  57. captcha.createCode();
  58. String code = isMath ? getCodeResult(captcha.getCode()) : captcha.getCode();
  59. RedisUtils.setCacheObject(verifyKey, code, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);
  60. ajax.put("uuid", uuid);
  61. ajax.put("img", captcha.getImageBase64());
  62. return AjaxResult.success(ajax);
  63. }
  64. private String getCodeResult(String capStr) {
  65. int numberLength = captchaProperties.getNumberLength();
  66. int a = Convert.toInt(StringUtils.substring(capStr, 0, numberLength).trim());
  67. char operator = capStr.charAt(numberLength);
  68. int b = Convert.toInt(StringUtils.substring(capStr, numberLength + 1, numberLength + 1 + numberLength).trim());
  69. switch (operator) {
  70. case '*':
  71. return Convert.toStr(a * b);
  72. case '+':
  73. return Convert.toStr(a + b);
  74. case '-':
  75. return Convert.toStr(a - b);
  76. default:
  77. return StringUtils.EMPTY;
  78. }
  79. }
  80. }