如何让 Swagger UI 将端口 443 与 Swashbuckle 一起使用? [英] How do I get Swagger UI to use port 443 with Swashbuckle?

查看:74
本文介绍了如何让 Swagger UI 将端口 443 与 Swashbuckle 一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在运行 RESTful Web 服务的 QA 和 Prod 环境中,端口 80 未打开.所以目前当我尝试在 QA 中使用 Swagger UI 时,我收到此消息并且它只是挂起:

In our QA and Prod environments that run our RESTful web services, port 80 is not open. So currently when I try to get to Swagger UI in QA, I get this message and it just hangs:

获取资源列表:http://qa-server:80/product-catalog-api/swagger/docs/v1;请稍候.

我正在使用 Swashbuckle 来配置 Swagger.我也在配置中更改了这一行,但它仍然不起作用.

I am using Swashbuckle to configure Swagger. I also changed this line in the config, but it still doesn't work.

// If schemes are not explicitly provided in a Swagger 2.0 document, then the scheme used to access
// the docs is taken as the default. If your API supports multiple schemes and you want to be explicit
// about them, you can use the "Schemes" option as shown below.
//
c.Schemes(new[] { "https" });

SSL 端口 443 已打开,因此我想使用 Swagger UI 在其上运行.我可以手动将 http://qa-server:80/product-catalog-api/swagger/docs/v1 更改为 https://qa-server/product-catalog-api/swagger/docs/v1 然后 Swagger 将列出我的 Web 方法,但是当我单击 Try it out! 时它挂起 这是控制台的输出: SCRIPT5: Access is否认.文件:swagger-ui-min-js,行:10,列:4300

The SSL port 443 is open so I would like to get to Swagger UI to run on it. I can manually change http://qa-server:80/product-catalog-api/swagger/docs/v1 to https://qa-server/product-catalog-api/swagger/docs/v1 and then Swagger will list my web methods, but it hangs when I click Try it out! This is the output from the console: SCRIPT5: Access is denied. File: swagger-ui-min-js, Line: 10, Column: 4300

所以我一直在挖掘更多,并且已经走得更远,但仍然不是我想要的地方.如果我查看 Swagger index.html 文件上的源代码,我可以看到问题:

So I have been digging in some more and have gotten a little farther but still not where I want to be. If I view source on the Swagger index.html file, I can see the problem:

window.swashbuckleConfig = {
  rootUrl: 'http://qa-server:80/product-catalog-api',
  discoveryPaths: arrayFrom('swagger/docs/v1'),
  booleanValues: arrayFrom('true|false'),
  validatorUrl: stringOrNullFrom('null'),
  customScripts: arrayFrom(''),
  docExpansion: 'none',
  oAuth2Enabled: ('false' == 'true'),
  oAuth2ClientId: '',
  oAuth2ClientSecret: '',
  oAuth2Realm: '',
  oAuth2AppName: '',
  oAuth2ScopeSeperator: ' ',
  oAuth2AdditionalQueryStringParams: JSON.parse('{}')
};

即使我以 https 导航到该站点并将 Swashbuckle 方案设置为 https,它仍将 rootUrl 生成为 http.我想因为我使用的是 Swashbuckle,所以我必须使用它来配置 index.html,因为我的代码中没有该文件,所以我猜 Swashbuckle 正在即时生成它.

Even though the I am navigating to the site as https and have the Swashbuckle scheme set to https, it is still generating the rootUrl as http. I think since I'm using Swashbuckle, I have to use it to configure the index.html because I don't have that file anywhere in my code so I guess Swashbuckle is generating it on the fly.

在更改 swagger.json 的路径时,我确实发现了我遗漏了什么.它显然需要那里的端口号.因此,如果我导航到 swagger 索引页面并手动将 json 文件的路径更改为 https://qa-server:443/product-catalog-api/swagger/docs/v1 一切正常美好的.所以现在我想我已经将问题归结为如何使用 Swashbuckle 更改 Swaggers index.html 中的 rootUrl.

I did find out what I was missing when changing the path the swagger.json. It apparently needs the port number on there. So if I navigate to the swagger index page and manually change the path to the json file to be https://qa-server:443/product-catalog-api/swagger/docs/v1 everything works fine. So now I think I have isolated the problem down to how do I change the rootUrl in Swaggers index.html using Swashbuckle.

编辑 2

好吧,我想我已经正确配置了 Swashbuckle,因为它在我们的开发服务器上正确生成了 index.html,但不是 qa,所以我想剩下的问题归结为环境的一些差异,或者我的包没有得到正确安装在 qa 中.

Well, I think I have Swashbuckle configured correctly because it generates the index.html correctly on our dev server, but not qa so I guess the rest of the problem is down to some difference in environments or my package didn't get installed in qa properly.

开发:

window.swashbuckleConfig = {
  rootUrl: 'https://server-dev:443/product-catalog-api',
  discoveryPaths: arrayFrom('swagger/docs/v1'),
  booleanValues: arrayFrom('true|false'),
  validatorUrl: stringOrNullFrom('null'),
  customScripts: arrayFrom(''),
  docExpansion: 'none',
  oAuth2Enabled: ('false' == 'true'),
  oAuth2ClientId: '',
  oAuth2ClientSecret: '',
  oAuth2Realm: '',
  oAuth2AppName: '',
  oAuth2ScopeSeperator: ' ',
  oAuth2AdditionalQueryStringParams: JSON.parse('{}')
};

质量检查:

window.swashbuckleConfig = {
  rootUrl: 'http://qa-server:80/product-catalog-api',
  discoveryPaths: arrayFrom('swagger/docs/v1'),
  booleanValues: arrayFrom('true|false'),
  validatorUrl: stringOrNullFrom('null'),
  customScripts: arrayFrom(''),
  docExpansion: 'none',
  oAuth2Enabled: ('false' == 'true'),
  oAuth2ClientId: '',
  oAuth2ClientSecret: '',
  oAuth2Realm: '',
  oAuth2AppName: '',
  oAuth2ScopeSeperator: ' ',
  oAuth2AdditionalQueryStringParams: JSON.parse('{}')
};

编辑 3

我们进行了测试以进一步隔离问题.我们的 QA 环境中有一个 A10 负载平衡器.我们为 dev 环境立了一个新的 A10 来看看发生了什么,现在我们在 dev 中遇到了同样的问题.A10 正在执行一些 http 标头操作,我们将其删除以查看是否是问题所在,但仍然得到相同的结果.我相信随着服务器的设置方式,SSL 正在被卸载到 A10 并且实际运行我的代码的盒子正在获取 http.因此,当 Swashbuckle 代码运行时,它是在 http 下运行导致问题的.我想我需要一种方法来强制它始终为 https.

We did a test to further isolate the problem. We have an A10 load balancer in our QA environment. We stood up a new A10 for the dev environment to see what happened, and we now have the same problem in dev. The A10 was doing some http header manipulation that we removed to see if that was the problem but still getting the same thing. I believe with the way the servers are setup, SSL is being offloaded to the A10 and the box actually running my code is getting http. So when the Swashbuckle code runs, it is running under http causing the problem. I think I need a way to force it to always be https.

推荐答案

我终于明白了!感谢 Sampada 和 strick01 帮助我隔离问题.我在github上找到了这篇文章,其中有使用Swashbuckle强制https的解决方案:

I finally got it! Thanks to Sampada and strick01 for helping me isolate the problem. I found this article on github with the solution of forcing https with Swashbuckle:

https://github.com/domaindrivendev/Swashbuckle/issues/296

config
    .EnableSwagger("docs/{apiVersion}",
    c =>
    {
      ...
      c.RootUrl(ResolveBasePath);
      ...
    })
    .EnableSwaggerUi();

private static string ResolveBasePath(HttpRequestMessage message)
{
    var virtualPathRoot = message.GetRequestContext().VirtualPathRoot;

    var schemeAndHost = "https://" + message.RequestUri.Host;
    return new Uri(new Uri(schemeAndHost, UriKind.Absolute), virtualPathRoot).AbsoluteUri;
}

这篇关于如何让 Swagger UI 将端口 443 与 Swashbuckle 一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆