簡體   English   中英

.Net Core Swashbuckle 在重定向時跳過授權標頭

[英].Net Core Swashbuckle skip authorization header on redirects

在 .Net Core Web API 中,我使用 Swashbuckle 集成了 Swagger。 該 API 受到保護,因此在 Swagger UI 中執行某些請求之前,需要進行授權和登錄。 這一切都很好。

現在,一個 API 調用會創建一個預簽名 URL 並將 HTTP 重定向返回到文件服務器(預簽名 URL)。

問題在於 Swagger UI 將帶有 JWT 令牌的授權標頭發送到文件服務器 (MinIO)。 這會導致文件服務器接收兩種不同的身份驗證機制並以無效請求進行響應。

有沒有辦法影響 Swagger UI 如何處理重定向或不在重定向時發送令牌?

我也遇到了這個問題,並意識到當fetch重定向到預先簽名的 S3 URL 時,您無法阻止它從您的 API 發送授權標頭。

最終,我能夠通過使用 Swagger 的responseInterceptor配置參數和一個自定義函數來實現這一點,該函數檢測來自 S3 的錯誤請求 (400) 響應,然后使用credentials: 'omit'重新發出fetch請求credentials: 'omit'

這是我的 Swagger 自定義響應攔截器:

// swagger-ui-extensions.js

function serializeHeaderValue(value) {
  const isMulti = value.includes(', ');
  return isMulti ? value.split(', ') : value;
}

function serializeHeaders(headers = {}) {
  return Array.from(headers.entries()).reduce((acc, [header, value]) => {
    acc[header] = serializeHeaderValue(value);
    return acc;
  }, {});
}

function myResponseInterceptor(response) {
  // NOTE: Additional checks should probably be added whether to re-issue the fetch. This was just an initial starting point.
  if (response.ok === false && response.status === 400 && response.headers['server'] === 'AmazonS3') {
    // Here is the important part, re-issue fetch but don't allow our Authentication header to flow
    response = fetch(response.url, { credentials: 'omit' })
      .then(nativeResponse => {
        // We can't return the native response because Swagger UI attempts to assign the header property (and potentially other properties
        // too) on the response. So return a serialized clone of the native response. FYI, this is the same exact logic from Swagger's fake
        // implementation of fetch.
        const getBody = nativeResponse.blob || nativeResponse.buffer;
        return getBody.call(nativeResponse).then(body => {
          return {
            ok: nativeResponse.ok,
            url: nativeResponse.url,
            status: nativeResponse.status,
            statusText: nativeResponse.statusText,
            headers: serializeHeaders(nativeResponse.headers),
            data: body
          };
        });
      });
  }
  return response;
}

然后我必須在index.html初始化 Swagger UI 時指定我的自定義myResponseInterceptor

      // (other code omitted for brevity...)

      // Make sure to include your custom JS in the page
      // <script src="./swagger-ui-extensions.js"></script>

      // Specifying the custom responseInterceptor here...
      configObject.responseInterceptor = myResponseInterceptor;

      // Begin Swagger UI call region

      const ui = SwaggerUIBundle(configObject);

      ui.initOAuth(oauthConfigObject);

      // End Swagger UI call region

      window.ui = ui;

我正在使用 ASP.NET Core 並使用這些說明為 Swagger UI 提供我自己的index.htmlhttps : //github.com/domaindrivendev/Swashbuckle.AspNetCore#customize-indexhtml

畢竟,這出人意料地有效,我能夠在 Swagger 中看到來自 S3 的重定向響應。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM