瀏覽代碼

短信验证

xiongzhu 3 年之前
父節點
當前提交
61af0354bb

+ 3 - 2
src/main/java/com/izouma/nineth/service/CaptchaService.java

@@ -32,10 +32,11 @@ public class CaptchaService {
 
         // 算术类型
         ArithmeticCaptcha captcha = new ArithmeticCaptcha(130, 48);
-        captcha.setLen(3);  // 几位数运算,默认是两位
+        captcha.setFont(com.pig4cloud.captcha.base.Captcha.FONT_7, 24 * 2);
+        captcha.setLen(2);  // 几位数运算,默认是两位
         captcha.getArithmeticString();  // 获取运算的公式:3+2=?
         captcha.supportAlgorithmSign(2); // 可设置支持的算法:2 表示只生成带加减法的公式
-        captcha.setDifficulty(50); // 设置计算难度,参与计算的每一个整数的最大值
+        captcha.setDifficulty(99); // 设置计算难度,参与计算的每一个整数的最大值
         String code = captcha.text();  // 获取运算的结果:5
         String image = captcha.toBase64();  // 输出验证码
         captchaCache.put(key, code);

+ 13 - 1
src/main/java/com/izouma/nineth/web/SmsController.java

@@ -2,6 +2,7 @@ package com.izouma.nineth.web;
 
 import com.izouma.nineth.config.Constants;
 import com.izouma.nineth.exception.BusinessException;
+import com.izouma.nineth.service.CaptchaService;
 import com.izouma.nineth.service.sms.SmsService;
 import lombok.AllArgsConstructor;
 import org.springframework.data.redis.core.RedisTemplate;
@@ -16,16 +17,27 @@ import java.util.regex.Pattern;
 @RequestMapping("/sms")
 @AllArgsConstructor
 public class SmsController {
-    private SmsService                    smsService;
+    private SmsService     smsService;
+    private CaptchaService captchaService;
 
     @GetMapping("/sendVerify")
     public String sendVerify(@RequestParam String phone) {
+        //throw new BusinessException("此接口已停用,请重启APP或刷新网页");
         if (!Pattern.matches(Constants.Regex.PHONE, phone)) {
             throw new BusinessException("请输入正确的手机号");
         }
         return smsService.sendVerify(phone);
     }
 
+    @GetMapping("/sendSecureVerify")
+    public String sendVerify(@RequestParam String phone, @RequestParam String captcha, @RequestParam String captchaKey) {
+        if (!Pattern.matches(Constants.Regex.PHONE, phone)) {
+            throw new BusinessException("请输入正确的手机号");
+        }
+        captchaService.verify(captchaKey, captcha);
+        return smsService.sendVerify(phone);
+    }
+
     @GetMapping("/verify")
     public String verify(@RequestParam String phone, @RequestParam String code) throws SmsService.SmsVerifyException {
         return smsService.verify(phone, code);