diff --git a/SwaggerStudy/Controllers/StudentController.cs b/SwaggerStudy/Controllers/StudentController.cs
index 42c909b..3b7a929 100644
--- a/SwaggerStudy/Controllers/StudentController.cs
+++ b/SwaggerStudy/Controllers/StudentController.cs
@@ -1,4 +1,5 @@
-using Microsoft.AspNetCore.Http;
+using Microsoft.AspNetCore.Authorization;
+using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
@@ -29,6 +30,7 @@ namespace SwaggerStudy.Controllers
_studentServer = studentServer;
}
+ [AllowAnonymous]
[HttpGet]
public ResultBase GetAll()
{
@@ -67,6 +69,7 @@ namespace SwaggerStudy.Controllers
///
/// 我是Remark注释
/// 学生Id(变量在路由中时,example才有用)
+ [AllowAnonymous]
[HttpGet("{studentId:int}")]
public IActionResult Get([FromRoute]int studentId)
{
@@ -80,6 +83,7 @@ namespace SwaggerStudy.Controllers
return Ok(apiResult);
}
+ [AllowAnonymous]
[HttpGet]
[ActionName("GetAsync")]
public async Task GetAsync(int studentId)
diff --git a/SwaggerStudy/Documents/README_zh.md b/SwaggerStudy/Documents/README_zh.md
index c9cbb96..e780072 100644
--- a/SwaggerStudy/Documents/README_zh.md
+++ b/SwaggerStudy/Documents/README_zh.md
@@ -836,7 +836,7 @@ services.AddSwaggerGen(c =>
};
```
-_NOTE: Filter pipelines are DI-aware. That is, you can create filters with constructor parameters and if the parameter types are registered with the DI framework, they'll be automatically injected when the filters are instantiated_
+_注意: 过滤器管道可识别。也就是说,您可以使用构造函数参数创建过滤器,如果参数类型注册到DI框架中,那么当过滤器被实例化时,它们将被自动注入_
#### Schema 筛选器 ####
diff --git a/SwaggerStudy/Filters/AuthResponsesOperationFilter.cs b/SwaggerStudy/Filters/AuthResponsesOperationFilter.cs
new file mode 100644
index 0000000..0825b73
--- /dev/null
+++ b/SwaggerStudy/Filters/AuthResponsesOperationFilter.cs
@@ -0,0 +1,32 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Threading.Tasks;
+using Microsoft.AspNetCore.Authorization;
+using Microsoft.OpenApi.Models;
+using Swashbuckle.AspNetCore.Swagger;
+using Swashbuckle.AspNetCore.SwaggerGen;
+using Swashbuckle.AspNetCore.SwaggerUI;
+
+namespace SwaggerStudy
+{
+ ///
+ /// Swagger Operation过滤器
+ /// 列出了用AuthorizeAttribute修饰的所有操作附加"401"响应
+ ///
+ public class AuthResponsesOperationFilter : IOperationFilter
+ {
+ public void Apply(OpenApiOperation operation, OperationFilterContext context)
+ {
+ var authAttributes = context.MethodInfo.DeclaringType.GetCustomAttributes(true)
+ .Union(context.MethodInfo.GetCustomAttributes(true));
+ var cc = authAttributes .OfType();
+
+ if (authAttributes .OfType().Any() || authAttributes .OfType().Any())
+ {
+ operation.Responses.Add("401", new OpenApiResponse { Description = "未授权" });
+ operation.Responses.Add("403", new OpenApiResponse { Description = "" });
+ }
+ }
+ }
+}
diff --git a/SwaggerStudy/StartupTest.cs b/SwaggerStudy/StartupTest.cs
index 6f2d2db..6ae9e68 100644
--- a/SwaggerStudy/StartupTest.cs
+++ b/SwaggerStudy/StartupTest.cs
@@ -92,6 +92,10 @@ namespace SwaggerStudy
return i;
});
+ #region 过滤器
+ setup.OperationFilter();
+ #endregion
+
});
//配置SwaggerGen