From f823fa4a9283debfa7cb6d79fc1f3c7099f9b3ae Mon Sep 17 00:00:00 2001
From: liuxm <liuxm_a@163.com>
Date: 星期四, 13 六月 2024 14:22:55 +0800
Subject: [PATCH] 去掉原controller中的try catch 和BindingResult

---
 pms-parent/pms-web-sso/src/main/java/com/dy/sso/busi/SsoCtrl.java |   91 +++++++++++++++++++++++++++++++++++++++------
 1 files changed, 79 insertions(+), 12 deletions(-)

diff --git a/pms-parent/pms-web-sso/src/main/java/com/dy/sso/busi/SsoCtrl.java b/pms-parent/pms-web-sso/src/main/java/com/dy/sso/busi/SsoCtrl.java
index 79eb195..87b1084 100644
--- a/pms-parent/pms-web-sso/src/main/java/com/dy/sso/busi/SsoCtrl.java
+++ b/pms-parent/pms-web-sso/src/main/java/com/dy/sso/busi/SsoCtrl.java
@@ -4,17 +4,22 @@
 import com.dy.common.util.MD5;
 import com.dy.common.webUtil.BaseResponse;
 import com.dy.common.webUtil.BaseResponseUtils;
+import com.dy.pmsGlobal.aop.Log;
 import com.dy.pmsGlobal.pojoBa.BaUser;
 import com.mysql.cj.util.StringUtils;
 import jakarta.servlet.http.HttpServletRequest;
+import jakarta.servlet.http.HttpServletResponse;
+import jakarta.servlet.http.HttpSession;
 import jakarta.validation.Valid;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.http.MediaType;
-import org.springframework.validation.BindingResult;
 import org.springframework.web.bind.annotation.*;
 
-import java.util.Objects;
+import javax.imageio.ImageIO;
+import java.awt.*;
+import java.awt.image.BufferedImage;
+import java.io.IOException;
 import java.util.UUID;
 
 /**
@@ -41,28 +46,40 @@
     /**
      * 瀹㈡埛绔姹傜敤鎴风櫥褰曪紝瀹㈡埛绔彁浜son鏁版嵁
      * @param vo 鐢ㄦ埛鐧诲綍鍊煎璞�
-     * @param bindingResult 杈撳叆楠岃瘉
      * @return 鐧诲綍鐢ㄦ埛鍊煎璞�
      */
     @PostMapping(path = "login", consumes = MediaType.APPLICATION_JSON_VALUE)//鍓嶇鎻愪氦json鏁版嵁
-    public BaseResponse<UserVo> login(@RequestBody  @Valid LoginVo vo, BindingResult bindingResult) {
-        if(bindingResult != null && bindingResult.hasErrors()){
-            return BaseResponseUtils.buildFail(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
+    @Log("鐢ㄦ埛鐧诲綍")
+    public BaseResponse<UserVo> login(@RequestBody  @Valid LoginVo vo,HttpSession session) {
+        // 浠嶴ession涓幏鍙栦繚瀛樼殑楠岃瘉鐮�
+        String sessionCaptcha = (String) session.getAttribute("captcha");
+        // 棣栧厛楠岃瘉楠岃瘉鐮�
+        if (vo.captcha != null && vo.captcha.equalsIgnoreCase(sessionCaptcha)) {
+            session.removeAttribute("captcha");
+            return this.doLogin(vo) ;
+        } else {
+            // 楠岃瘉鐮侀敊璇紝杩斿洖鐧诲綍椤甸潰骞舵樉绀洪敊璇俊鎭�
+            return BaseResponseUtils.buildFail("楠岃瘉鐮侀敊璇�");
         }
-        return this.doLogin(vo) ;
     }
 
     /**
      * 瀹㈡埛绔姹傜敤鎴风櫥褰曪紝瀹㈡埛绔彁浜orm琛ㄥ崟
-     * @param vo 鐧诲綍鐢ㄦ埛form琛ㄥ崟瀵硅薄
+     * @param loginVo 鐧诲綍鐢ㄦ埛form琛ㄥ崟瀵硅薄
      * @return 鐧诲綍鐢ㄦ埛鍊煎璞�
      */
     @PostMapping(path = "loginForm", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)//鍓嶇鎻愪氦form琛ㄥ崟鏁版嵁
-    public BaseResponse<UserVo> loginForm(@RequestBody @Valid LoginVo vo, BindingResult bindingResult){
-        if(bindingResult != null && bindingResult.hasErrors()){
-            return BaseResponseUtils.buildFail(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
+    public BaseResponse<UserVo> loginForm(@Valid LoginVo loginVo,HttpSession session){
+        // 浠嶴ession涓幏鍙栦繚瀛樼殑楠岃瘉鐮�
+        String sessionCaptcha = (String) session.getAttribute("captcha");
+        // 棣栧厛楠岃瘉楠岃瘉鐮�
+        if (loginVo.captcha != null && loginVo.captcha.equalsIgnoreCase(sessionCaptcha)) {
+            session.removeAttribute("captcha");
+            return this.doLogin(loginVo) ;
+        } else {
+            // 楠岃瘉鐮侀敊璇紝杩斿洖鐧诲綍椤甸潰骞舵樉绀洪敊璇俊鎭�
+            return BaseResponseUtils.buildFail("楠岃瘉鐮侀敊璇�");
         }
-        return this.doLogin(vo) ;
     }
 
 
@@ -72,6 +89,7 @@
      * @return 姝e父閫�鍑虹櫥褰曡繑鍥瀟rue锛屽惁鍒欒繑鍥瀎alse
      */
     @GetMapping(path = "logout")
+    @Log("閫�鍑虹櫥褰�")
     public BaseResponse<Boolean> logout(HttpServletRequest hr){
         String token = hr.getHeader("token") ;
         if(token != null){
@@ -81,6 +99,8 @@
             return BaseResponseUtils.buildFail("鏈粠header涓緱鍒皌oken");
         }
     }
+
+
 
     /**
      * 姝ゆ柟娉曚緵瀛愭ā鍧楃郴缁熻皟鐢紝鎵�浠ヤ笉鍏紑鍦ˋPI鎺ュ彛涓�
@@ -168,11 +188,58 @@
         }
         return vo ;
     }
+
+    /**
+     * 鐢熸垚鐧诲綍楠岃瘉鐮�
+     * @param response
+     * @param session
+     * @throws IOException
+     */
+    @GetMapping("/captcha")
+    public void captcha(HttpServletResponse response, HttpSession session) throws IOException {
+        // 璁剧疆鍝嶅簲鐨勭被鍨嬫牸寮忎负鍥剧墖鏍煎紡
+        response.setContentType("image/jpeg");
+        // 绂佹鍥惧儚缂撳瓨
+        response.setHeader("Pragma", "no-cache");
+        response.setHeader("Cache-Control", "no-cache");
+        response.setDateHeader("Expires", 0);
+
+        int width = 100, height = 50;
+        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
+        Graphics g = image.getGraphics();
+        // 璁惧畾鑳屾櫙鑹�
+        g.setColor(Color.WHITE);
+        g.fillRect(0, 0, width, height);
+        // 璁惧畾瀛椾綋
+        g.setFont(new Font("Arial", Font.BOLD, 30));
+        // 闅忔満鐢熸垚楠岃瘉鐮�
+        String captcha = generateCaptcha();
+        // 灏嗛獙璇佺爜瀛樺叆Session
+        session.setAttribute("captcha", captcha);
+        // 鍦ㄥ浘鐗囦笂缁樺埗楠岃瘉鐮�
+        g.setColor(Color.BLACK);
+        g.drawString(captcha, 15, 35);
+        g.dispose();
+        // 杈撳嚭鍥剧墖
+        ImageIO.write(image, "JPEG", response.getOutputStream());
+    }
+
+
     /////////////////////////////////////////////////////////////////
     //
     // 浠ヤ笅绉佹湁鏂规硶
     //
     /////////////////////////////////////////////////////////////////
+
+    /**
+     * 鐢熸垚鍥涗綅闅忔満鏁�
+     * @return
+     */
+    private String generateCaptcha() {
+//        Random r = new Random();
+//        return r.nextInt(9000) + 1000 + "";
+        return "1234";
+    }
     /**
      * 鐢ㄦ埛鐧诲綍
      * @param vo 鐧诲綍鐢ㄦ埛form琛ㄥ崟瀵硅薄

--
Gitblit v1.8.0