CaptchaController.java 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package com.ruoyi.web.controller.common;
  2. import cn.hutool.captcha.AbstractCaptcha;
  3. import cn.hutool.captcha.CircleCaptcha;
  4. import cn.hutool.captcha.LineCaptcha;
  5. import cn.hutool.captcha.ShearCaptcha;
  6. import cn.hutool.captcha.generator.CodeGenerator;
  7. import cn.hutool.captcha.generator.RandomGenerator;
  8. import cn.hutool.core.convert.Convert;
  9. import cn.hutool.core.util.IdUtil;
  10. import com.ruoyi.common.utils.StringUtils;
  11. import com.ruoyi.common.constant.Constants;
  12. import com.ruoyi.common.core.domain.AjaxResult;
  13. import com.ruoyi.common.core.redis.RedisCache;
  14. import com.ruoyi.framework.captcha.UnsignedMathGenerator;
  15. import com.ruoyi.framework.config.properties.CaptchaProperties;
  16. import com.ruoyi.system.service.ISysConfigService;
  17. import org.springframework.beans.factory.annotation.Autowired;
  18. import org.springframework.web.bind.annotation.GetMapping;
  19. import org.springframework.web.bind.annotation.RestController;
  20. import javax.annotation.Resource;
  21. import java.util.HashMap;
  22. import java.util.Map;
  23. import java.util.concurrent.TimeUnit;
  24. /**
  25. * 验证码操作处理
  26. *
  27. * @author ruoyi
  28. */
  29. @RestController
  30. public class CaptchaController {
  31. // 圆圈干扰验证码
  32. @Resource(name = "CircleCaptcha")
  33. private CircleCaptcha circleCaptcha;
  34. // 线段干扰的验证码
  35. @Resource(name = "LineCaptcha")
  36. private LineCaptcha lineCaptcha;
  37. // 扭曲干扰验证码
  38. @Resource(name = "ShearCaptcha")
  39. private ShearCaptcha shearCaptcha;
  40. @Autowired
  41. private RedisCache redisCache;
  42. @Autowired
  43. private CaptchaProperties captchaProperties;
  44. @Autowired
  45. private ISysConfigService configService;
  46. /**
  47. * 生成验证码
  48. */
  49. @GetMapping("/captchaImage")
  50. public AjaxResult getCode() {
  51. Map<String, Object> ajax = new HashMap<>();
  52. boolean captchaOnOff = configService.selectCaptchaOnOff();
  53. ajax.put("captchaOnOff", captchaOnOff);
  54. if (!captchaOnOff) {
  55. return AjaxResult.success(ajax);
  56. }
  57. // 保存验证码信息
  58. String uuid = IdUtil.simpleUUID();
  59. String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;
  60. String code = null;
  61. // 生成验证码
  62. CodeGenerator codeGenerator;
  63. AbstractCaptcha captcha;
  64. switch (captchaProperties.getType()) {
  65. case "math":
  66. codeGenerator = new UnsignedMathGenerator(captchaProperties.getNumberLength());
  67. break;
  68. case "char":
  69. codeGenerator = new RandomGenerator(captchaProperties.getCharLength());
  70. break;
  71. default:
  72. throw new IllegalArgumentException("验证码类型异常");
  73. }
  74. switch (captchaProperties.getCategory()) {
  75. case "line":
  76. captcha = lineCaptcha;
  77. break;
  78. case "circle":
  79. captcha = circleCaptcha;
  80. break;
  81. case "shear":
  82. captcha = shearCaptcha;
  83. break;
  84. default:
  85. throw new IllegalArgumentException("验证码类别异常");
  86. }
  87. captcha.setGenerator(codeGenerator);
  88. captcha.createCode();
  89. if ("math".equals(captchaProperties.getType())) {
  90. code = getCodeResult(captcha.getCode());
  91. } else if ("char".equals(captchaProperties.getType())) {
  92. code = captcha.getCode();
  93. }
  94. redisCache.setCacheObject(verifyKey, code, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);
  95. ajax.put("uuid", uuid);
  96. ajax.put("img", captcha.getImageBase64());
  97. return AjaxResult.success(ajax);
  98. }
  99. private String getCodeResult(String capStr) {
  100. int numberLength = captchaProperties.getNumberLength();
  101. int a = Convert.toInt(StringUtils.sub(capStr, 0, numberLength).trim());
  102. char operator = capStr.charAt(numberLength);
  103. int b = Convert.toInt(StringUtils.sub(capStr, numberLength + 1, numberLength + 1 + numberLength).trim());
  104. switch (operator) {
  105. case '*':
  106. return a * b + "";
  107. case '+':
  108. return a + b + "";
  109. case '-':
  110. return a - b + "";
  111. default:
  112. return "";
  113. }
  114. }
  115. }