From 41c7e0ebcdaa27eef33c86f2c455bee0df9a38d3 Mon Sep 17 00:00:00 2001
From: kongzy <kongzy>
Date: 星期四, 07 十一月 2024 10:50:59 +0800
Subject: [PATCH] change captcha

---
 exam-framework/src/main/java/com/gkhy/exam/framework/web/service/SysLoginService.java |   31 ++++++++++++++++++++++++++++++-
 1 files changed, 30 insertions(+), 1 deletions(-)

diff --git a/exam-framework/src/main/java/com/gkhy/exam/framework/web/service/SysLoginService.java b/exam-framework/src/main/java/com/gkhy/exam/framework/web/service/SysLoginService.java
index bcde6f7..25f9814 100644
--- a/exam-framework/src/main/java/com/gkhy/exam/framework/web/service/SysLoginService.java
+++ b/exam-framework/src/main/java/com/gkhy/exam/framework/web/service/SysLoginService.java
@@ -1,6 +1,7 @@
 package com.gkhy.exam.framework.web.service;
 
 import cn.hutool.core.codec.Base64;
+import com.gkhy.exam.common.constant.CacheConstant;
 import com.gkhy.exam.common.constant.Constant;
 import com.gkhy.exam.common.constant.UserConstant;
 import com.gkhy.exam.common.domain.entity.SysUser;
@@ -10,6 +11,7 @@
 import com.gkhy.exam.common.enums.LoginUserTagEnum;
 import com.gkhy.exam.common.exception.ApiException;
 import com.gkhy.exam.common.utils.IpUtils;
+import com.gkhy.exam.common.utils.RedisUtils;
 import com.gkhy.exam.framework.manager.AsyncManager;
 import com.gkhy.exam.framework.manager.factory.AsyncFactory;
 import com.gkhy.exam.framework.security.context.AuthenticationContextHolder;
@@ -43,6 +45,8 @@
     @Autowired
     private HttpServletRequest request;
 
+    @Autowired
+    private RedisUtils redisUtils;
 
 
 
@@ -80,7 +84,7 @@
         String password=loginBody.getPassword();
         password= Base64.decodeStr(password);
         //验证码校验
-        //validateCaptcha(username,loginBody.code,loginBody.uuid);
+        validateCaptcha(username,loginBody.getCode(),loginBody.getUuid());
         loginPreCheck(username, password);
         Authentication authentication=null;
         try{
@@ -195,6 +199,31 @@
 //        }
     }
 
+    /**
+     * 校验验证码
+     *
+     * @param username 用户名
+     * @param code 验证码
+     * @param uuid 唯一标识
+     * @return 结果
+     */
+    public void validateCaptcha(String username, String code, String uuid)
+    {
+        if(StringUtils.isBlank(code)||StringUtils.isBlank(uuid)){
+            throw new ApiException("验证码或验证码标识为空");
+        }
+        String verifyKey = CacheConstant.CAPTCHA_CODE_KEY +uuid;
+        String captcha = (String) redisUtils.get(verifyKey);
+        redisUtils.del(verifyKey);
+        if (StringUtils.isBlank(captcha))
+        {
+            throw new ApiException("验证码已失效");
+        }
+        if (!code.equalsIgnoreCase(captcha))
+        {
+            throw new ApiException("验证码不正确");
+        }
+    }
 
     public void logout(){
         tokenService.delTokenCache(request);

--
Gitblit v1.9.2