Jus*_*tte 5 swagger swashbuckle
在我们运行RESTful Web服务的QA和Prod环境中,端口80未打开.所以目前当我尝试在QA中使用Swagger UI时,我收到此消息并且它只是挂起:
fetching resource list: http://qa-server:80/product-catalog-api/swagger/docs/v1; Please wait.
我正在使用Swashbuckle来配置Swagger.我也在配置中更改了这一行,但它仍然无效.
// 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" });
Run Code Online (Sandbox Code Playgroud)
SSL端口443是打开的,所以我想在Swagger UI上运行它.我可以手动更改http://qa-server:80/product-catalog-api/swagger/docs/v1到https://qa-server/product-catalog-api/swagger/docs/v1再扬鞭会列出我的网络的方法,但是当我点击它挂起Try it out! 这是从控制台输出:SCRIPT5: Access is denied. File: swagger-ui-min-js, Line: 10, Column: 4300
编辑:
所以我一直在挖掘更多,并且已经变得更远但仍然不在我想要的地方.如果我在Swagger index.html文件上查看源代码,我可以看到问题:
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('{}')
};
Run Code Online (Sandbox Code Playgroud)
即使我以https方式导航到网站并将Swashbuckle方案设置为https,它仍然将rootUrl生成为http.我想因为我使用的是Swashbuckle,所以我必须使用它来配置index.html,因为我的代码中没有任何文件,所以我想Swashbuckle会动态生成它.
我确实在改变swagger.json的路径时发现了我所遗漏的内容.它显然需要那里的端口号.因此,如果我导航到swagger索引页面并手动更改json文件的路径,那么https://qa-server:443/product-catalog-api/swagger/docs/v1一切正常.所以现在我认为我已经将问题分解为如何使用Swashbuckle更改Swaggers index.html中的rootUrl.
编辑2
好吧,我认为我已正确配置Swashbuckle,因为它在我们的开发服务器上正确生成index.html,但不是qa所以我猜其余的问题是由于环境有些不同或者我的包没有安装在qa中正常.
DEV:
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('{}')
};
Run Code Online (Sandbox Code Playgroud)
QA:
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('{}')
};
Run Code Online (Sandbox Code Playgroud)
编辑3
我们做了一个测试,以进一步隔离问题.我们的质量保证环境中有一个A10负载均衡器.我们为开发环境设置了一个新的A10,看看发生了什么,我们现在在开发中遇到了同样的问题.A10正在做一些我们删除的http标头操作,看看是不是问题,但仍然得到同样的东西.我相信服务器的设置方式,SSL正在卸载到A10,实际运行我的代码的盒子正在获取http.因此,当Swashbuckle代码运行时,它在http下运行导致问题.我想我需要一种方法来强制它始终是https.
Jus*_*tte 12
我终于明白了!感谢Sampada和strick01帮助我解决问题.我在github上发现了这篇文章,其中包含使用Swashbuckle强制https的解决方案:
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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5340 次 |
| 最近记录: |