CaptchaController.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 org.springframework.beans.factory.annotation.Autowired;
  16. import org.springframework.beans.factory.annotation.Value;
  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. // 验证码类型
  40. @Value("${captcha.captchaType}")
  41. private String captchaType;
  42. // 验证码类别
  43. @Value("${captcha.captchaCategory}")
  44. private String captchaCategory;
  45. // 数字验证码位数
  46. @Value("${captcha.captchaNumberLength}")
  47. private int numberLength;
  48. // 字符验证码长度
  49. @Value("${captcha.captchaCharLength}")
  50. private int charLength;
  51. /**
  52. * 生成验证码
  53. */
  54. @GetMapping("/captchaImage")
  55. public AjaxResult getCode() {
  56. // 保存验证码信息
  57. String uuid = IdUtil.simpleUUID();
  58. String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;
  59. String code = null;
  60. // 生成验证码
  61. CodeGenerator codeGenerator;
  62. AbstractCaptcha captcha;
  63. switch (captchaType) {
  64. case "math":
  65. codeGenerator = new MathGenerator(numberLength);
  66. break;
  67. case "char":
  68. codeGenerator = new RandomGenerator(charLength);
  69. break;
  70. default:
  71. throw new IllegalArgumentException("验证码类型异常");
  72. }
  73. switch (captchaCategory) {
  74. case "line":
  75. captcha = lineCaptcha;
  76. break;
  77. case "circle":
  78. captcha = circleCaptcha;
  79. break;
  80. case "shear":
  81. captcha = shearCaptcha;
  82. break;
  83. default:
  84. throw new IllegalArgumentException("验证码类别异常");
  85. }
  86. captcha.setGenerator(codeGenerator);
  87. captcha.createCode();
  88. if ("math".equals(captchaType)) {
  89. code = getCodeResult(captcha.getCode());
  90. } else if ("char".equals(captchaType)) {
  91. code = captcha.getCode();
  92. }
  93. redisCache.setCacheObject(verifyKey, code, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);
  94. AjaxResult ajax = AjaxResult.success();
  95. ajax.put("uuid", uuid);
  96. ajax.put("img", captcha.getImageBase64());
  97. return ajax;
  98. }
  99. private String getCodeResult(String capStr) {
  100. int a = Convert.toInt(StrUtil.sub(capStr, 0, numberLength).trim());
  101. char operator = capStr.charAt(numberLength);
  102. int b = Convert.toInt(StrUtil.sub(capStr, numberLength + 1, numberLength + 1 + numberLength).trim());
  103. switch (operator) {
  104. case '*':
  105. return a * b + "";
  106. case '+':
  107. return a + b + "";
  108. case '-':
  109. return a - b + "";
  110. default:
  111. return "";
  112. }
  113. }
  114. }