From c67db4ff15db318868dcdc84f4192da4c4e9d0eb Mon Sep 17 00:00:00 2001 From: maliming Date: Fri, 6 Mar 2026 14:19:29 +0800 Subject: [PATCH] refactor: remove IClientIpAddressProvider and replace with IWebClientInfoProvider in operation rate limiting --- .../AbpAspNetCoreAbstractionsModule.cs | 2 -- .../IClientIpAddressProvider.cs | 6 ---- .../NullClientIpAddressProvider.cs | 6 ---- .../HttpContextClientIpAddressProvider.cs | 36 ------------------- .../FixedWindowOperationRateLimitRule.cs | 12 +++---- .../OperationRateLimitChecker.cs | 10 +++--- .../OperationRateLimitRuleBuilder.cs | 2 +- .../AbpOperationRateLimitTestModule.cs | 8 ++--- 8 files changed, 16 insertions(+), 66 deletions(-) delete mode 100644 framework/src/Volo.Abp.AspNetCore.Abstractions/Volo/Abp/AspNetCore/ClientIpAddress/IClientIpAddressProvider.cs delete mode 100644 framework/src/Volo.Abp.AspNetCore.Abstractions/Volo/Abp/AspNetCore/ClientIpAddress/NullClientIpAddressProvider.cs delete mode 100644 framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/ClientIpAddress/HttpContextClientIpAddressProvider.cs diff --git a/framework/src/Volo.Abp.AspNetCore.Abstractions/Volo/Abp/AspNetCore/AbpAspNetCoreAbstractionsModule.cs b/framework/src/Volo.Abp.AspNetCore.Abstractions/Volo/Abp/AspNetCore/AbpAspNetCoreAbstractionsModule.cs index 7d72a0bfa8..603a578ef4 100644 --- a/framework/src/Volo.Abp.AspNetCore.Abstractions/Volo/Abp/AspNetCore/AbpAspNetCoreAbstractionsModule.cs +++ b/framework/src/Volo.Abp.AspNetCore.Abstractions/Volo/Abp/AspNetCore/AbpAspNetCoreAbstractionsModule.cs @@ -1,5 +1,4 @@ using Microsoft.Extensions.DependencyInjection; -using Volo.Abp.AspNetCore.ClientIpAddress; using Volo.Abp.AspNetCore.VirtualFileSystem; using Volo.Abp.AspNetCore.WebClientInfo; using Volo.Abp.Modularity; @@ -12,6 +11,5 @@ public class AbpAspNetCoreAbstractionsModule : AbpModule { context.Services.AddSingleton(); context.Services.AddSingleton(); - context.Services.AddSingleton(); } } diff --git a/framework/src/Volo.Abp.AspNetCore.Abstractions/Volo/Abp/AspNetCore/ClientIpAddress/IClientIpAddressProvider.cs b/framework/src/Volo.Abp.AspNetCore.Abstractions/Volo/Abp/AspNetCore/ClientIpAddress/IClientIpAddressProvider.cs deleted file mode 100644 index 6318ec0989..0000000000 --- a/framework/src/Volo.Abp.AspNetCore.Abstractions/Volo/Abp/AspNetCore/ClientIpAddress/IClientIpAddressProvider.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace Volo.Abp.AspNetCore.ClientIpAddress; - -public interface IClientIpAddressProvider -{ - string? ClientIpAddress { get; } -} diff --git a/framework/src/Volo.Abp.AspNetCore.Abstractions/Volo/Abp/AspNetCore/ClientIpAddress/NullClientIpAddressProvider.cs b/framework/src/Volo.Abp.AspNetCore.Abstractions/Volo/Abp/AspNetCore/ClientIpAddress/NullClientIpAddressProvider.cs deleted file mode 100644 index f1dbcc903e..0000000000 --- a/framework/src/Volo.Abp.AspNetCore.Abstractions/Volo/Abp/AspNetCore/ClientIpAddress/NullClientIpAddressProvider.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace Volo.Abp.AspNetCore.ClientIpAddress; - -public class NullClientIpAddressProvider : IClientIpAddressProvider -{ - public string? ClientIpAddress => null; -} diff --git a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/ClientIpAddress/HttpContextClientIpAddressProvider.cs b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/ClientIpAddress/HttpContextClientIpAddressProvider.cs deleted file mode 100644 index fa0a252e3c..0000000000 --- a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/ClientIpAddress/HttpContextClientIpAddressProvider.cs +++ /dev/null @@ -1,36 +0,0 @@ -using System; -using Microsoft.AspNetCore.Http; -using Microsoft.Extensions.Logging; -using Volo.Abp.DependencyInjection; - -namespace Volo.Abp.AspNetCore.ClientIpAddress; - -[Dependency(ReplaceServices = true)] -public class HttpContextClientIpAddressProvider : IClientIpAddressProvider, ITransientDependency -{ - protected ILogger Logger { get; } - protected IHttpContextAccessor HttpContextAccessor { get; } - - public HttpContextClientIpAddressProvider( - ILogger logger, - IHttpContextAccessor httpContextAccessor) - { - Logger = logger; - HttpContextAccessor = httpContextAccessor; - } - - public string? ClientIpAddress => GetClientIpAddress(); - - protected virtual string? GetClientIpAddress() - { - try - { - return HttpContextAccessor.HttpContext?.Connection?.RemoteIpAddress?.ToString(); - } - catch (Exception ex) - { - Logger.LogException(ex, LogLevel.Warning); - return null; - } - } -} diff --git a/framework/src/Volo.Abp.OperationRateLimit/Volo/Abp/OperationRateLimit/FixedWindowOperationRateLimitRule.cs b/framework/src/Volo.Abp.OperationRateLimit/Volo/Abp/OperationRateLimit/FixedWindowOperationRateLimitRule.cs index a13d00c087..3b46cf3c7b 100644 --- a/framework/src/Volo.Abp.OperationRateLimit/Volo/Abp/OperationRateLimit/FixedWindowOperationRateLimitRule.cs +++ b/framework/src/Volo.Abp.OperationRateLimit/Volo/Abp/OperationRateLimit/FixedWindowOperationRateLimitRule.cs @@ -1,5 +1,5 @@ using System.Threading.Tasks; -using Volo.Abp.AspNetCore.ClientIpAddress; +using Volo.Abp.AspNetCore.WebClientInfo; using Volo.Abp.MultiTenancy; using Volo.Abp.Users; @@ -15,7 +15,7 @@ public class FixedWindowOperationRateLimitRule : IOperationRateLimitRule protected IOperationRateLimitStore Store { get; } protected ICurrentUser CurrentUser { get; } protected ICurrentTenant CurrentTenant { get; } - protected IClientIpAddressProvider ClientIpAddressProvider { get; } + protected IWebClientInfoProvider WebClientInfoProvider { get; } public FixedWindowOperationRateLimitRule( string policyName, @@ -24,7 +24,7 @@ public class FixedWindowOperationRateLimitRule : IOperationRateLimitRule IOperationRateLimitStore store, ICurrentUser currentUser, ICurrentTenant currentTenant, - IClientIpAddressProvider clientIpAddressProvider) + IWebClientInfoProvider webClientInfoProvider) { PolicyName = policyName; RuleIndex = ruleIndex; @@ -32,7 +32,7 @@ public class FixedWindowOperationRateLimitRule : IOperationRateLimitRule Store = store; CurrentUser = currentUser; CurrentTenant = currentTenant; - ClientIpAddressProvider = clientIpAddressProvider; + WebClientInfoProvider = webClientInfoProvider; } public virtual async Task AcquireAsync( @@ -78,10 +78,10 @@ public class FixedWindowOperationRateLimitRule : IOperationRateLimitRule CurrentTenant.Id?.ToString() ?? HostTenantKey, OperationRateLimitPartitionType.ClientIp => - ClientIpAddressProvider.ClientIpAddress + WebClientInfoProvider.ClientIpAddress ?? throw new AbpException( $"Client IP address could not be determined. Policy '{PolicyName}' requires PartitionByClientIp. " + - "Ensure IClientIpAddressProvider is properly configured."), + "Ensure IWebClientInfoProvider is properly configured."), OperationRateLimitPartitionType.Email => context.Parameter diff --git a/framework/src/Volo.Abp.OperationRateLimit/Volo/Abp/OperationRateLimit/OperationRateLimitChecker.cs b/framework/src/Volo.Abp.OperationRateLimit/Volo/Abp/OperationRateLimit/OperationRateLimitChecker.cs index 98965c445f..9240d096b7 100644 --- a/framework/src/Volo.Abp.OperationRateLimit/Volo/Abp/OperationRateLimit/OperationRateLimitChecker.cs +++ b/framework/src/Volo.Abp.OperationRateLimit/Volo/Abp/OperationRateLimit/OperationRateLimitChecker.cs @@ -4,7 +4,7 @@ using System.Linq; using System.Threading.Tasks; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; -using Volo.Abp.AspNetCore.ClientIpAddress; +using Volo.Abp.AspNetCore.WebClientInfo; using Volo.Abp.DependencyInjection; using Volo.Abp.MultiTenancy; using Volo.Abp.Users; @@ -19,7 +19,7 @@ public class OperationRateLimitChecker : IOperationRateLimitChecker, ITransientD protected IOperationRateLimitStore Store { get; } protected ICurrentUser CurrentUser { get; } protected ICurrentTenant CurrentTenant { get; } - protected IClientIpAddressProvider ClientIpAddressProvider { get; } + protected IWebClientInfoProvider WebClientInfoProvider { get; } public OperationRateLimitChecker( IOptions options, @@ -28,7 +28,7 @@ public class OperationRateLimitChecker : IOperationRateLimitChecker, ITransientD IOperationRateLimitStore store, ICurrentUser currentUser, ICurrentTenant currentTenant, - IClientIpAddressProvider clientIpAddressProvider) + IWebClientInfoProvider webClientInfoProvider) { Options = options.Value; PolicyProvider = policyProvider; @@ -36,7 +36,7 @@ public class OperationRateLimitChecker : IOperationRateLimitChecker, ITransientD Store = store; CurrentUser = currentUser; CurrentTenant = currentTenant; - ClientIpAddressProvider = clientIpAddressProvider; + WebClientInfoProvider = webClientInfoProvider; } public virtual async Task CheckAsync(string policyName, OperationRateLimitContext? context = null) @@ -162,7 +162,7 @@ public class OperationRateLimitChecker : IOperationRateLimitChecker, ITransientD Store, CurrentUser, CurrentTenant, - ClientIpAddressProvider)); + WebClientInfoProvider)); } foreach (var customRuleType in policy.CustomRuleTypes) diff --git a/framework/src/Volo.Abp.OperationRateLimit/Volo/Abp/OperationRateLimit/OperationRateLimitRuleBuilder.cs b/framework/src/Volo.Abp.OperationRateLimit/Volo/Abp/OperationRateLimit/OperationRateLimitRuleBuilder.cs index 6cf8a89921..2908f9a538 100644 --- a/framework/src/Volo.Abp.OperationRateLimit/Volo/Abp/OperationRateLimit/OperationRateLimitRuleBuilder.cs +++ b/framework/src/Volo.Abp.OperationRateLimit/Volo/Abp/OperationRateLimit/OperationRateLimitRuleBuilder.cs @@ -63,7 +63,7 @@ public class OperationRateLimitRuleBuilder } /// - /// Auto resolve from IClientIpAddressProvider.ClientIpAddress. + /// Auto resolve from IWebClientInfoProvider.ClientIpAddress. /// public OperationRateLimitPolicyBuilder PartitionByClientIp() { diff --git a/framework/test/Volo.Abp.OperationRateLimit.Tests/Volo/Abp/OperationRateLimit/AbpOperationRateLimitTestModule.cs b/framework/test/Volo.Abp.OperationRateLimit.Tests/Volo/Abp/OperationRateLimit/AbpOperationRateLimitTestModule.cs index 13a9a3a4f5..45cf7320e1 100644 --- a/framework/test/Volo.Abp.OperationRateLimit.Tests/Volo/Abp/OperationRateLimit/AbpOperationRateLimitTestModule.cs +++ b/framework/test/Volo.Abp.OperationRateLimit.Tests/Volo/Abp/OperationRateLimit/AbpOperationRateLimitTestModule.cs @@ -1,7 +1,7 @@ using System; using Microsoft.Extensions.DependencyInjection; using NSubstitute; -using Volo.Abp.AspNetCore.ClientIpAddress; +using Volo.Abp.AspNetCore.WebClientInfo; using Volo.Abp.Autofac; using Volo.Abp.ExceptionHandling; using Volo.Abp.Modularity; @@ -18,9 +18,9 @@ public class AbpOperationRateLimitTestModule : AbpModule { public override void ConfigureServices(ServiceConfigurationContext context) { - var mockIpProvider = Substitute.For(); - mockIpProvider.ClientIpAddress.Returns("127.0.0.1"); - context.Services.AddSingleton(mockIpProvider); + var mockWebClientInfoProvider = Substitute.For(); + mockWebClientInfoProvider.ClientIpAddress.Returns("127.0.0.1"); + context.Services.AddSingleton(mockWebClientInfoProvider); Configure(options => {