Explorar el Código

fix: nginx 302重定向SSO回调到hash路由格式

kinglee hace 1 semana
padre
commit
c7a494941f
Se han modificado 3 ficheros con 6 adiciones y 11 borrados
  1. 5 3
      nginx.conf.template
  2. 0 7
      src/app.tsx
  3. 1 1
      src/pages/auth/callback/index.tsx

+ 5 - 3
nginx.conf.template

@@ -7,9 +7,11 @@ server {
         try_files $uri $uri/ /index.html;
     }
 
-    # SSO 回调页面由前端 SPA 处理,^~ 确保优先于下面的正则匹配
-    location ^~ /auth/callback {
-        try_files $uri /index.html;
+    # SSO 回调页面:服务端重定向到 hash 路由格式
+    # SSO平台不支持带#的redirect_uri,所以需要nginx将 /auth/callback?code=xxx
+    # 重定向到 /#/auth/callback?code=xxx
+    location = /auth/callback {
+        return 302 /#/auth/callback$is_args$args;
     }
 
     location ~ ^/(v1|v2|auth|version|proxy|update|cli|grafana) {

+ 0 - 7
src/app.tsx

@@ -124,13 +124,6 @@ export async function getInitialState(): Promise<{
   // SSO 回调页面不需要获取用户信息,等待换码完成
   const isSSOCallback = location.pathname.startsWith('/auth/callback');
 
-  // hash 路由模式下,SSO 平台回调 URL 不带 hash,
-  // 需要把 /auth/callback?code=xxx 转为 /auth/callback#/auth/callback?code=xxx
-  if (isSSOCallback && !location.pathname.includes('#') && !location.hash.startsWith('#/auth/callback')) {
-    const search = location.search;
-    history.replace(`/auth/callback#${location.pathname}${search}`);
-  }
-
   if (![DEFAULT_ENTER_PAGE.login].includes(location.pathname) && !isSSOCallback) {
     const userInfo = await fetchUserInfo();
     checkDefaultPage(userInfo);

+ 1 - 1
src/pages/auth/callback/index.tsx

@@ -7,7 +7,7 @@ import { DEFAULT_ENTER_PAGE } from '@/config/settings';
 /**
  * 从 URL 中提取 code 参数
  * 兼容两种 URL 格式:
- * 1. /auth/callback?code=xxx#/login (SSO回调无hash)
+ * 1. /auth/callback?code=xxx#/login (SSO回调无hash, UmiJS自动补的)
  * 2. /#/auth/callback?code=xxx (SSO回调带hash, 推荐)
  */
 function extractCodeFromUrl(): { code: string | null; error: string | null; errorDesc: string | null } {