当前位置: 首页 > news >正文

.net 7 隐藏swagger的api

1.写一个隐藏接口特性表示 

using Microsoft.AspNetCore.Mvc.ApiExplorer;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;using System.Web.Http.Description;namespace JiaTongInterface.Filter
{public class SwaggerApi : Swashbuckle.AspNetCore.SwaggerGen.IDocumentFilter{/// <summary>/// 隐藏swagger接口特性标识/// </summary>[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class)]public class HideApiAttribute : System.Attribute{}public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context){foreach (Microsoft.AspNetCore.Mvc.ApiExplorer.ApiDescription description in context.ApiDescriptions){if (description.TryGetMethodInfo(out MethodInfo method)){if (method.ReflectedType.CustomAttributes.Any(t => t.AttributeType == typeof(HideApiAttribute))|| method.CustomAttributes.Any(t => t.AttributeType == typeof(HideApiAttribute))){string key = "/" + description.RelativePath;if (key.Contains("?")){int idx = key.IndexOf("?", System.StringComparison.Ordinal);key = key.Substring(0, idx);}swaggerDoc.Paths.Remove(key);}}}}}}

2.在porgram.cs中加入option.DocumentFilter<SwaggerApi>();

builder.Services.AddSwaggerGen(option =>
{var xmlFilename = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";option.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, xmlFilename), true);option.DocumentFilter<SwaggerApi>();
});

3.使用方法,在controlller前加入[SwaggerApi.HideApi]即可实现隐藏

4.效果 这里就看不到接口了

http://www.lryc.cn/news/164386.html

相关文章:

  • Maven插件的作用
  • C++(三)——运算符重载
  • 【Springcloud】elk分布式日志
  • 华为mate60麒麟9000s的架构体系
  • 面试半个月后的一些想法
  • 基于FPGA的图像二值化处理,包括tb测试文件和MATLAB辅助验证
  • 文件操作(个人学习笔记黑马学习)
  • Android Studio新版本New UI及相关设置丨遥遥领先版
  • 新型人工智能技术让机器人的识别能力大幅提升
  • 聚观早报|蚂蚁集团发布“蚁天鉴”;vivo X100系列即将亮相
  • 读高性能MySQL(第4版)笔记05_优化服务器设置
  • Spring Boot跨域问题简介
  • 【Java】过滤器和拦截器区别
  • es滚动查询分析和使用步骤
  • 飞书公式总结
  • vue3.2 导出pdf文件或表格数据
  • Linux学习--MySQL学习之查询语句
  • 三、视频设备的枚举以及插拔检测
  • Qt开发_调用OpenCV(4.x)完成人脸检测并绘制马赛克(摄像头实时数据)
  • ssl证书有效期为什么越来越短?
  • XFF漏洞利用([SWPUCTF 2021 新赛]Do_you_know_http)
  • Java——》JVM对原生的锁做了哪些优化
  • 华为云云耀云服务器L实例评测|用docker搭建frp服务测试
  • 群狼调研(长沙满意度调查专业公司)开展公交车乘客满意度调查
  • Spring与OAuth2:实现第三方认证和授权的最佳实践
  • Mysql的定时备份与恢复
  • 学习Java基础面试题第五天
  • (10)(10.9) 术语表(一)
  • 面试(类加载器)
  • 二维差分---基础算法