Skip to content

Commit 8cb3125

Browse files
authored
Merge pull request #1279 from colinin/auditing-header
feat(auditing): Record the request header in the audit log
2 parents edb3cf0 + 0073773 commit 8cb3125

6 files changed

Lines changed: 129 additions & 0 deletions

File tree

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<Weavers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="FodyWeavers.xsd">
2+
<ConfigureAwait ContinueOnCapturedContext="false" />
3+
</Weavers>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<Import Project="..\..\..\..\configureawait.props" />
4+
<Import Project="..\..\..\..\common.props" />
5+
6+
<PropertyGroup>
7+
<TargetFramework>net9.0</TargetFramework>
8+
<AssemblyName>LINGYUN.Abp.AspNetCore.Auditing</AssemblyName>
9+
<PackageId>LINGYUN.Abp.AspNetCore.Auditing</PackageId>
10+
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
11+
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
12+
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
13+
<RootNamespace />
14+
</PropertyGroup>
15+
16+
<ItemGroup>
17+
<PackageReference Include="Volo.Abp.AspNetCore" />
18+
</ItemGroup>
19+
20+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System.Collections.Generic;
2+
3+
namespace LINGYUN.Abp.AspNetCore.Auditing;
4+
public class AbpAspNetCoreAuditingHeaderOptions
5+
{
6+
/// <summary>
7+
/// 是否在审计日志中记录Http请求头,默认: true
8+
/// </summary>
9+
public bool IsEnabled { get; set; }
10+
/// <summary>
11+
/// 要记录的Http请求头
12+
/// </summary>
13+
public IList<string> HttpHeaders { get; }
14+
public AbpAspNetCoreAuditingHeaderOptions()
15+
{
16+
IsEnabled = true;
17+
HttpHeaders = new List<string>();
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using Volo.Abp.AspNetCore;
2+
using Volo.Abp.Auditing;
3+
using Volo.Abp.Modularity;
4+
5+
namespace LINGYUN.Abp.AspNetCore.Auditing;
6+
7+
[DependsOn(typeof(AbpAspNetCoreModule))]
8+
public class AbpAspNetCoreAuditingModule : AbpModule
9+
{
10+
public override void ConfigureServices(ServiceConfigurationContext context)
11+
{
12+
Configure<AbpAuditingOptions>(options =>
13+
{
14+
options.Contributors.Add(new AspNetCoreRecordHeaderAuditLogContributor());
15+
});
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using Microsoft.AspNetCore.Http;
2+
using Microsoft.Extensions.DependencyInjection;
3+
using Microsoft.Extensions.Options;
4+
using System.Collections.Generic;
5+
using System.Collections.Immutable;
6+
using Volo.Abp.Auditing;
7+
using Volo.Abp.Data;
8+
using Volo.Abp.DependencyInjection;
9+
10+
namespace LINGYUN.Abp.AspNetCore.Auditing;
11+
public class AspNetCoreRecordHeaderAuditLogContributor : AuditLogContributor, ITransientDependency
12+
{
13+
private const string HttpHeaderRecordKey = "HttpHeaders";
14+
15+
public AspNetCoreRecordHeaderAuditLogContributor()
16+
{
17+
}
18+
19+
public override void PreContribute(AuditLogContributionContext context)
20+
{
21+
var options = context.ServiceProvider.GetRequiredService<IOptions<AbpAspNetCoreAuditingHeaderOptions>>();
22+
if (!options.Value.IsEnabled)
23+
{
24+
return;
25+
}
26+
27+
var httpContext = context.ServiceProvider.GetRequiredService<IHttpContextAccessor>().HttpContext;
28+
if (httpContext == null)
29+
{
30+
return;
31+
}
32+
33+
if (context.AuditInfo.HasProperty(HttpHeaderRecordKey))
34+
{
35+
return;
36+
}
37+
38+
var headerRcords = new Dictionary<string, string>();
39+
var httpHeaders = httpContext.Request.Headers.ToImmutableDictionary();
40+
41+
foreach (var headerKey in options.Value.HttpHeaders)
42+
{
43+
if (httpHeaders.TryGetValue(headerKey, out var headers))
44+
{
45+
headerRcords[headerKey] = headers.JoinAsString(";");
46+
}
47+
}
48+
49+
context.AuditInfo.SetProperty(HttpHeaderRecordKey, headerRcords);
50+
}
51+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# LINGYUN.Abp.AspNetCore.Auditing
2+
3+
审计日期扩展模块, 用于在审计日志中加入特定的Http请求头记录
4+
5+
## 模块引用
6+
7+
8+
```csharp
9+
[DependsOn(typeof(AbpAspNetCoreAuditingModule))]
10+
public class YouProjectModule : AbpModule
11+
{
12+
// other
13+
}
14+
```
15+
16+
## 配置项
17+
18+
* AbpAspNetCoreAuditingHeaderOptions.IsEnabled 是否在审计日志中记录Http请求头,默认: true
19+
* AbpAspNetCoreAuditingHeaderOptions.HttpHeaders 需要在审计日志中记录的Http请求头列表

0 commit comments

Comments
 (0)