1- using System . Collections . Generic ;
1+ using System . Collections . Generic ;
22using System . Linq ;
33using Swashbuckle . AspNetCore . Swagger ;
44using Swashbuckle . AspNetCore . SwaggerGen ;
@@ -10,21 +10,33 @@ public class SecurityRequirementsOperationFilter : IOperationFilter
1010 {
1111 public void Apply ( Operation operation , OperationFilterContext context )
1212 {
13- var controllerPermissions = context . ApiDescription . ControllerAttributes ( )
14- . OfType < AbpAuthorizeAttribute > ( )
15- . Select ( attr => attr . Permissions ) ;
13+ var actionAttrs = context . ApiDescription . ActionAttributes ( ) ;
14+ if ( actionAttrs . OfType < AbpAllowAnonymousAttribute > ( ) . Any ( ) )
15+ {
16+ return ;
17+ }
1618
17- var actionPermissions = context . ApiDescription . ActionAttributes ( )
18- . OfType < AbpAuthorizeAttribute > ( )
19- . Select ( attr => attr . Permissions ) ;
19+ var controllerAttrs = context . ApiDescription . ControllerAttributes ( ) ;
20+ var actionAbpAuthorizeAttrs = actionAttrs . OfType < AbpAuthorizeAttribute > ( ) ;
2021
21- var permissions = controllerPermissions . Union ( actionPermissions ) . Distinct ( )
22- . SelectMany ( p => p ) ;
22+ if ( ! actionAbpAuthorizeAttrs . Any ( ) && controllerAttrs . OfType < AbpAllowAnonymousAttribute > ( ) . Any ( ) )
23+ {
24+ return ;
25+ }
2326
24- if ( permissions . Any ( ) )
27+ var controllerAbpAuthorizeAttrs = controllerAttrs . OfType < AbpAuthorizeAttribute > ( ) ;
28+ if ( controllerAbpAuthorizeAttrs . Any ( ) || actionAbpAuthorizeAttrs . Any ( ) )
2529 {
2630 operation . Responses . Add ( "401" , new Response { Description = "Unauthorized" } ) ;
27- operation . Responses . Add ( "403" , new Response { Description = "Forbidden" } ) ;
31+
32+ var permissions = controllerAbpAuthorizeAttrs . Union ( actionAbpAuthorizeAttrs )
33+ . SelectMany ( p => p . Permissions )
34+ . Distinct ( ) ;
35+
36+ if ( permissions . Any ( ) )
37+ {
38+ operation . Responses . Add ( "403" , new Response { Description = "Forbidden" } ) ;
39+ }
2840
2941 operation . Security = new List < IDictionary < string , IEnumerable < string > > >
3042 {
0 commit comments