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 |   54 ++++++++++++++++++++++++++++++++++++++++++++++--------
 1 files changed, 46 insertions(+), 8 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 a9bbf57..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;
@@ -25,6 +27,7 @@
 import org.springframework.stereotype.Component;
 
 import javax.annotation.Resource;
+import javax.servlet.http.HttpServletRequest;
 import java.time.LocalDateTime;
 
 @Component
@@ -39,7 +42,11 @@
     private TokenService tokenService;
     @Autowired
     private ExStudentService studentService;
+    @Autowired
+    private HttpServletRequest request;
 
+    @Autowired
+    private RedisUtils redisUtils;
 
 
 
@@ -77,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{
@@ -87,18 +94,18 @@
             authentication = authenticationManager.authenticate(authenticationToken);
             LoginUserDetails loginUserDetails= (LoginUserDetails) authentication.getPrincipal();
             passwordService.validate(loginUserDetails.getUser(),password);
-            AsyncManager.me().execute(AsyncFactory.recordLoginInfo(username, Constant.LOGIN_SUCCESS, "登录成功"));
+        //    AsyncManager.me().execute(AsyncFactory.recordLoginInfo(username, Constant.LOGIN_SUCCESS, "登录成功"));
             recordLoginInfo(loginUserDetails.getUser().getId(),LoginUserTagEnum.ADMIN_USER);
             return createLoginUser(loginUserDetails,LoginUserTagEnum.ADMIN_USER);
         }catch (Exception e){
             if (e instanceof BadCredentialsException)
             {
-                AsyncManager.me().execute(AsyncFactory.recordLoginInfo(username, Constant.LOGIN_FAIL, "用户密码不匹配"));
+             //   AsyncManager.me().execute(AsyncFactory.recordLoginInfo(username, Constant.LOGIN_FAIL, "用户密码不匹配"));
                 throw new ApiException("用户密码不匹配");
             }
             else
             {
-                AsyncManager.me().execute(AsyncFactory.recordLoginInfo(username, Constant.LOGIN_FAIL, e.getMessage()));
+              //  AsyncManager.me().execute(AsyncFactory.recordLoginInfo(username, Constant.LOGIN_FAIL, e.getMessage()));
                 throw new ApiException(e.getMessage());
             }
         }finally {
@@ -125,18 +132,18 @@
             authentication = authenticationManager.authenticate(authenticationToken);
             LoginUserDetails loginUserDetails= (LoginUserDetails) authentication.getPrincipal();
             passwordService.validate(loginUserDetails.getUser(),password);
-            AsyncManager.me().execute(AsyncFactory.recordLoginInfo(username, Constant.LOGIN_SUCCESS, "登录成功"));
+        //    AsyncManager.me().execute(AsyncFactory.recordLoginInfo(username, Constant.LOGIN_SUCCESS, "登录成功"));
             recordLoginInfo(loginUserDetails.getUser().getId(),LoginUserTagEnum.STUDENT_USER);
             return createLoginUser(loginUserDetails,LoginUserTagEnum.STUDENT_USER);
         }catch (Exception e){
             if (e instanceof BadCredentialsException)
             {
-                AsyncManager.me().execute(AsyncFactory.recordLoginInfo(username, Constant.LOGIN_FAIL, "用户密码不匹配"));
+               // AsyncManager.me().execute(AsyncFactory.recordLoginInfo(username, Constant.LOGIN_FAIL, "用户密码不匹配"));
                 throw new ApiException("用户密码不匹配");
             }
             else
             {
-                AsyncManager.me().execute(AsyncFactory.recordLoginInfo(username, Constant.LOGIN_FAIL, e.getMessage()));
+               // AsyncManager.me().execute(AsyncFactory.recordLoginInfo(username, Constant.LOGIN_FAIL, e.getMessage()));
                 throw new ApiException(e.getMessage());
             }
         }finally {
@@ -148,7 +155,8 @@
     private LoginUser createLoginUser(LoginUserDetails loginUserDetails,LoginUserTagEnum loginUserTagEnum){
         LoginUser loginUser= new LoginUser()
                 .setId(loginUserDetails.getUser().getId())
-                .setUsername(loginUserDetails.getUsername());
+                .setUsername(loginUserDetails.getUsername())
+                .setCompanyId(loginUserDetails.getUser().getCompanyId());
         loginUser.setToken(tokenService.createToken(loginUserDetails.getUsername()+loginUserTagEnum.getCode()));
         tokenService.cacheUserToken(loginUserDetails.getUsername(),loginUser.getToken());
         return loginUser;
@@ -191,6 +199,36 @@
 //        }
     }
 
+    /**
+     * 校验验证码
+     *
+     * @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