CaptchaController.java 3.3 KB

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