CaptchaController.java 3.3 KB

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