From 1cf88d43994ec7ec403319032a9d118b39fe3571 Mon Sep 17 00:00:00 2001
From: liurunyu <lry9898@163.com>
Date: 星期五, 25 四月 2025 14:35:35 +0800
Subject: [PATCH] 登录代码优化

---
 pipIrr-platform/pipIrr-web/pipIrr-web-sso/src/main/java/com/dy/sso/busi/SsoCtrl.java |   68 ++++++++++++++++++++++-----------
 1 files changed, 45 insertions(+), 23 deletions(-)

diff --git a/pipIrr-platform/pipIrr-web/pipIrr-web-sso/src/main/java/com/dy/sso/busi/SsoCtrl.java b/pipIrr-platform/pipIrr-web/pipIrr-web-sso/src/main/java/com/dy/sso/busi/SsoCtrl.java
index 9ab8e52..2dd7dee 100644
--- a/pipIrr-platform/pipIrr-web/pipIrr-web-sso/src/main/java/com/dy/sso/busi/SsoCtrl.java
+++ b/pipIrr-platform/pipIrr-web/pipIrr-web-sso/src/main/java/com/dy/sso/busi/SsoCtrl.java
@@ -25,11 +25,7 @@
 import org.springframework.validation.BindingResult;
 import org.springframework.web.bind.annotation.*;
 
-import java.util.List;
-import java.util.Objects;
-import java.util.UUID;
-
-//import org.springframework.cache.CacheManager;
+import java.util.*;
 
 /**
  * 娉ㄨВTag 鍦ˋPI涓樉绀猴細 Tag 娉ㄨВ, 缁欐暣涓帴鍙h捣浜嗕釜鍚嶅瓧涓庢弿杩�"
@@ -47,6 +43,7 @@
     //鍦ㄥ睘鎬т笂娉ㄨВ@Autowired鏃讹紝浼氳鍛� Field injection is not recommended锛堜笉鍐嶆帹鑽愪娇鐢ㄥ瓧娈垫敞鍏ワ級
     private SsoSv sv ;
 
+    //private KaptchaConfig kaptchaConfig;
     //@Autowired
     //private CacheManager cacheManager ;
 
@@ -55,6 +52,10 @@
         this.sv = sv ;
     }
 
+    //@Autowired
+    //public void setKaptchaConfig(KaptchaConfig kaptchaConfig) {
+    //    this.kaptchaConfig = kaptchaConfig;
+    //}
 
     /**
      * 瀹㈡埛绔姹傚緱鍒版墍鏈夌粍缁囨満鏋�
@@ -70,7 +71,7 @@
             )
     })
     @GetMapping(path = "allOrg")
-    public BaseResponse<List<Org>> allOrg(){
+    public BaseResponse<List<Org.OrgVo>> allOrg(){
         //List<Org> list = Arrays.asList(Org.Ym, Org.Pj) ;
         return BaseResponseUtils.buildSuccess(Org.OrgList);
     }
@@ -100,26 +101,47 @@
             )
     })
     @PostMapping(path = "loginJson", consumes = MediaType.APPLICATION_JSON_VALUE)
-    public BaseResponse<UserVo> loginJson(@RequestBody @Parameter(description = "鐧诲綍json鏁版嵁", required = true) @Valid LoginVo vo,  @Parameter(hidden = true) BindingResult bindingResult) {
+    public BaseResponse<UserVo> loginJson(HttpServletRequest request, @RequestBody @Parameter(description = "鐧诲綍json鏁版嵁", required = true) @Valid LoginVo vo,  @Parameter(hidden = true) BindingResult bindingResult) {
         if(bindingResult != null && bindingResult.hasErrors()){
-            return BaseResponseUtils.buildFail(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
+            return BaseResponseUtils.buildErrorMsg(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
         }
         if(!vo.phone.equals("admin")){
             if(vo.phone.length() != 11){
-                return BaseResponseUtils.buildFail("鎵嬫満鍙凤紙闀垮害涓嶆槸11浣嶏級涓嶆纭�");
+                return BaseResponseUtils.buildErrorMsg("鎵嬫満鍙凤紙闀垮害涓嶆槸11浣嶏級涓嶆纭�");
             }
         }
         if(vo.orgTag == null || vo.orgTag.trim().length() == 0){
-            return BaseResponseUtils.buildFail("鏈�夋嫨缁勭粐鍗曚綅");
+            return BaseResponseUtils.buildErrorMsg("鏈�夋嫨缁勭粐鍗曚綅");
+        }
+        if(vo.token == null || vo.token.trim().length() == 0){
+            return BaseResponseUtils.buildErrorMsg("鏃爐oken");
+        }
+        if(vo.code == null || vo.code.trim().length() == 0){
+            return BaseResponseUtils.buildErrorMsg("璇疯緭鍏ラ獙璇佺爜");
         }
         //鎶婄粍缁囧崟浣嶆爣绛句綔涓烘暟鎹簮鍚嶇О
         DataSourceContext.set(vo.orgTag);
 
-        String uuid ;
-        BaUser userPo ;
+        // 浠庢暟鎹簱鑾峰彇楠岃瘉鐮�
+        Map map = sv.getCodeByToken(vo.token);
+        Long expiration = Long.parseLong(map.get("expiration").toString());
+        Long currentTimestamp = System.currentTimeMillis();
+        if(currentTimestamp > expiration) {
+            return BaseResponseUtils.buildErrorMsg("楠岃瘉鐮佸凡瓒呮椂");
+        }
+        String localCode = map.get("code").toString();
+        if(!vo.code.equals(localCode)) {
+            return BaseResponseUtils.buildErrorMsg("楠岃瘉鐮侀敊璇�");
+        }
+
+        if(!sv.existPhone(vo.phone)){
+            return BaseResponseUtils.buildErrorMsg("璐﹀彿涓嶅瓨鍦�");
+        }
+
+        String uuid4Token = null;
+        BaUser userPo = null ;
         try {
-            //Boolean flag = cacheManager.getCacheNames().isEmpty() ;
-            uuid = UUID.randomUUID().toString();
+            uuid4Token = UUID.randomUUID().toString();
             if(!StringUtils.isNullOrEmpty(vo.password)){
                 /*
                 濡傛灉鍓嶇杩涜浜哹ase64鍔犲瘑
@@ -127,7 +149,7 @@
                 */
                 vo.password = MD5.encrypt(vo.password) ;
             }
-            userPo = this.sv.loginWithMapperXml(uuid, vo.phone, vo.password);
+            userPo = this.sv.loginWithMapperXml(uuid4Token, vo.phone, vo.password);
         } catch (Exception e) {
             log.error("鐢ㄦ埛鐧诲綍寮傚父", e);
             return BaseResponseUtils.buildException(e.getMessage()) ;
@@ -135,10 +157,10 @@
 
         if(userPo != null){
             UserVo uVo = UserVoMapper.INSTANCT.po2vo(userPo);
-            uVo.token = uuid ;
+            uVo.token = uuid4Token ;
             return BaseResponseUtils.buildSuccess(uVo);
         }else{
-            return BaseResponseUtils.buildFail("鐧诲綍澶辫触");
+            return BaseResponseUtils.buildErrorMsg("鐧诲綍澶辫触");
         }
     }
 
@@ -159,10 +181,10 @@
     @PostMapping(path = "loginForm", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
     public BaseResponse<UserVo> loginForm(@Parameter(description = "form琛ㄥ崟鏁版嵁", required = true) @Valid LoginVo vo,  @Parameter(hidden = true) BindingResult bindingResult){
         if(bindingResult != null && bindingResult.hasErrors()){
-            return BaseResponseUtils.buildFail(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
+            return BaseResponseUtils.buildErrorMsg(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
         }
         if(vo.orgTag == null || vo.orgTag.trim().length() == 0){
-            return BaseResponseUtils.buildFail("鏈�夋嫨缁勭粐鍗曚綅");
+            return BaseResponseUtils.buildErrorMsg("鏈�夋嫨缁勭粐鍗曚綅");
         }
         //鎶婄粍缁囧崟浣嶆爣绛句綔涓烘暟鎹簮鍚嶇О
         DataSourceContext.set(vo.orgTag);
@@ -190,7 +212,7 @@
             uVo.token = uuid ;
             return BaseResponseUtils.buildSuccess(uVo);
         }else{
-            return BaseResponseUtils.buildFail("鐧诲綍澶辫触");
+            return BaseResponseUtils.buildErrorMsg("鐧诲綍澶辫触");
         }
     }
 
@@ -215,7 +237,7 @@
             this.sv.logout(token) ;
             return BaseResponseUtils.buildSuccess(true);
         }else{
-            return BaseResponseUtils.buildFail("鏈粠header涓緱鍒皌oken");
+            return BaseResponseUtils.buildErrorMsg("鏈粠header涓緱鍒皌oken");
         }
     }
 
@@ -245,10 +267,10 @@
         if(userPo != null){
             vo.dataSourceName = userPo.orgTag ;
             vo.logined = true ;
-            vo.hasPower = true ;//榛樿鏈夋潈闄�
+            vo.hasPower = true ;//榛樿鏈夋潈闄愩��2023-12-21 缁忓晢璁紝鐢卞墠绔壌鏉�
         }else{
             vo.logined = false ;
-            vo.hasPower = true ;//榛樿鏈夋潈闄�
+            vo.hasPower = true ;//榛樿鏈夋潈闄愩��2023-12-21 缁忓晢璁紝鐢卞墠绔壌鏉�
         }
         return vo ;
     }

--
Gitblit v1.8.0