在項目中引用Swashbuckle.AspNetCore和Swashbuckle.AspNetCore.Filters兩個dll,在Startup中的ConfigureServices相關配置代碼如下
兩個重點:
1、options.DocumentFilter<HiddenApiFilter>();定義那些接口方法被隱藏
2、啓用oauth2安全授權訪問api接口
options.OperationFilter<SecurityRequirementsOperationFilter>();
//給api添加token令牌證書
options.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme
{
Description = "JWT授權(數據將在請求頭中進行傳輸) 直接在下框中輸入Bearer {token}(注意兩者之間是一個空格)\"",
Name = "Authorization",//jwt默認的參數名稱
In = ParameterLocation.Header,//jwt默認存放Authorization信息的位置(請求頭中)
Type = SecuritySchemeType.ApiKey
});
其中使用SecurityRequirementsOperationFilter需要在控制器頭部加[Authorization]或則方法頭部加[Authorization],如下:
[Authorize]
public class TokenController : ControllerBase
或者
[Authorize("Customer")]
public PersonResponse GetPerson([FromBody]PersonRequest personRequest)
這樣在每個接口才會有小鎖出現。
更多介紹請參考https://github.com/domaindrivendev/Swashbuckle.AspNetCore和https://github.com/mattfrear/Swashbuckle.AspNetCore.Filters
原文地址:https://www.cnblogs.com/cqinwn/p/10918206.html
.NET社區新聞,深度好文,歡迎訪問公衆號文章彙總 http://www.csharpkit.com