|
|
@@ -0,0 +1,50 @@
|
|
|
+package com.ufo.module.common;
|
|
|
+
|
|
|
+import java.util.Random;
|
|
|
+
|
|
|
+import com.wf.captcha.ArithmeticCaptcha;
|
|
|
+
|
|
|
+public class PureJavaArithmeticCaptcha extends ArithmeticCaptcha{
|
|
|
+
|
|
|
+ private static final Random RANDOM = new Random();
|
|
|
+ public PureJavaArithmeticCaptcha(int width, int height) {
|
|
|
+ super(width, height);
|
|
|
+ }
|
|
|
+ @Override
|
|
|
+ protected char[] alphas() {
|
|
|
+ // 1. 生成 1-10 的随机数
|
|
|
+ int n1 = RANDOM.nextInt(10) + 1;
|
|
|
+ int n2 = RANDOM.nextInt(10) + 1;
|
|
|
+
|
|
|
+ // 2. 随机生成运算符:0-加,1-减,2-乘
|
|
|
+ int optType = RANDOM.nextInt(3);
|
|
|
+ int res;
|
|
|
+ char optChar;
|
|
|
+
|
|
|
+ switch (optType) {
|
|
|
+ case 0:
|
|
|
+ res = n1 + n2;
|
|
|
+ optChar = '+';
|
|
|
+ break;
|
|
|
+ case 1:
|
|
|
+ // 确保减法结果不为负数,提升用户体验
|
|
|
+ if (n1 < n2) {
|
|
|
+ int temp = n1; n1 = n2; n2 = temp;
|
|
|
+ }
|
|
|
+ res = n1 - n2;
|
|
|
+ optChar = '-';
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ res = n1 * n2;
|
|
|
+ optChar = 'x';
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 3. 设置算式字符串(前端展示用,如 "3+5=?")
|
|
|
+ this.setArithmeticString(String.format("%s%c%s=?", n1, optChar, n2));
|
|
|
+ // 4. 设置计算结果(后端存入 Redis 校验用)
|
|
|
+ this.chars = String.valueOf(res);
|
|
|
+
|
|
|
+ return chars.toCharArray();
|
|
|
+ }
|
|
|
+}
|