Browse Source

feat(auditing): Record the request header in the audit log

- Increase `AbpAspNetCoreAuditingModule` module
pull/1279/head
colin 7 months ago
parent
commit
0073773038
  1. 3
      aspnet-core/framework/auditing/LINGYUN.Abp.AspNetCore.Auditing/FodyWeavers.xml
  2. 20
      aspnet-core/framework/auditing/LINGYUN.Abp.AspNetCore.Auditing/LINGYUN.Abp.AspNetCore.Auditing.csproj
  3. 19
      aspnet-core/framework/auditing/LINGYUN.Abp.AspNetCore.Auditing/LINGYUN/Abp/AspNetCore/Auditing/AbpAspNetCoreAuditingHeaderOptions.cs
  4. 17
      aspnet-core/framework/auditing/LINGYUN.Abp.AspNetCore.Auditing/LINGYUN/Abp/AspNetCore/Auditing/AbpAspNetCoreAuditingModule.cs
  5. 51
      aspnet-core/framework/auditing/LINGYUN.Abp.AspNetCore.Auditing/LINGYUN/Abp/AspNetCore/Auditing/AspNetCoreRecordHeaderAuditLogContributor.cs
  6. 19
      aspnet-core/framework/auditing/LINGYUN.Abp.AspNetCore.Auditing/README.md

3
aspnet-core/framework/auditing/LINGYUN.Abp.AspNetCore.Auditing/FodyWeavers.xml

@ -0,0 +1,3 @@
<Weavers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="FodyWeavers.xsd">
<ConfigureAwait ContinueOnCapturedContext="false" />
</Weavers>

20
aspnet-core/framework/auditing/LINGYUN.Abp.AspNetCore.Auditing/LINGYUN.Abp.AspNetCore.Auditing.csproj

@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="..\..\..\..\configureawait.props" />
<Import Project="..\..\..\..\common.props" />
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<AssemblyName>LINGYUN.Abp.AspNetCore.Auditing</AssemblyName>
<PackageId>LINGYUN.Abp.AspNetCore.Auditing</PackageId>
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
<RootNamespace />
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Volo.Abp.AspNetCore" />
</ItemGroup>
</Project>

19
aspnet-core/framework/auditing/LINGYUN.Abp.AspNetCore.Auditing/LINGYUN/Abp/AspNetCore/Auditing/AbpAspNetCoreAuditingHeaderOptions.cs

@ -0,0 +1,19 @@
using System.Collections.Generic;
namespace LINGYUN.Abp.AspNetCore.Auditing;
public class AbpAspNetCoreAuditingHeaderOptions
{
/// <summary>
/// 是否在审计日志中记录Http请求头,默认: true
/// </summary>
public bool IsEnabled { get; set; }
/// <summary>
/// 要记录的Http请求头
/// </summary>
public IList<string> HttpHeaders { get; }
public AbpAspNetCoreAuditingHeaderOptions()
{
IsEnabled = true;
HttpHeaders = new List<string>();
}
}

17
aspnet-core/framework/auditing/LINGYUN.Abp.AspNetCore.Auditing/LINGYUN/Abp/AspNetCore/Auditing/AbpAspNetCoreAuditingModule.cs

@ -0,0 +1,17 @@
using Volo.Abp.AspNetCore;
using Volo.Abp.Auditing;
using Volo.Abp.Modularity;
namespace LINGYUN.Abp.AspNetCore.Auditing;
[DependsOn(typeof(AbpAspNetCoreModule))]
public class AbpAspNetCoreAuditingModule : AbpModule
{
public override void ConfigureServices(ServiceConfigurationContext context)
{
Configure<AbpAuditingOptions>(options =>
{
options.Contributors.Add(new AspNetCoreRecordHeaderAuditLogContributor());
});
}
}

51
aspnet-core/framework/auditing/LINGYUN.Abp.AspNetCore.Auditing/LINGYUN/Abp/AspNetCore/Auditing/AspNetCoreRecordHeaderAuditLogContributor.cs

@ -0,0 +1,51 @@
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using System.Collections.Generic;
using System.Collections.Immutable;
using Volo.Abp.Auditing;
using Volo.Abp.Data;
using Volo.Abp.DependencyInjection;
namespace LINGYUN.Abp.AspNetCore.Auditing;
public class AspNetCoreRecordHeaderAuditLogContributor : AuditLogContributor, ITransientDependency
{
private const string HttpHeaderRecordKey = "HttpHeaders";
public AspNetCoreRecordHeaderAuditLogContributor()
{
}
public override void PreContribute(AuditLogContributionContext context)
{
var options = context.ServiceProvider.GetRequiredService<IOptions<AbpAspNetCoreAuditingHeaderOptions>>();
if (!options.Value.IsEnabled)
{
return;
}
var httpContext = context.ServiceProvider.GetRequiredService<IHttpContextAccessor>().HttpContext;
if (httpContext == null)
{
return;
}
if (context.AuditInfo.HasProperty(HttpHeaderRecordKey))
{
return;
}
var headerRcords = new Dictionary<string, string>();
var httpHeaders = httpContext.Request.Headers.ToImmutableDictionary();
foreach (var headerKey in options.Value.HttpHeaders)
{
if (httpHeaders.TryGetValue(headerKey, out var headers))
{
headerRcords[headerKey] = headers.JoinAsString(";");
}
}
context.AuditInfo.SetProperty(HttpHeaderRecordKey, headerRcords);
}
}

19
aspnet-core/framework/auditing/LINGYUN.Abp.AspNetCore.Auditing/README.md

@ -0,0 +1,19 @@
# LINGYUN.Abp.AspNetCore.Auditing
审计日期扩展模块, 用于在审计日志中加入特定的Http请求头记录
## 模块引用
```csharp
[DependsOn(typeof(AbpAspNetCoreAuditingModule))]
public class YouProjectModule : AbpModule
{
// other
}
```
## 配置项
* AbpAspNetCoreAuditingHeaderOptions.IsEnabled 是否在审计日志中记录Http请求头,默认: true
* AbpAspNetCoreAuditingHeaderOptions.HttpHeaders 需要在审计日志中记录的Http请求头列表
Loading…
Cancel
Save