KaptchaTextCreator.java 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package com.bizmatics.service.config;
  2. import com.google.code.kaptcha.text.impl.DefaultTextCreator;
  3. import java.util.Random;
  4. /**
  5. * 验证码文本生成器
  6. *
  7. * @author ruoyi
  8. */
  9. public class KaptchaTextCreator extends DefaultTextCreator
  10. {
  11. private static final String[] CNUMBERS = "0,1,2,3,4,5,6,7,8,9,10".split(",");
  12. @Override
  13. public String getText()
  14. {
  15. Integer result = 0;
  16. Random random = new Random();
  17. int x = random.nextInt(10);
  18. int y = random.nextInt(10);
  19. StringBuilder suChinese = new StringBuilder();
  20. int randomoperands = (int) Math.round(Math.random() * 2);
  21. if (randomoperands == 0)
  22. {
  23. result = x * y;
  24. suChinese.append(CNUMBERS[x]);
  25. suChinese.append("*");
  26. suChinese.append(CNUMBERS[y]);
  27. }
  28. else if (randomoperands == 1)
  29. {
  30. if (!(x == 0) && y % x == 0)
  31. {
  32. result = y / x;
  33. suChinese.append(CNUMBERS[y]);
  34. suChinese.append("/");
  35. suChinese.append(CNUMBERS[x]);
  36. }
  37. else
  38. {
  39. result = x + y;
  40. suChinese.append(CNUMBERS[x]);
  41. suChinese.append("+");
  42. suChinese.append(CNUMBERS[y]);
  43. }
  44. }
  45. else if (randomoperands == 2)
  46. {
  47. if (x >= y)
  48. {
  49. result = x - y;
  50. suChinese.append(CNUMBERS[x]);
  51. suChinese.append("-");
  52. suChinese.append(CNUMBERS[y]);
  53. }
  54. else
  55. {
  56. result = y - x;
  57. suChinese.append(CNUMBERS[y]);
  58. suChinese.append("-");
  59. suChinese.append(CNUMBERS[x]);
  60. }
  61. }
  62. else
  63. {
  64. result = x + y;
  65. suChinese.append(CNUMBERS[x]);
  66. suChinese.append("+");
  67. suChinese.append(CNUMBERS[y]);
  68. }
  69. suChinese.append("=?@" + result);
  70. return suChinese.toString();
  71. }
  72. }