From bc13da3e9f4ce609dc8cab7dba5166f7c4044349 Mon Sep 17 00:00:00 2001 From: maliming Date: Sat, 30 Dec 2023 17:54:45 +0800 Subject: [PATCH 1/4] Provide a way for the controller to skip all interceptors and filters. Resolve #18635 --- .../MultiTenancy/MultiTenancyMiddleware.cs | 5 ++-- .../AspNetCore/Mvc/AbpAspNetCoreMvcModule.cs | 5 +++- .../Mvc/AbpMvcActionDescriptorProvider.cs | 30 +++++++++++++++++++ .../Mvc/Auditing/AbpAuditActionFilter.cs | 3 +- .../Mvc/Auditing/AbpAuditPageFilter.cs | 3 +- .../ExceptionHandling/AbpExceptionFilter.cs | 3 +- .../AbpExceptionPageFilter.cs | 3 +- .../Mvc/Features/AbpFeatureActionFilter.cs | 3 +- .../Mvc/Features/AbpFeaturePageFilter.cs | 3 +- .../GlobalFeatureActionFilter.cs | 3 +- .../GlobalFeatures/GlobalFeaturePageFilter.cs | 3 +- .../Mvc/Response/AbpNoContentActionFilter.cs | 3 +- .../AspNetCore/Mvc/Uow/AbpUowActionFilter.cs | 3 +- .../AspNetCore/Mvc/Uow/AbpUowPageFilter.cs | 3 +- .../Validation/AbpValidationActionFilter.cs | 3 +- .../Serilog/AbpSerilogMiddleware.cs | 5 ++-- .../AbpRequestLocalizationMiddleware.cs | 5 ++-- .../Auditing/AbpAuditingMiddleware.cs | 7 +++-- .../AbpExceptionHandlingMiddleware.cs | 5 ++-- .../Filters/DisableAbpFilterAttribute.cs | 19 ++++++++++++ .../Volo/Abp/AspNetCore/Filters/IAbpFilter.cs | 6 ++++ .../Middleware/AbpMiddlewareBase.cs | 20 +++++++++++++ .../Security/AbpSecurityHeadersMiddleware.cs | 5 ++-- .../Security/Claims/AbpClaimsMapMiddleware.cs | 5 ++-- .../Claims/AbpDynamicClaimsMiddleware.cs | 5 ++-- .../Tracing/AbpCorrelationIdMiddleware.cs | 5 ++-- .../AspNetCore/Uow/AbpUnitOfWorkMiddleware.cs | 7 +++-- .../AbpRegistrationBuilderExtensions.cs | 4 ++- .../DisableInterceptorAttribute.cs | 9 ++++++ .../Mvc/FakeAuthenticationMiddleware.cs | 5 ++-- 30 files changed, 150 insertions(+), 38 deletions(-) create mode 100644 framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpMvcActionDescriptorProvider.cs create mode 100644 framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Filters/DisableAbpFilterAttribute.cs create mode 100644 framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Filters/IAbpFilter.cs create mode 100644 framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Middleware/AbpMiddlewareBase.cs create mode 100644 framework/src/Volo.Abp.Core/Volo/Abp/DynamicProxy/DisableInterceptorAttribute.cs diff --git a/framework/src/Volo.Abp.AspNetCore.MultiTenancy/Volo/Abp/AspNetCore/MultiTenancy/MultiTenancyMiddleware.cs b/framework/src/Volo.Abp.AspNetCore.MultiTenancy/Volo/Abp/AspNetCore/MultiTenancy/MultiTenancyMiddleware.cs index 310add4bfc..520e2944f6 100644 --- a/framework/src/Volo.Abp.AspNetCore.MultiTenancy/Volo/Abp/AspNetCore/MultiTenancy/MultiTenancyMiddleware.cs +++ b/framework/src/Volo.Abp.AspNetCore.MultiTenancy/Volo/Abp/AspNetCore/MultiTenancy/MultiTenancyMiddleware.cs @@ -8,6 +8,7 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.Options; +using Volo.Abp.AspNetCore.Middleware; using Volo.Abp.DependencyInjection; using Volo.Abp.Localization; using Volo.Abp.MultiTenancy; @@ -15,7 +16,7 @@ using Volo.Abp.Settings; namespace Volo.Abp.AspNetCore.MultiTenancy; -public class MultiTenancyMiddleware : IMiddleware, ITransientDependency +public class MultiTenancyMiddleware : AbpMiddlewareBase, ITransientDependency { public ILogger Logger { get; set; } @@ -38,7 +39,7 @@ public class MultiTenancyMiddleware : IMiddleware, ITransientDependency _options = options.Value; } - public async Task InvokeAsync(HttpContext context, RequestDelegate next) + public async override Task InvokeAsync(HttpContext context, RequestDelegate next) { TenantConfiguration? tenant = null; try diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpAspNetCoreMvcModule.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpAspNetCoreMvcModule.cs index c9b5984843..a2f2c1c989 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpAspNetCoreMvcModule.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpAspNetCoreMvcModule.cs @@ -14,6 +14,7 @@ using System.Linq; using System.Net; using System.Reflection; using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Mvc.Abstractions; using Microsoft.AspNetCore.Mvc.ApiExplorer; using Microsoft.AspNetCore.Mvc.DataAnnotations; using Microsoft.AspNetCore.Mvc.RazorPages; @@ -25,6 +26,7 @@ using Volo.Abp.ApiVersioning; using Volo.Abp.Application; using Volo.Abp.AspNetCore.Mvc.AntiForgery; using Volo.Abp.AspNetCore.Mvc.ApiExploring; +using Volo.Abp.AspNetCore.Mvc.ApplicationModels; using Volo.Abp.AspNetCore.Mvc.Conventions; using Volo.Abp.AspNetCore.Mvc.DataAnnotations; using Volo.Abp.AspNetCore.Mvc.DependencyInjection; @@ -176,6 +178,7 @@ public class AbpAspNetCoreMvcModule : AbpModule context.Services.Replace(ServiceDescriptor.Singleton()); context.Services.AddSingleton(); + context.Services.TryAddEnumerable(ServiceDescriptor.Transient()); context.Services.AddOptions() .Configure((mvcOptions, serviceProvider) => { @@ -247,7 +250,7 @@ public class AbpAspNetCoreMvcModule : AbpModule .Distinct(); AddToApplicationParts(partManager, controllerAssemblies); - + var additionalAssemblies = moduleContainer .Modules .SelectMany(m => m.GetAdditionalAssemblies()) diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpMvcActionDescriptorProvider.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpMvcActionDescriptorProvider.cs new file mode 100644 index 0000000000..9131cd44d6 --- /dev/null +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpMvcActionDescriptorProvider.cs @@ -0,0 +1,30 @@ +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.Abstractions; +using Microsoft.AspNetCore.Mvc.Controllers; +using Volo.Abp.AspNetCore.Filters; + +namespace Volo.Abp.AspNetCore.Mvc.ApplicationModels; + +public class AbpMvcActionDescriptorProvider : IActionDescriptorProvider +{ + public virtual int Order => -1000 + 10; + + public virtual void OnProvidersExecuting(ActionDescriptorProviderContext context) + { + } + + public virtual void OnProvidersExecuted(ActionDescriptorProviderContext context) + { + foreach (var action in context.Results.Where(x => x is ControllerActionDescriptor).Cast()) + { + var disableFilterAttribute = action.ControllerTypeInfo.GetCustomAttribute(true); + if (disableFilterAttribute != null) + { + action.FilterDescriptors.RemoveAll(x => x.Filter is ServiceFilterAttribute serviceFilterAttribute && typeof(IAbpFilter).IsAssignableFrom(serviceFilterAttribute.ServiceType)); + } + } + } +} diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Auditing/AbpAuditActionFilter.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Auditing/AbpAuditActionFilter.cs index 07101da0cb..d982adb12a 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Auditing/AbpAuditActionFilter.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Auditing/AbpAuditActionFilter.cs @@ -5,12 +5,13 @@ using Microsoft.AspNetCore.Mvc.Abstractions; using Microsoft.AspNetCore.Mvc.Filters; using Microsoft.Extensions.Options; using Volo.Abp.Aspects; +using Volo.Abp.AspNetCore.Filters; using Volo.Abp.Auditing; using Volo.Abp.DependencyInjection; namespace Volo.Abp.AspNetCore.Mvc.Auditing; -public class AbpAuditActionFilter : IAsyncActionFilter, ITransientDependency +public class AbpAuditActionFilter : IAsyncActionFilter, IAbpFilter, ITransientDependency { public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) { diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Auditing/AbpAuditPageFilter.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Auditing/AbpAuditPageFilter.cs index 8445be60ae..6b97ac980a 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Auditing/AbpAuditPageFilter.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Auditing/AbpAuditPageFilter.cs @@ -5,12 +5,13 @@ using Microsoft.AspNetCore.Mvc.Abstractions; using Microsoft.AspNetCore.Mvc.Filters; using Microsoft.Extensions.Options; using Volo.Abp.Aspects; +using Volo.Abp.AspNetCore.Filters; using Volo.Abp.Auditing; using Volo.Abp.DependencyInjection; namespace Volo.Abp.AspNetCore.Mvc.Auditing; -public class AbpAuditPageFilter : IAsyncPageFilter, ITransientDependency +public class AbpAuditPageFilter : IAsyncPageFilter, IAbpFilter, ITransientDependency { public Task OnPageHandlerSelectionAsync(PageHandlerSelectedContext context) { diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/AbpExceptionFilter.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/AbpExceptionFilter.cs index 61e37104ee..a63e557198 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/AbpExceptionFilter.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/AbpExceptionFilter.cs @@ -10,6 +10,7 @@ using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.Options; using Volo.Abp.AspNetCore.ExceptionHandling; +using Volo.Abp.AspNetCore.Filters; using Volo.Abp.Authorization; using Volo.Abp.DependencyInjection; using Volo.Abp.ExceptionHandling; @@ -18,7 +19,7 @@ using Volo.Abp.Json; namespace Volo.Abp.AspNetCore.Mvc.ExceptionHandling; -public class AbpExceptionFilter : IAsyncExceptionFilter, ITransientDependency +public class AbpExceptionFilter : IAsyncExceptionFilter, IAbpFilter, ITransientDependency { public virtual async Task OnExceptionAsync(ExceptionContext context) { diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/AbpExceptionPageFilter.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/AbpExceptionPageFilter.cs index 31784cb6ea..97c21268fe 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/AbpExceptionPageFilter.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/AbpExceptionPageFilter.cs @@ -10,6 +10,7 @@ using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.Options; using Volo.Abp.AspNetCore.ExceptionHandling; +using Volo.Abp.AspNetCore.Filters; using Volo.Abp.Authorization; using Volo.Abp.DependencyInjection; using Volo.Abp.ExceptionHandling; @@ -18,7 +19,7 @@ using Volo.Abp.Json; namespace Volo.Abp.AspNetCore.Mvc.ExceptionHandling; -public class AbpExceptionPageFilter : IAsyncPageFilter, ITransientDependency +public class AbpExceptionPageFilter : IAsyncPageFilter, IAbpFilter, ITransientDependency { public Task OnPageHandlerSelectionAsync(PageHandlerSelectedContext context) { diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Features/AbpFeatureActionFilter.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Features/AbpFeatureActionFilter.cs index b4b70a5e1a..35bb9ba572 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Features/AbpFeatureActionFilter.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Features/AbpFeatureActionFilter.cs @@ -2,12 +2,13 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc.Abstractions; using Volo.Abp.Aspects; +using Volo.Abp.AspNetCore.Filters; using Volo.Abp.DependencyInjection; using Volo.Abp.Features; namespace Volo.Abp.AspNetCore.Mvc.Features; -public class AbpFeatureActionFilter : IAsyncActionFilter, ITransientDependency +public class AbpFeatureActionFilter : IAsyncActionFilter, IAbpFilter, ITransientDependency { public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) { diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Features/AbpFeaturePageFilter.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Features/AbpFeaturePageFilter.cs index 3c586f762b..a35764c43c 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Features/AbpFeaturePageFilter.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Features/AbpFeaturePageFilter.cs @@ -2,12 +2,13 @@ using Microsoft.AspNetCore.Mvc.Abstractions; using Microsoft.AspNetCore.Mvc.Filters; using Volo.Abp.Aspects; +using Volo.Abp.AspNetCore.Filters; using Volo.Abp.DependencyInjection; using Volo.Abp.Features; namespace Volo.Abp.AspNetCore.Mvc.Features; -public class AbpFeaturePageFilter : IAsyncPageFilter, ITransientDependency +public class AbpFeaturePageFilter : IAsyncPageFilter, IAbpFilter, ITransientDependency { public Task OnPageHandlerSelectionAsync(PageHandlerSelectedContext context) { diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/GlobalFeatures/GlobalFeatureActionFilter.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/GlobalFeatures/GlobalFeatureActionFilter.cs index aae85e4d25..34f125f228 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/GlobalFeatures/GlobalFeatureActionFilter.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/GlobalFeatures/GlobalFeatureActionFilter.cs @@ -6,12 +6,13 @@ using Microsoft.AspNetCore.Mvc.Filters; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Abstractions; using Volo.Abp.Aspects; +using Volo.Abp.AspNetCore.Filters; using Volo.Abp.DependencyInjection; using Volo.Abp.GlobalFeatures; namespace Volo.Abp.AspNetCore.Mvc.GlobalFeatures; -public class GlobalFeatureActionFilter : IAsyncActionFilter, ITransientDependency +public class GlobalFeatureActionFilter : IAsyncActionFilter, IAbpFilter, ITransientDependency { public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) { diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/GlobalFeatures/GlobalFeaturePageFilter.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/GlobalFeatures/GlobalFeaturePageFilter.cs index 46455730a4..3c43301221 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/GlobalFeatures/GlobalFeaturePageFilter.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/GlobalFeatures/GlobalFeaturePageFilter.cs @@ -6,12 +6,13 @@ using Microsoft.AspNetCore.Mvc.Filters; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Abstractions; using Volo.Abp.Aspects; +using Volo.Abp.AspNetCore.Filters; using Volo.Abp.DependencyInjection; using Volo.Abp.GlobalFeatures; namespace Volo.Abp.AspNetCore.Mvc.GlobalFeatures; -public class GlobalFeaturePageFilter : IAsyncPageFilter, ITransientDependency +public class GlobalFeaturePageFilter : IAsyncPageFilter, IAbpFilter, ITransientDependency { public Task OnPageHandlerSelectionAsync(PageHandlerSelectedContext context) { diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Response/AbpNoContentActionFilter.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Response/AbpNoContentActionFilter.cs index b0565cbc4d..455dd52f24 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Response/AbpNoContentActionFilter.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Response/AbpNoContentActionFilter.cs @@ -2,11 +2,12 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc.Abstractions; using Microsoft.AspNetCore.Mvc.Filters; +using Volo.Abp.AspNetCore.Filters; using Volo.Abp.DependencyInjection; namespace Volo.Abp.AspNetCore.Mvc.Response; -public class AbpNoContentActionFilter : IAsyncActionFilter, ITransientDependency +public class AbpNoContentActionFilter : IAsyncActionFilter, IAbpFilter, ITransientDependency { public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) { diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Uow/AbpUowActionFilter.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Uow/AbpUowActionFilter.cs index 14032b52d5..4a6a04a554 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Uow/AbpUowActionFilter.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Uow/AbpUowActionFilter.cs @@ -4,12 +4,13 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc.Abstractions; using Microsoft.AspNetCore.Mvc.Filters; using Microsoft.Extensions.Options; +using Volo.Abp.AspNetCore.Filters; using Volo.Abp.DependencyInjection; using Volo.Abp.Uow; namespace Volo.Abp.AspNetCore.Mvc.Uow; -public class AbpUowActionFilter : IAsyncActionFilter, ITransientDependency +public class AbpUowActionFilter : IAsyncActionFilter, IAbpFilter, ITransientDependency { public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) { diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Uow/AbpUowPageFilter.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Uow/AbpUowPageFilter.cs index 7cfc1f0492..f4a7be3333 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Uow/AbpUowPageFilter.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Uow/AbpUowPageFilter.cs @@ -4,13 +4,14 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc.Abstractions; using Microsoft.AspNetCore.Mvc.Filters; using Microsoft.Extensions.Options; +using Volo.Abp.AspNetCore.Filters; using Volo.Abp.AspNetCore.Uow; using Volo.Abp.DependencyInjection; using Volo.Abp.Uow; namespace Volo.Abp.AspNetCore.Mvc.Uow; -public class AbpUowPageFilter : IAsyncPageFilter, ITransientDependency +public class AbpUowPageFilter : IAsyncPageFilter, IAbpFilter, ITransientDependency { public Task OnPageHandlerSelectionAsync(PageHandlerSelectedContext context) { diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Validation/AbpValidationActionFilter.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Validation/AbpValidationActionFilter.cs index e88cee4eb6..f866c85585 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Validation/AbpValidationActionFilter.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Validation/AbpValidationActionFilter.cs @@ -3,13 +3,14 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc.Abstractions; using Microsoft.AspNetCore.Mvc.Filters; using Microsoft.Extensions.Options; +using Volo.Abp.AspNetCore.Filters; using Volo.Abp.DependencyInjection; using Volo.Abp.Reflection; using Volo.Abp.Validation; namespace Volo.Abp.AspNetCore.Mvc.Validation; -public class AbpValidationActionFilter : IAsyncActionFilter, ITransientDependency +public class AbpValidationActionFilter : IAsyncActionFilter, IAbpFilter, ITransientDependency { public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) { diff --git a/framework/src/Volo.Abp.AspNetCore.Serilog/Volo/Abp/AspNetCore/Serilog/AbpSerilogMiddleware.cs b/framework/src/Volo.Abp.AspNetCore.Serilog/Volo/Abp/AspNetCore/Serilog/AbpSerilogMiddleware.cs index 1df327b742..52ee2e6b76 100644 --- a/framework/src/Volo.Abp.AspNetCore.Serilog/Volo/Abp/AspNetCore/Serilog/AbpSerilogMiddleware.cs +++ b/framework/src/Volo.Abp.AspNetCore.Serilog/Volo/Abp/AspNetCore/Serilog/AbpSerilogMiddleware.cs @@ -5,6 +5,7 @@ using Microsoft.Extensions.Options; using Serilog.Context; using Serilog.Core; using Serilog.Core.Enrichers; +using Volo.Abp.AspNetCore.Middleware; using Volo.Abp.Clients; using Volo.Abp.DependencyInjection; using Volo.Abp.MultiTenancy; @@ -13,7 +14,7 @@ using Volo.Abp.Users; namespace Volo.Abp.AspNetCore.Serilog; -public class AbpSerilogMiddleware : IMiddleware, ITransientDependency +public class AbpSerilogMiddleware : AbpMiddlewareBase, ITransientDependency { private readonly ICurrentClient _currentClient; private readonly ICurrentTenant _currentTenant; @@ -35,7 +36,7 @@ public class AbpSerilogMiddleware : IMiddleware, ITransientDependency _options = options.Value; } - public async Task InvokeAsync(HttpContext context, RequestDelegate next) + public async override Task InvokeAsync(HttpContext context, RequestDelegate next) { var enrichers = new List(); diff --git a/framework/src/Volo.Abp.AspNetCore/Microsoft/AspNetCore/RequestLocalization/AbpRequestLocalizationMiddleware.cs b/framework/src/Volo.Abp.AspNetCore/Microsoft/AspNetCore/RequestLocalization/AbpRequestLocalizationMiddleware.cs index 6fd02a0470..cb531762ba 100644 --- a/framework/src/Volo.Abp.AspNetCore/Microsoft/AspNetCore/RequestLocalization/AbpRequestLocalizationMiddleware.cs +++ b/framework/src/Volo.Abp.AspNetCore/Microsoft/AspNetCore/RequestLocalization/AbpRequestLocalizationMiddleware.cs @@ -4,11 +4,12 @@ using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Localization; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; +using Volo.Abp.AspNetCore.Middleware; using Volo.Abp.DependencyInjection; namespace Microsoft.AspNetCore.RequestLocalization; -public class AbpRequestLocalizationMiddleware : IMiddleware, ITransientDependency +public class AbpRequestLocalizationMiddleware : AbpMiddlewareBase, ITransientDependency { public const string HttpContextItemName = "__AbpSetCultureCookie"; @@ -23,7 +24,7 @@ public class AbpRequestLocalizationMiddleware : IMiddleware, ITransientDependenc _loggerFactory = loggerFactory; } - public async Task InvokeAsync(HttpContext context, RequestDelegate next) + public async override Task InvokeAsync(HttpContext context, RequestDelegate next) { var middleware = new RequestLocalizationMiddleware( next, diff --git a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Auditing/AbpAuditingMiddleware.cs b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Auditing/AbpAuditingMiddleware.cs index 0987a8fd8f..86cc997ae7 100644 --- a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Auditing/AbpAuditingMiddleware.cs +++ b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Auditing/AbpAuditingMiddleware.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Options; +using Volo.Abp.AspNetCore.Middleware; using Volo.Abp.Auditing; using Volo.Abp.DependencyInjection; using Volo.Abp.Uow; @@ -11,7 +12,7 @@ using Volo.Abp.Users; namespace Volo.Abp.AspNetCore.Auditing; -public class AbpAuditingMiddleware : IMiddleware, ITransientDependency +public class AbpAuditingMiddleware : AbpMiddlewareBase, ITransientDependency { private readonly IAuditingManager _auditingManager; protected AbpAuditingOptions AuditingOptions { get; } @@ -34,9 +35,9 @@ public class AbpAuditingMiddleware : IMiddleware, ITransientDependency AspNetCoreAuditingOptions = aspNetCoreAuditingOptions.Value; } - public async Task InvokeAsync(HttpContext context, RequestDelegate next) + public async override Task InvokeAsync(HttpContext context, RequestDelegate next) { - if (!AuditingOptions.IsEnabled || IsIgnoredUrl(context)) + if (await ShouldSkipAsync(context, next) || !AuditingOptions.IsEnabled || IsIgnoredUrl(context)) { await next(context); return; diff --git a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/ExceptionHandling/AbpExceptionHandlingMiddleware.cs b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/ExceptionHandling/AbpExceptionHandlingMiddleware.cs index b9d3b83068..7d5ef610e5 100644 --- a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/ExceptionHandling/AbpExceptionHandlingMiddleware.cs +++ b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/ExceptionHandling/AbpExceptionHandlingMiddleware.cs @@ -5,6 +5,7 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using Microsoft.Net.Http.Headers; +using Volo.Abp.AspNetCore.Middleware; using Volo.Abp.AspNetCore.Mvc; using Volo.Abp.Authorization; using Volo.Abp.DependencyInjection; @@ -14,7 +15,7 @@ using Volo.Abp.Json; namespace Volo.Abp.AspNetCore.ExceptionHandling; -public class AbpExceptionHandlingMiddleware : IMiddleware, ITransientDependency +public class AbpExceptionHandlingMiddleware : AbpMiddlewareBase, ITransientDependency { private readonly ILogger _logger; @@ -27,7 +28,7 @@ public class AbpExceptionHandlingMiddleware : IMiddleware, ITransientDependency _clearCacheHeadersDelegate = ClearCacheHeaders; } - public async Task InvokeAsync(HttpContext context, RequestDelegate next) + public async override Task InvokeAsync(HttpContext context, RequestDelegate next) { try { diff --git a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Filters/DisableAbpFilterAttribute.cs b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Filters/DisableAbpFilterAttribute.cs new file mode 100644 index 0000000000..035c7b288f --- /dev/null +++ b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Filters/DisableAbpFilterAttribute.cs @@ -0,0 +1,19 @@ +using System; + +namespace Volo.Abp.AspNetCore.Filters; + +[AttributeUsage(AttributeTargets.Class)] +public class DisableAbpFilterAttribute : Attribute +{ + public bool SkipInMiddleware { get; set; } + + public DisableAbpFilterAttribute() + { + SkipInMiddleware = true; + } + + public DisableAbpFilterAttribute(bool skipInMiddleware) + { + SkipInMiddleware = skipInMiddleware; + } +} diff --git a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Filters/IAbpFilter.cs b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Filters/IAbpFilter.cs new file mode 100644 index 0000000000..dbd55a8ed4 --- /dev/null +++ b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Filters/IAbpFilter.cs @@ -0,0 +1,6 @@ +namespace Volo.Abp.AspNetCore.Filters; + +public interface IAbpFilter +{ + +} diff --git a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Middleware/AbpMiddlewareBase.cs b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Middleware/AbpMiddlewareBase.cs new file mode 100644 index 0000000000..bbf79ba22e --- /dev/null +++ b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Middleware/AbpMiddlewareBase.cs @@ -0,0 +1,20 @@ +using System.Reflection; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc.Controllers; +using Volo.Abp.AspNetCore.Filters; + +namespace Volo.Abp.AspNetCore.Middleware; + +public abstract class AbpMiddlewareBase : IMiddleware +{ + protected Task ShouldSkipAsync(HttpContext context, RequestDelegate next) + { + var endpoint = context.GetEndpoint(); + var controllerActionDescriptor = endpoint?.Metadata.GetMetadata(); + var disableAbpFilterAttribute = controllerActionDescriptor?.ControllerTypeInfo.GetCustomAttribute(); + return Task.FromResult(disableAbpFilterAttribute != null && disableAbpFilterAttribute.SkipInMiddleware); + } + + public abstract Task InvokeAsync(HttpContext context, RequestDelegate next); +} diff --git a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Security/AbpSecurityHeadersMiddleware.cs b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Security/AbpSecurityHeadersMiddleware.cs index ae2e3b173b..4542dd32e1 100644 --- a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Security/AbpSecurityHeadersMiddleware.cs +++ b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Security/AbpSecurityHeadersMiddleware.cs @@ -5,11 +5,12 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Options; using Microsoft.Extensions.Primitives; +using Volo.Abp.AspNetCore.Middleware; using Volo.Abp.DependencyInjection; namespace Volo.Abp.AspNetCore.Security; -public class AbpSecurityHeadersMiddleware : IMiddleware, ITransientDependency +public class AbpSecurityHeadersMiddleware : AbpMiddlewareBase, ITransientDependency { public IOptions Options { get; set; } protected const string ScriptSrcKey = "script-src"; @@ -20,7 +21,7 @@ public class AbpSecurityHeadersMiddleware : IMiddleware, ITransientDependency Options = options; } - public async Task InvokeAsync(HttpContext context, RequestDelegate next) + public async override Task InvokeAsync(HttpContext context, RequestDelegate next) { /*X-Content-Type-Options header tells the browser to not try and “guess” what a mimetype of a resource might be, and to just take what mimetype the server has returned as fact.*/ AddHeader(context, "X-Content-Type-Options", "nosniff"); diff --git a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Security/Claims/AbpClaimsMapMiddleware.cs b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Security/Claims/AbpClaimsMapMiddleware.cs index ab23bdb944..bb2bedf901 100644 --- a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Security/Claims/AbpClaimsMapMiddleware.cs +++ b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Security/Claims/AbpClaimsMapMiddleware.cs @@ -4,14 +4,15 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; +using Volo.Abp.AspNetCore.Middleware; using Volo.Abp.DependencyInjection; using Volo.Abp.Security.Claims; namespace Volo.Abp.AspNetCore.Security.Claims; -public class AbpClaimsMapMiddleware : IMiddleware, ITransientDependency +public class AbpClaimsMapMiddleware : AbpMiddlewareBase, ITransientDependency { - public async Task InvokeAsync(HttpContext context, RequestDelegate next) + public async override Task InvokeAsync(HttpContext context, RequestDelegate next) { var currentPrincipalAccessor = context.RequestServices .GetRequiredService(); diff --git a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Security/Claims/AbpDynamicClaimsMiddleware.cs b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Security/Claims/AbpDynamicClaimsMiddleware.cs index 777ca7744a..9c62fa8b25 100644 --- a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Security/Claims/AbpDynamicClaimsMiddleware.cs +++ b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Security/Claims/AbpDynamicClaimsMiddleware.cs @@ -2,14 +2,15 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; +using Volo.Abp.AspNetCore.Middleware; using Volo.Abp.DependencyInjection; using Volo.Abp.Security.Claims; namespace Volo.Abp.AspNetCore.Security.Claims; -public class AbpDynamicClaimsMiddleware : IMiddleware, ITransientDependency +public class AbpDynamicClaimsMiddleware : AbpMiddlewareBase, ITransientDependency { - public async Task InvokeAsync(HttpContext context, RequestDelegate next) + public async override Task InvokeAsync(HttpContext context, RequestDelegate next) { if (context.User.Identity?.IsAuthenticated == true) { diff --git a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Tracing/AbpCorrelationIdMiddleware.cs b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Tracing/AbpCorrelationIdMiddleware.cs index a5f954c260..38356f8151 100644 --- a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Tracing/AbpCorrelationIdMiddleware.cs +++ b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Tracing/AbpCorrelationIdMiddleware.cs @@ -3,12 +3,13 @@ using System.Collections.Generic; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Options; using System.Threading.Tasks; +using Volo.Abp.AspNetCore.Middleware; using Volo.Abp.DependencyInjection; using Volo.Abp.Tracing; namespace Volo.Abp.AspNetCore.Tracing; -public class AbpCorrelationIdMiddleware : IMiddleware, ITransientDependency +public class AbpCorrelationIdMiddleware : AbpMiddlewareBase, ITransientDependency { private readonly AbpCorrelationIdOptions _options; private readonly ICorrelationIdProvider _correlationIdProvider; @@ -20,7 +21,7 @@ public class AbpCorrelationIdMiddleware : IMiddleware, ITransientDependency _correlationIdProvider = correlationIdProvider; } - public async Task InvokeAsync(HttpContext context, RequestDelegate next) + public async override Task InvokeAsync(HttpContext context, RequestDelegate next) { var correlationId = GetCorrelationIdFromRequest(context); using (_correlationIdProvider.Change(correlationId)) diff --git a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Uow/AbpUnitOfWorkMiddleware.cs b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Uow/AbpUnitOfWorkMiddleware.cs index 186501b4ac..013b3dad98 100644 --- a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Uow/AbpUnitOfWorkMiddleware.cs +++ b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Uow/AbpUnitOfWorkMiddleware.cs @@ -3,12 +3,13 @@ using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Options; +using Volo.Abp.AspNetCore.Middleware; using Volo.Abp.DependencyInjection; using Volo.Abp.Uow; namespace Volo.Abp.AspNetCore.Uow; -public class AbpUnitOfWorkMiddleware : IMiddleware, ITransientDependency +public class AbpUnitOfWorkMiddleware : AbpMiddlewareBase, ITransientDependency { private readonly IUnitOfWorkManager _unitOfWorkManager; private readonly AbpAspNetCoreUnitOfWorkOptions _options; @@ -21,9 +22,9 @@ public class AbpUnitOfWorkMiddleware : IMiddleware, ITransientDependency _options = options.Value; } - public async Task InvokeAsync(HttpContext context, RequestDelegate next) + public async override Task InvokeAsync(HttpContext context, RequestDelegate next) { - if (IsIgnoredUrl(context)) + if (await ShouldSkipAsync(context, next) || IsIgnoredUrl(context)) { await next(context); return; diff --git a/framework/src/Volo.Abp.Autofac/Autofac/Builder/AbpRegistrationBuilderExtensions.cs b/framework/src/Volo.Abp.Autofac/Autofac/Builder/AbpRegistrationBuilderExtensions.cs index 043f824b1a..9a2b1fa43a 100644 --- a/framework/src/Volo.Abp.Autofac/Autofac/Builder/AbpRegistrationBuilderExtensions.cs +++ b/framework/src/Volo.Abp.Autofac/Autofac/Builder/AbpRegistrationBuilderExtensions.cs @@ -7,6 +7,7 @@ using Microsoft.Extensions.DependencyInjection; using Volo.Abp.Autofac; using Volo.Abp.Castle.DynamicProxy; using Volo.Abp.DependencyInjection; +using Volo.Abp.DynamicProxy; using Volo.Abp.Modularity; namespace Autofac.Builder; @@ -73,7 +74,8 @@ public static class AbpRegistrationBuilderExtensions registrationAction.Invoke(serviceRegistredArgs); } - if (serviceRegistredArgs.Interceptors.Any()) + if (serviceRegistredArgs.Interceptors.Any() && + !serviceRegistredArgs.ImplementationType.IsDefined(typeof(DisableInterceptorAttribute), true)) { registrationBuilder = registrationBuilder.AddInterceptors( registrationActionList, diff --git a/framework/src/Volo.Abp.Core/Volo/Abp/DynamicProxy/DisableInterceptorAttribute.cs b/framework/src/Volo.Abp.Core/Volo/Abp/DynamicProxy/DisableInterceptorAttribute.cs new file mode 100644 index 0000000000..7d3bf5bcc5 --- /dev/null +++ b/framework/src/Volo.Abp.Core/Volo/Abp/DynamicProxy/DisableInterceptorAttribute.cs @@ -0,0 +1,9 @@ +using System; + +namespace Volo.Abp.DynamicProxy; + +[AttributeUsage(AttributeTargets.Class)] +public class DisableInterceptorAttribute : Attribute +{ + +} diff --git a/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/FakeAuthenticationMiddleware.cs b/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/FakeAuthenticationMiddleware.cs index 41a9b56fe5..a3cd4a57e6 100644 --- a/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/FakeAuthenticationMiddleware.cs +++ b/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/FakeAuthenticationMiddleware.cs @@ -3,11 +3,12 @@ using System.Linq; using System.Security.Claims; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; +using Volo.Abp.AspNetCore.Middleware; using Volo.Abp.DependencyInjection; namespace Volo.Abp.AspNetCore.Mvc; -public class FakeAuthenticationMiddleware : IMiddleware, ITransientDependency +public class FakeAuthenticationMiddleware : AbpMiddlewareBase, ITransientDependency { private readonly FakeUserClaims _fakeUserClaims; @@ -16,7 +17,7 @@ public class FakeAuthenticationMiddleware : IMiddleware, ITransientDependency _fakeUserClaims = fakeUserClaims; } - public async Task InvokeAsync(HttpContext context, RequestDelegate next) + public async override Task InvokeAsync(HttpContext context, RequestDelegate next) { if (_fakeUserClaims.Claims.Any()) { From cb7e441a71e54a6bc03b65955a8cb266ca096dba Mon Sep 17 00:00:00 2001 From: maliming Date: Sat, 30 Dec 2023 17:57:32 +0800 Subject: [PATCH 2/4] Rename `DisableInterceptorAttribute` to `DisableAbpInterceptorAttribute`. --- .../Autofac/Builder/AbpRegistrationBuilderExtensions.cs | 2 +- ...nterceptorAttribute.cs => DisableAbpInterceptorAttribute.cs} | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) rename framework/src/Volo.Abp.Core/Volo/Abp/DynamicProxy/{DisableInterceptorAttribute.cs => DisableAbpInterceptorAttribute.cs} (62%) diff --git a/framework/src/Volo.Abp.Autofac/Autofac/Builder/AbpRegistrationBuilderExtensions.cs b/framework/src/Volo.Abp.Autofac/Autofac/Builder/AbpRegistrationBuilderExtensions.cs index 9a2b1fa43a..de5effd86c 100644 --- a/framework/src/Volo.Abp.Autofac/Autofac/Builder/AbpRegistrationBuilderExtensions.cs +++ b/framework/src/Volo.Abp.Autofac/Autofac/Builder/AbpRegistrationBuilderExtensions.cs @@ -75,7 +75,7 @@ public static class AbpRegistrationBuilderExtensions } if (serviceRegistredArgs.Interceptors.Any() && - !serviceRegistredArgs.ImplementationType.IsDefined(typeof(DisableInterceptorAttribute), true)) + !serviceRegistredArgs.ImplementationType.IsDefined(typeof(DisableAbpInterceptorAttribute), true)) { registrationBuilder = registrationBuilder.AddInterceptors( registrationActionList, diff --git a/framework/src/Volo.Abp.Core/Volo/Abp/DynamicProxy/DisableInterceptorAttribute.cs b/framework/src/Volo.Abp.Core/Volo/Abp/DynamicProxy/DisableAbpInterceptorAttribute.cs similarity index 62% rename from framework/src/Volo.Abp.Core/Volo/Abp/DynamicProxy/DisableInterceptorAttribute.cs rename to framework/src/Volo.Abp.Core/Volo/Abp/DynamicProxy/DisableAbpInterceptorAttribute.cs index 7d3bf5bcc5..be7b773c87 100644 --- a/framework/src/Volo.Abp.Core/Volo/Abp/DynamicProxy/DisableInterceptorAttribute.cs +++ b/framework/src/Volo.Abp.Core/Volo/Abp/DynamicProxy/DisableAbpInterceptorAttribute.cs @@ -3,7 +3,7 @@ using System; namespace Volo.Abp.DynamicProxy; [AttributeUsage(AttributeTargets.Class)] -public class DisableInterceptorAttribute : Attribute +public class DisableAbpInterceptorAttribute : Attribute { } From b10761d99708e54246daba6c680c3ae044496dab Mon Sep 17 00:00:00 2001 From: maliming Date: Sat, 30 Dec 2023 20:23:29 +0800 Subject: [PATCH 3/4] Add `DisableAbpFeaturesAttribute`. --- .../Mvc/AbpMvcActionDescriptorProvider.cs | 7 +++--- .../Filters/DisableAbpFilterAttribute.cs | 19 -------------- .../Middleware/AbpMiddlewareBase.cs | 5 ++-- .../AbpRegistrationBuilderExtensions.cs | 19 ++++++++------ .../Volo/Abp/DisableAbpFeaturesAttribute.cs | 25 +++++++++++++++++++ .../DisableAbpInterceptorAttribute.cs | 9 ------- 6 files changed, 43 insertions(+), 41 deletions(-) delete mode 100644 framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Filters/DisableAbpFilterAttribute.cs create mode 100644 framework/src/Volo.Abp.Core/Volo/Abp/DisableAbpFeaturesAttribute.cs delete mode 100644 framework/src/Volo.Abp.Core/Volo/Abp/DynamicProxy/DisableAbpInterceptorAttribute.cs diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpMvcActionDescriptorProvider.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpMvcActionDescriptorProvider.cs index 9131cd44d6..1ae2180e47 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpMvcActionDescriptorProvider.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpMvcActionDescriptorProvider.cs @@ -20,10 +20,11 @@ public class AbpMvcActionDescriptorProvider : IActionDescriptorProvider { foreach (var action in context.Results.Where(x => x is ControllerActionDescriptor).Cast()) { - var disableFilterAttribute = action.ControllerTypeInfo.GetCustomAttribute(true); - if (disableFilterAttribute != null) + var disableAbpFeaturesAttribute = action.ControllerTypeInfo.GetCustomAttribute(true); + if (disableAbpFeaturesAttribute != null && disableAbpFeaturesAttribute.DisableMvcFilters) { - action.FilterDescriptors.RemoveAll(x => x.Filter is ServiceFilterAttribute serviceFilterAttribute && typeof(IAbpFilter).IsAssignableFrom(serviceFilterAttribute.ServiceType)); + action.FilterDescriptors.RemoveAll(x => x.Filter is ServiceFilterAttribute serviceFilterAttribute && + typeof(IAbpFilter).IsAssignableFrom(serviceFilterAttribute.ServiceType)); } } } diff --git a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Filters/DisableAbpFilterAttribute.cs b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Filters/DisableAbpFilterAttribute.cs deleted file mode 100644 index 035c7b288f..0000000000 --- a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Filters/DisableAbpFilterAttribute.cs +++ /dev/null @@ -1,19 +0,0 @@ -using System; - -namespace Volo.Abp.AspNetCore.Filters; - -[AttributeUsage(AttributeTargets.Class)] -public class DisableAbpFilterAttribute : Attribute -{ - public bool SkipInMiddleware { get; set; } - - public DisableAbpFilterAttribute() - { - SkipInMiddleware = true; - } - - public DisableAbpFilterAttribute(bool skipInMiddleware) - { - SkipInMiddleware = skipInMiddleware; - } -} diff --git a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Middleware/AbpMiddlewareBase.cs b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Middleware/AbpMiddlewareBase.cs index bbf79ba22e..f41578da33 100644 --- a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Middleware/AbpMiddlewareBase.cs +++ b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Middleware/AbpMiddlewareBase.cs @@ -2,7 +2,6 @@ using System.Reflection; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc.Controllers; -using Volo.Abp.AspNetCore.Filters; namespace Volo.Abp.AspNetCore.Middleware; @@ -12,8 +11,8 @@ public abstract class AbpMiddlewareBase : IMiddleware { var endpoint = context.GetEndpoint(); var controllerActionDescriptor = endpoint?.Metadata.GetMetadata(); - var disableAbpFilterAttribute = controllerActionDescriptor?.ControllerTypeInfo.GetCustomAttribute(); - return Task.FromResult(disableAbpFilterAttribute != null && disableAbpFilterAttribute.SkipInMiddleware); + var disableAbpFeaturesAttribute = controllerActionDescriptor?.ControllerTypeInfo.GetCustomAttribute(); + return Task.FromResult(disableAbpFeaturesAttribute != null && disableAbpFeaturesAttribute.DisableMiddlewares); } public abstract Task InvokeAsync(HttpContext context, RequestDelegate next); diff --git a/framework/src/Volo.Abp.Autofac/Autofac/Builder/AbpRegistrationBuilderExtensions.cs b/framework/src/Volo.Abp.Autofac/Autofac/Builder/AbpRegistrationBuilderExtensions.cs index de5effd86c..53216b03b9 100644 --- a/framework/src/Volo.Abp.Autofac/Autofac/Builder/AbpRegistrationBuilderExtensions.cs +++ b/framework/src/Volo.Abp.Autofac/Autofac/Builder/AbpRegistrationBuilderExtensions.cs @@ -1,9 +1,11 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Reflection; using Autofac.Core; using Autofac.Extras.DynamicProxy; using Microsoft.Extensions.DependencyInjection; +using Volo.Abp; using Volo.Abp.Autofac; using Volo.Abp.Castle.DynamicProxy; using Volo.Abp.DependencyInjection; @@ -74,14 +76,17 @@ public static class AbpRegistrationBuilderExtensions registrationAction.Invoke(serviceRegistredArgs); } - if (serviceRegistredArgs.Interceptors.Any() && - !serviceRegistredArgs.ImplementationType.IsDefined(typeof(DisableAbpInterceptorAttribute), true)) + if (serviceRegistredArgs.Interceptors.Any()) { - registrationBuilder = registrationBuilder.AddInterceptors( - registrationActionList, - serviceType, - serviceRegistredArgs.Interceptors - ); + var disableAbpFeaturesAttribute = serviceRegistredArgs.ImplementationType.GetCustomAttribute(true); + if (disableAbpFeaturesAttribute == null || !disableAbpFeaturesAttribute.DisableInterceptors) + { + registrationBuilder = registrationBuilder.AddInterceptors( + registrationActionList, + serviceType, + serviceRegistredArgs.Interceptors + ); + } } return registrationBuilder; diff --git a/framework/src/Volo.Abp.Core/Volo/Abp/DisableAbpFeaturesAttribute.cs b/framework/src/Volo.Abp.Core/Volo/Abp/DisableAbpFeaturesAttribute.cs new file mode 100644 index 0000000000..69f80446a6 --- /dev/null +++ b/framework/src/Volo.Abp.Core/Volo/Abp/DisableAbpFeaturesAttribute.cs @@ -0,0 +1,25 @@ +using System; + +namespace Volo.Abp; + +[AttributeUsage(AttributeTargets.Class)] +public class DisableAbpFeaturesAttribute : Attribute +{ + /// + /// The framework will not register any interceptors for the class. + /// This will cause the all features that depend on interceptors to not work. + /// + public bool DisableInterceptors { get; set; } = true; + + /// + /// The framework middleware will skip the class. + /// This will cause the all features that depend on middleware to not work. + /// + public bool DisableMiddlewares { get; set; } = true; + + /// + /// The framework will not remove all built-in filters for the class. + /// This will cause the all features that depend on filters to not work. + /// + public bool DisableMvcFilters { get; set; } = true; +} diff --git a/framework/src/Volo.Abp.Core/Volo/Abp/DynamicProxy/DisableAbpInterceptorAttribute.cs b/framework/src/Volo.Abp.Core/Volo/Abp/DynamicProxy/DisableAbpInterceptorAttribute.cs deleted file mode 100644 index be7b773c87..0000000000 --- a/framework/src/Volo.Abp.Core/Volo/Abp/DynamicProxy/DisableAbpInterceptorAttribute.cs +++ /dev/null @@ -1,9 +0,0 @@ -using System; - -namespace Volo.Abp.DynamicProxy; - -[AttributeUsage(AttributeTargets.Class)] -public class DisableAbpInterceptorAttribute : Attribute -{ - -} From 2823596497cecaa5c91ac2e568b5524607da7ff4 Mon Sep 17 00:00:00 2001 From: maliming Date: Sat, 30 Dec 2023 21:27:54 +0800 Subject: [PATCH 4/4] Add unit tests. --- .../Middleware/AbpMiddlewareBase.cs | 2 +- .../Volo/Abp/DisableAbpFeaturesAttribute.cs | 2 +- .../MvcDisableAbpFeaturesAttribute_Tests.cs | 74 +++++++++++++++++++ .../DynamicProxy/AbpInterceptionTestBase.cs | 31 ++++++++ .../AlwaysExceptionAsyncInterceptor.cs | 11 +++ .../DisableInterceptionTargetClass.cs | 41 ++++++++++ 6 files changed, 159 insertions(+), 2 deletions(-) create mode 100644 framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Filters/MvcDisableAbpFeaturesAttribute_Tests.cs create mode 100644 framework/test/Volo.Abp.Core.Tests/Volo/Abp/DynamicProxy/AlwaysExceptionAsyncInterceptor.cs create mode 100644 framework/test/Volo.Abp.Core.Tests/Volo/Abp/DynamicProxy/DisableInterceptionTargetClass.cs diff --git a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Middleware/AbpMiddlewareBase.cs b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Middleware/AbpMiddlewareBase.cs index f41578da33..52c9990a4f 100644 --- a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Middleware/AbpMiddlewareBase.cs +++ b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Middleware/AbpMiddlewareBase.cs @@ -12,7 +12,7 @@ public abstract class AbpMiddlewareBase : IMiddleware var endpoint = context.GetEndpoint(); var controllerActionDescriptor = endpoint?.Metadata.GetMetadata(); var disableAbpFeaturesAttribute = controllerActionDescriptor?.ControllerTypeInfo.GetCustomAttribute(); - return Task.FromResult(disableAbpFeaturesAttribute != null && disableAbpFeaturesAttribute.DisableMiddlewares); + return Task.FromResult(disableAbpFeaturesAttribute != null && disableAbpFeaturesAttribute.DisableMiddleware); } public abstract Task InvokeAsync(HttpContext context, RequestDelegate next); diff --git a/framework/src/Volo.Abp.Core/Volo/Abp/DisableAbpFeaturesAttribute.cs b/framework/src/Volo.Abp.Core/Volo/Abp/DisableAbpFeaturesAttribute.cs index 69f80446a6..28c92b7463 100644 --- a/framework/src/Volo.Abp.Core/Volo/Abp/DisableAbpFeaturesAttribute.cs +++ b/framework/src/Volo.Abp.Core/Volo/Abp/DisableAbpFeaturesAttribute.cs @@ -15,7 +15,7 @@ public class DisableAbpFeaturesAttribute : Attribute /// The framework middleware will skip the class. /// This will cause the all features that depend on middleware to not work. /// - public bool DisableMiddlewares { get; set; } = true; + public bool DisableMiddleware { get; set; } = true; /// /// The framework will not remove all built-in filters for the class. diff --git a/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Filters/MvcDisableAbpFeaturesAttribute_Tests.cs b/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Filters/MvcDisableAbpFeaturesAttribute_Tests.cs new file mode 100644 index 0000000000..7ff181c645 --- /dev/null +++ b/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Filters/MvcDisableAbpFeaturesAttribute_Tests.cs @@ -0,0 +1,74 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.Controllers; +using Microsoft.AspNetCore.Mvc.Filters; +using Shouldly; +using Volo.Abp.AspNetCore.Mvc.Auditing; +using Volo.Abp.AspNetCore.Mvc.ExceptionHandling; +using Volo.Abp.AspNetCore.Mvc.Features; +using Volo.Abp.AspNetCore.Mvc.GlobalFeatures; +using Volo.Abp.AspNetCore.Mvc.Response; +using Volo.Abp.AspNetCore.Mvc.Uow; +using Volo.Abp.AspNetCore.Mvc.Validation; +using Xunit; + +namespace Volo.Abp.AspNetCore.Mvc.Filters; + +[Route("api/enabled-features-test")] +public class EnabledAbpFeaturesController : AbpController, IRemoteService +{ + [HttpGet] + public Task> GetAsync() + { + var filters = HttpContext.GetEndpoint().Metadata.GetMetadata() + .FilterDescriptors.Where(x => x.Filter is ServiceFilterAttribute) + .Select(x => x.Filter.As().ServiceType.FullName).ToList(); + + return Task.FromResult(filters); + } +} + +[Route("api/disabled-features-test")] +[DisableAbpFeatures] +public class DisabledAbpFeaturesController : AbpController, IRemoteService +{ + [HttpGet] + public Task> GetAsync() + { + var filters = HttpContext.GetEndpoint().Metadata.GetMetadata() + .FilterDescriptors.Where(x => x.Filter is ServiceFilterAttribute) + .Select(x => x.Filter.As().ServiceType.FullName).ToList(); + + return Task.FromResult(filters); + } +} + +public class MvcDisableAbpFeaturesAttribute_Tests : AspNetCoreMvcTestBase +{ + [Fact] + public async Task Should_Disable_MVC_Filters() + { + var filters = await GetResponseAsObjectAsync>("/api/enabled-features-test"); + filters.ShouldContain(typeof(GlobalFeatureActionFilter).FullName); + filters.ShouldContain(typeof(AbpAuditActionFilter).FullName); + filters.ShouldContain(typeof(AbpNoContentActionFilter).FullName); + filters.ShouldContain(typeof(AbpFeatureActionFilter).FullName); + filters.ShouldContain(typeof(AbpValidationActionFilter).FullName); + filters.ShouldContain(typeof(AbpUowActionFilter).FullName); + filters.ShouldContain(typeof(AbpExceptionFilter).FullName); + + filters = await GetResponseAsObjectAsync>("/api/disabled-features-test"); + filters.ShouldNotContain(typeof(GlobalFeatureActionFilter).FullName); + filters.ShouldNotContain(typeof(AbpAuditActionFilter).FullName); + filters.ShouldNotContain(typeof(AbpNoContentActionFilter).FullName); + filters.ShouldNotContain(typeof(AbpFeatureActionFilter).FullName); + filters.ShouldNotContain(typeof(AbpValidationActionFilter).FullName); + filters.ShouldNotContain(typeof(AbpUowActionFilter).FullName); + filters.ShouldNotContain(typeof(AbpExceptionFilter).FullName); + } + +} diff --git a/framework/test/Volo.Abp.Core.Tests/Volo/Abp/DynamicProxy/AbpInterceptionTestBase.cs b/framework/test/Volo.Abp.Core.Tests/Volo/Abp/DynamicProxy/AbpInterceptionTestBase.cs index e244237598..4cce6a0c78 100644 --- a/framework/test/Volo.Abp.Core.Tests/Volo/Abp/DynamicProxy/AbpInterceptionTestBase.cs +++ b/framework/test/Volo.Abp.Core.Tests/Volo/Abp/DynamicProxy/AbpInterceptionTestBase.cs @@ -15,22 +15,34 @@ public abstract class AbpInterceptionTestBase : AbpAsyncIntegrat services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); + services.OnRegistered(registration => { if (typeof(SimpleInterceptionTargetClass) == registration.ImplementationType) { registration.Interceptors.Add(); registration.Interceptors.Add(); + registration.Interceptors.Add(); } if (typeof(CachedTestObject) == registration.ImplementationType) { registration.Interceptors.Add(); } + + if (typeof(DisableInterceptionTargetClass) == registration.ImplementationType) + { + registration.Interceptors.Add(); + registration.Interceptors.Add(); + registration.Interceptors.Add(); + registration.Interceptors.Add(); + } }); return Task.CompletedTask; @@ -96,4 +108,23 @@ public abstract class AbpInterceptionTestBase : AbpAsyncIntegrat (await target.GetValueAsync(43)).ShouldBe(42); //First run, cached previous value (await target.GetValueAsync(44)).ShouldBe(42); //First run, cached previous value } + + [Fact] + public async Task Should_Disable_Interceptors() + { + //Arrange + + var target = ServiceProvider.GetService(); + + //Act + + await target.DoItAsync(); + + //Assert + + target.Logs.Count.ShouldBe(3); + target.Logs[0].ShouldBe("EnterDoItAsync"); + target.Logs[1].ShouldBe("MiddleDoItAsync"); + target.Logs[2].ShouldBe("ExitDoItAsync"); + } } diff --git a/framework/test/Volo.Abp.Core.Tests/Volo/Abp/DynamicProxy/AlwaysExceptionAsyncInterceptor.cs b/framework/test/Volo.Abp.Core.Tests/Volo/Abp/DynamicProxy/AlwaysExceptionAsyncInterceptor.cs new file mode 100644 index 0000000000..2b9845a6ea --- /dev/null +++ b/framework/test/Volo.Abp.Core.Tests/Volo/Abp/DynamicProxy/AlwaysExceptionAsyncInterceptor.cs @@ -0,0 +1,11 @@ +using System.Threading.Tasks; + +namespace Volo.Abp.DynamicProxy; + +public class AlwaysExceptionAsyncInterceptor : AbpInterceptor +{ + public override Task InterceptAsync(IAbpMethodInvocation invocation) + { + throw new AbpException("This interceptor should not be executed!"); + } +} diff --git a/framework/test/Volo.Abp.Core.Tests/Volo/Abp/DynamicProxy/DisableInterceptionTargetClass.cs b/framework/test/Volo.Abp.Core.Tests/Volo/Abp/DynamicProxy/DisableInterceptionTargetClass.cs new file mode 100644 index 0000000000..caf17b5b9d --- /dev/null +++ b/framework/test/Volo.Abp.Core.Tests/Volo/Abp/DynamicProxy/DisableInterceptionTargetClass.cs @@ -0,0 +1,41 @@ +using System.Collections.Generic; +using System.Threading.Tasks; +using Volo.Abp.TestBase.Logging; + +namespace Volo.Abp.DynamicProxy; + +[DisableAbpFeatures] +public class DisableInterceptionTargetClass : ICanLogOnObject +{ + public List Logs { get; } = new List(); + + public virtual void DoIt() + { + Logs.Add("ExecutingDoIt"); + } + + public virtual int GetValue() + { + Logs.Add("ExecutingGetValue"); + return 42; + } + + public virtual async Task GetValueAsync() + { + Logs.Add("EnterGetValueAsync"); + await Task.Delay(5); + Logs.Add("MiddleGetValueAsync"); + await Task.Delay(5); + Logs.Add("ExitGetValueAsync"); + return 42; + } + + public virtual async Task DoItAsync() + { + Logs.Add("EnterDoItAsync"); + await Task.Delay(5); + Logs.Add("MiddleDoItAsync"); + await Task.Delay(5); + Logs.Add("ExitDoItAsync"); + } +}