deason il y a 5 ans
Parent
commit
6594812908

+ 28 - 11
examcloud-core-basic-base/src/main/java/cn/com/qmth/examcloud/core/basic/base/util/VerifyCode.java

@@ -1,6 +1,7 @@
 package cn.com.qmth.examcloud.core.basic.base.util;
 
 import cn.com.qmth.examcloud.commons.exception.StatusException;
+import org.springframework.util.Base64Utils;
 
 import javax.imageio.ImageIO;
 import javax.script.ScriptEngine;
@@ -8,6 +9,7 @@ import javax.script.ScriptEngineManager;
 import java.awt.*;
 import java.awt.image.BufferedImage;
 import java.io.ByteArrayOutputStream;
+import java.io.File;
 import java.io.IOException;
 import java.io.Serializable;
 import java.util.Random;
@@ -44,17 +46,15 @@ public class VerifyCode {
         }
 
         // 随机生成三个数字、并组合成一个数学公式
-//        String verifyCode = new StringBuilder()
-//                .append(random.nextInt(10))
-//                .append(OPS[random.nextInt(2)])
-//                .append(random.nextInt(10))
-//                .append(OPS[random.nextInt(2)])
-//                .append(random.nextInt(10))
-//                .append(OPS[random.nextInt(2)])
-//                .append(random.nextInt(10))
-//                .toString();
-
-        String verifyCode = "1+2+3+4";
+        String verifyCode = new StringBuilder()
+                .append(random.nextInt(10))
+                .append(OPS[random.nextInt(2)])
+                .append(random.nextInt(10))
+                .append(OPS[random.nextInt(2)])
+                .append(random.nextInt(10))
+                .append(OPS[random.nextInt(2)])
+                .append(random.nextInt(10))
+                .toString();
 
         graphics.setColor(new Color(0, 0, 255));
         graphics.setFont(new Font("Candara", Font.BOLD, 22));
@@ -118,4 +118,21 @@ public class VerifyCode {
         }
     }
 
+    /*public static void main(String[] args) throws Exception {
+        final String dir = "/home/verifyCodes";
+        new File(dir).mkdirs();
+
+        for (int n = 1; n <= 1; n++) {
+            Result result = VerifyCode.generateVerifyCode();
+
+            final String imgPath = String.format("Img_%s.jpg", n);
+            File imgFile = new File(dir + "/" + imgPath);
+            ImageIO.write(result.getImage(), "JPEG", imgFile);
+
+            byte[] bytes = imageToBytes(result.getImage());
+            String base64 = Base64Utils.encodeToString(bytes);
+            System.out.println(base64);
+        }
+    }*/
+
 }

+ 1 - 7
examcloud-core-basic-service/src/main/java/cn/com/qmth/examcloud/core/basic/service/impl/VerifyCodeServiceImpl.java

@@ -20,7 +20,6 @@ import org.apache.commons.codec.binary.Base64;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
-import org.springframework.util.Base64Utils;
 
 /**
  * 验证码相关接口
@@ -59,7 +58,6 @@ public class VerifyCodeServiceImpl implements VerifyCodeService {
         VerifyCode.Result result = VerifyCode.generateVerifyCode();
         byte[] bytes = VerifyCode.imageToBytes(result.getImage());
         String base64 = Base64.encodeBase64String(bytes);
-        String base642 = Base64Utils.encodeToString(bytes);
         String uuid = UUID.randomUUID();
 
         log.info(String.format("accountValue = %s, uuid = %s, verifyCode = %s", accountValue, uuid, result.getResult()));
@@ -70,11 +68,7 @@ public class VerifyCodeServiceImpl implements VerifyCodeService {
         final String cacheKeyVerifyCode = String.format(CACHE_KEY_VERIFY_CODE, rootOrgId, accountValue);
         redisClient.set(cacheKeyVerifyCode, cacheBean, 60);// N秒
 
-        log.info(base64);
-        log.info("=========");
-        log.info(base642);
-
-        return uuid + base642;
+        return uuid + base64;
     }
 
     /**