首页 \ 问答 \ 用Swagger隐藏服务(Hiding services with Swagger)

用Swagger隐藏服务(Hiding services with Swagger)

有什么方法可以在进入生产时从swagger UI中隐藏某些服务但是在localhost上运行时会显示?

EX:

    /// <summary>
    /// GET: .../api/SomeController/{id: int}
    /// </summary>
    /// <param name="id">int</param>
    /// <returns></returns>
public IHttpActionResult SomeService(int id) { return Ok();}

当我在我的环境中运行并且去swagger UI localhost:12345 / swagger / ui / index我会看到服务文档等,但是当我在http:// someDomain / swagger / ui / index时我不会看见。

我一直在查看文档,但没有找到任何与此相关的内容。

谢谢。


is there any way that some services can be hidden from swagger UI when going into production but when run on localhost be displayed?

EX:

    /// <summary>
    /// GET: .../api/SomeController/{id: int}
    /// </summary>
    /// <param name="id">int</param>
    /// <returns></returns>
public IHttpActionResult SomeService(int id) { return Ok();}

When i'm running on my environment and go to swagger UI localhost:12345/swagger/ui/index i will see that service documentation etc, but when i'm on http://someDomain/swagger/ui/index i will not see it.

I've been looking in the documentation but didn't find anything related to this.

Thank you.


原文:https://stackoverflow.com/questions/41037162
更新时间:2021-06-17 11:06

最满意答案

您可以创建自己的属性并使用它从swagger中排除该方法或整个控制器:

[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class)]
public class HideInDocsAttribute:Attribute
{
}

接着:

public class HideInDocsFilter:IDocumentFilter
{
    public void Apply(SwaggerDocument swaggerDoc, SchemaRegistry schemaRegistry, IApiExplorer apiExplorer, IHostingEnvironment env)
    {
        if(env.IsEnvironment("Production")) {
            foreach (var apiDescription in apiExplorer.ApiDescriptions)
            {
                if (!apiDescription.ActionDescriptor.ControllerDescriptor.GetCustomAttributes<HideInDocsAttribute>().Any() && !apiDescription.ActionDescriptor.GetCustomAttributes<HideInDocsAttribute>().Any()) continue;
                var route = "/" + apiDescription.Route.RouteTemplate.TrimEnd('/');
                swaggerDoc.paths.Remove(route);
            }
        }            
    }
}

You can create your own attribute and use it to exclude that method or whole controller from swagger:

[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class)]
public class HideInDocsAttribute:Attribute
{
}

And then:

public class HideInDocsFilter:IDocumentFilter
{
    public void Apply(SwaggerDocument swaggerDoc, SchemaRegistry schemaRegistry, IApiExplorer apiExplorer, IHostingEnvironment env)
    {
        if(env.IsEnvironment("Production")) {
            foreach (var apiDescription in apiExplorer.ApiDescriptions)
            {
                if (!apiDescription.ActionDescriptor.ControllerDescriptor.GetCustomAttributes<HideInDocsAttribute>().Any() && !apiDescription.ActionDescriptor.GetCustomAttributes<HideInDocsAttribute>().Any()) continue;
                var route = "/" + apiDescription.Route.RouteTemplate.TrimEnd('/');
                swaggerDoc.paths.Remove(route);
            }
        }            
    }
}

相关问答

更多
  • 注意,对于Swagger 2.0(OpenAPI Specification 2.0),Parameter对象允许将type设置为file : consumes: - multipart/form-data # and/or application/x-www-form-urlencoded parameters: - name: file in: formData description: The uploaded file data requ ...
  • 我刚为Apache Wink做了一个项目。 你可以在下面找到它 https://github.com/zouzias/swagger-jaxrs-wink 我也会尽快将其推向swagger-core样品系列。 I have just made a project for Apache Wink. You can find it below https://github.com/zouzias/swagger-jaxrs-wink I will push it to the swagger-core samp ...
  • 我正在使用另一个应用程序来模仿它的服务,它可能运行的是旧版本的JHipster,Swagger ......并且这使问题再次出现,但这次是在Edge上。 删除浏览器的缓存文件可修复Edge和Chrome的错误。 I was using another app to mimic its services and it was probably running older versions of JHipster, Swagger... and that made the problem appear agai ...
  • 例 有关Swagger-servlet modeule的更多信息,请参见此处 Example More information on Swagger-servlet modeule can be found here
  • 实际上,这将是一个与泽西4.1杰克逊库版本有关的问题。 我最终做了什么来完成它的工作: 增加Swagger版本(感谢Ron没有注意到这个新版本) 在{glassfish_install}/glassfish/modules取代最后一个版本(此时为2.6)的Jackson jars在此处找到 删除了我的域( {glassfish_install}/glassfish/domains/domain1/osgi-cache/felix )的{glassfish_install}/glassfish/domains ...
  • 您可以创建自己的属性并使用它从swagger中排除该方法或整个控制器: [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class)] public class HideInDocsAttribute:Attribute { } 接着: public class HideInDocsFilter:IDocumentFilter { public void Apply(SwaggerDocument swaggerDoc, Sch ...
  • swagger-tools解决了我的问题。 在解析每个swagger文件后,我已经递归地创建了管道。 swagger-tools solved my question. I've created the pipeline recursively after parsing each swagger file.
  • 问题的两个部分: 1)现在无法在EasyAPI上启用Swagger。 也就是说,我们不合并也不生成Swagger for EasyAPI,因为所有参数都在代码中定义。 2)如果要启用Swagger,则需要在API上方的app.js文件中将“swagger:true”添加到Azure移动应用程序的初始化程序中。 进入Easy API或Easy Tables并编辑任何API /表。 这将打开编辑器。 转到上面的目录(包含api和tables目录的目录)并在那里编辑app.js文件。 Two parts to y ...
  • 目前它是GReg-5.1.0的限制。 https://wso2.org/jira/browse/REGISTRY-3195 有一个解决方法,这将在产品的更高版本中正确修复。 It is currently a limitation with GReg-5.1.0. https://wso2.org/jira/browse/REGISTRY-3195 Has a workaround, and this will be properly fixed in a later version of the prod ...
  • 我解决了官方github资源库Swashbuckle的问题 。 问题是由于OWIN引起的。 我想安装包Swashbuckle.Core 。 I was able to solve the issue fallowing the official github respository Swashbuckle. The problem was due OWIN. I miss installed the package Swashbuckle.Core.

相关文章

更多

最新问答

更多
  • 如何重新加载下拉列表(How to reload dropdownlist)
  • RStudio:在脚本中保留特殊字符(RStudio: keeping special characters in a script)
  • Powershell的“GetLatest”不会在新的TFS工作区上下载文件(“GetLatest” with Powershell doesn't download files on new TFS workspace)
  • 我如何让JS识别一个由字符组成的数组?(How do I get JS to recognise an array insted of characters?)
  • EF从存储过程中急切加载(EF eager loading from stored procedure)
  • 将输出文件添加到Python扩展(Adding output file to Python extension)
  • 淮北职业技术学院电脑应用专业咋样?
  • 更改默认扩展面板箭头的箭头样式(Change arrow style for default expansion panel arrow)
  • 芜湖计算机(计算机)培训机构(培训班,学校)哪家好
  • 致命错误:使用clang-llvm ASTMatcher时未找到'stddef.h'文件(fatal error: 'stddef.h' file not found when using clang-llvm ASTMatcher)
  • 内容的.NET缓存(Contentful .NET caching)
  • 客户端没有发生WCF回调(WCF callback is not happening in client)
  • 使用friend在全局范围内调用类成员函数会产生27个错误(Calling a Class member function in Global Scope using friend Gives 27 ERRORS)
  • 如何绑定到WPF中的另一个控件属性(How to Bind to Another Control Property in WPF)
  • 南华大学电脑专业,就业好不好
  • 是否存在泄密文件的官方(或常见)文件扩展名或后缀?(Is there an official (or common) file extention or suffix for deflated files?)
  • 在SVM python中只训练一次(Training only once in SVM python)
  • 淘汰自定义绑定光滑js无法正常工作(knockout custom binding for slick js not working)
  • 似乎无法正确地抓住网站“福布斯”(Can't seem to scrape the website “Forbes” properly)
  • 无法使用boto.rds2从describe_instance方法检索有关db实例的信息(Not able to retrieve information about db instances from the describe_instance method using boto.rds2)
  • 转换为英国日期格式问题(Convert to british date format issue)
  • 在表中列出不同的元组(10种方法)(List distinct tuples in a table(SQL query)(10 ways))
  • OrientDB查询比较(OrientDB query compare)
  • 全局变量有什么不好?(What is so bad about global variables? [duplicate])
  • 为什么JavaMail Transport.send()是一个静态方法?(Why is JavaMail Transport.send() a static method?)
  • 获取最近3个Instagram图像张贴在一个地方(Get last 3 instagram images posted in a place)
  • 使用libnfc格式化/读/写NDEF Mifare 1K卡(Format/Read/Write NDEF Mifare 1K Card using libnfc)
  • 阻止谷歌索引特定图像(Block Google from indexing a particular image)
  • 消息模板接收让Dispatcher没有订阅频道(Message Template receive gives Dispatcher has no subscribers for channel)
  • OpenShift:使用自定义节点版本(OpenShift: Use custom node version)