【问题标题】:.Net Core Swashbuckle skip authorization header on redirects.Net Core Swashbuckle 在重定向时跳过授权标头
【发布时间】:2019-06-13 07:37:34
【问题描述】:

在 .Net Core Web API 中,我使用 Swashbuckle 集成了 Swagger。 API 受到保护,因此在 Swagger UI 中执行某些请求之前需要授权和登录。这一切都很好。

现在,一个 API 调用会创建一个预签名 URL,并将 HTTP 重定向返回到文件服务器(预签名 URL)。

问题在于 Swagger UI 将带有 JWT 令牌的授权标头发送到文件服务器 (MinIO)。这会导致文件服务器接收到两种不同的身份验证机制并以无效请求进行响应。

有没有办法影响 Swagger UI 如何处理重定向或不在重定向时发送令牌?

【问题讨论】:

  • 我找到了这篇博文。你能检查一下吗? mattfrear.com/2018/07/21/…
  • 你解决了吗?
  • 不,我从来没有解决过这个问题。由于 swagger 在生产中已被禁用,并且我仅将其用于开发/测试,因此没有进一步调查的优先级。对不起。

标签: c# .net-core swagger-ui swashbuckle minio


【解决方案1】:

我也遇到了这个问题,并意识到当fetch 重定向到预签名的 S3 URL 时,您无法阻止它从您的 API 发送授权标头。

最终我可以通过使用 Swagger 的 responseInterceptor 配置参数和一个自定义函数来实现这个工作,该函数检测来自 S3 的错误请求 (400) 响应,然后使用 credentials: 'omit' 重新发出 fetch 请求.

这是我对 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 的重定向响应。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-24
    • 1970-01-01
    • 1970-01-01
    • 2020-04-28
    相关资源
    最近更新 更多