Selaa lähdekoodia

feat:单点登录多参数请求处理逻辑、浏览器错误解析回调地址bug修复

韩帛霖 1 vuosi sitten
vanhempi
säilyke
feef26bd94

+ 1 - 1
zkqy-admin/src/main/java/com/zkqy/web/controller/system/SysIndexController.java

@@ -24,6 +24,6 @@ public class SysIndexController
     @RequestMapping("/")
     public String index()
     {
-        return StringUtils.format("欢迎使用{}后台管理框架,当前版本:v{},请通过前端地址访问。", ruoyiConfig.getName(), ruoyiConfig.getVersion());
+        return StringUtils.format("生产协同{}后台管理框架,当前版本:v{},请通过前端地址访问。", ruoyiConfig.getName(), ruoyiConfig.getVersion());
     }
 }

+ 76 - 7
zkqy-framework/src/main/java/com/zkqy/framework/sso_oauth2/controller/OauthController.java

@@ -28,11 +28,18 @@ import org.springframework.security.core.AuthenticationException;
 import org.springframework.stereotype.Controller;
 import org.springframework.web.bind.annotation.*;
 
+import java.io.UnsupportedEncodingException;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.net.URLDecoder;
+import java.net.URLEncoder;
+import java.util.HashMap;
+import java.util.Map;
+
+
 import javax.annotation.Resource;
 import javax.servlet.http.HttpServletRequest;
 import java.time.LocalDateTime;
-import java.util.HashMap;
-import java.util.Map;
 
 /**
  * 基于oauth2.0相关的授权相关操作
@@ -131,15 +138,38 @@ public class OauthController {
             String redirectUri = request.getParameter("redirect_uri");
             String status = request.getParameter("status");
             String authorizationCode = authorizationService.createAuthorizationCode(clientIdStr, scopeStr, sysUser);
-            String params =
-                    redirectUri + "?code=" + authorizationCode;
-            if (StringUtils.isNoneBlank(status)) {
-                params = params + "&status=" + status;
+            String UrlString = "";
+            try {
+                URL url = new URL(redirectUri);
+                String queryString = url.getQuery();
+                // 解析查询参数
+                Map<String, String> params = parseQueryParameters(queryString);
+
+                // 修改或添加参数
+                params.put("code", authorizationCode);
+                params.put("status", status);
+                // 封装新的查询参数为字符串
+                String newQueryString = buildQueryString(params);
+                // 构造新的URL(这里仅演示字符串拼接,不实际创建URL对象)
+                UrlString = url.getProtocol() + "://" + url.getHost() + ":" + url.getPort() + url.getPath() + "?" + newQueryString;
+
+                // redisService.delete(key);
+                return "redirect:" + UrlString;
+            } catch (MalformedURLException e) {
+                e.printStackTrace();
             }
             // redisService.delete(key);
-            return "redirect:" + params;
+            return "redirect:" + REDIRECT_URL + "/login?tenantCode=" + tenantCode;
+
         } else {
             return "redirect:" + REDIRECT_URL + "/login?tenantCode=" + tenantCode;
+            /*
+            作为一个参数
+            redirect_uri:http://nbxl.tpddns.cn:4000/index?hideTitle=1&third=1
+            浏览器解析之后就变成两个参数了
+            redirect_uri:http://nbxl.tpddns.cn:4000/index?hideTitle=1
+            third:1
+             */
         }
     }
 
@@ -322,4 +352,43 @@ public class OauthController {
         result.put("error_description", errorCodeEnum.getErrorDescription());
     }
 
+    // 解析URL查询参数
+    public static Map<String, String> parseQueryParameters(String queryString) {
+        Map<String, String> params = new HashMap<>();
+        if (queryString != null && !queryString.isEmpty()) {
+            String[] pairs = queryString.split("&");
+            for (String pair : pairs) {
+                int idx = pair.indexOf("=");
+                if (idx > 0) {
+                    try {
+                        String key = URLDecoder.decode(pair.substring(0, idx), "UTF-8");
+                        String value = URLDecoder.decode(pair.substring(idx + 1), "UTF-8");
+                        params.put(key, value);
+                    } catch (UnsupportedEncodingException e) {
+                        e.printStackTrace();
+                    }
+                }
+            }
+        }
+        return params;
+    }
+
+    // 封装查询参数为字符串
+    public static String buildQueryString(Map<String, String> params) {
+        StringBuilder queryBuilder = new StringBuilder();
+        for (Map.Entry<String, String> entry : params.entrySet()) {
+            if (queryBuilder.length() > 0) {
+                queryBuilder.append("&");
+            }
+            try {
+                queryBuilder.append(URLEncoder.encode(entry.getKey(), "UTF-8"))
+                        .append("=")
+                        .append(URLEncoder.encode(entry.getValue(), "UTF-8"));
+            } catch (UnsupportedEncodingException e) {
+                e.printStackTrace();
+            }
+        }
+        return queryBuilder.toString();
+    }
+
 }

+ 3 - 0
zkqy-ui/src/layout/components/Sidebar/SidebarItem.vue

@@ -96,10 +96,13 @@ export default {
       if (isExternal(this.basePath)) {
         if (routeQuery) {
           let query = JSON.parse(routeQuery);
+          console.log("query参数", query)
           if (query.key) {
             // 设置code
             query.key = window.localStorage.getItem("setoauthUUID" + this.userInfo.name);
             query.tenantCode = this.userInfo.tenant.tenantCode
+            query.redirect_uri = encodeURIComponent(query.redirect_uri);
+            console.log(query)
             let baseURL = this.basePath
             Object.keys(query).forEach((key, index) => {
               if (index == 0) {