diff --git a/Admin.NET/Admin.NET.Core/Attribute/DataMaskAttribute.cs b/Admin.NET/Admin.NET.Core/Attribute/DataMaskAttribute.cs
new file mode 100644
index 00000000..67d71f3c
--- /dev/null
+++ b/Admin.NET/Admin.NET.Core/Attribute/DataMaskAttribute.cs
@@ -0,0 +1,59 @@
+// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
+//
+// 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
+//
+// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
+
+namespace Admin.NET.Core;
+
+///
+/// 数据脱敏特性(支持自定义脱敏位置和脱敏字符)
+///
+[AttributeUsage(AttributeTargets.Property)]
+public class DataMaskAttribute : Attribute
+{
+ ///
+ /// 脱敏起始位置(从0开始)
+ ///
+ private int StartIndex { get; }
+
+ ///
+ /// 脱敏长度
+ ///
+ private int Length { get; }
+
+ ///
+ /// 脱敏字符(默认*)
+ ///
+ private char MaskChar { get; set; } = '*';
+
+ ///
+ /// 是否保留原始长度(默认true)
+ ///
+ private bool KeepLength { get; set; } = true;
+
+ public DataMaskAttribute(int startIndex, int length)
+ {
+ if (startIndex < 0) throw new ArgumentOutOfRangeException(nameof(startIndex));
+ if (length <= 0) throw new ArgumentOutOfRangeException(nameof(length));
+
+ StartIndex = startIndex;
+ Length = length;
+ }
+
+ ///
+ /// 执行脱敏处理
+ ///
+ public string Mask(string input)
+ {
+ if (string.IsNullOrEmpty(input) || input.Length <= StartIndex)
+ return input;
+
+ var maskedLength = Math.Min(Length, input.Length - StartIndex);
+ var maskStr = new string(MaskChar, KeepLength ? maskedLength : Math.Min(4, maskedLength));
+
+ return input.Substring(0, StartIndex) + maskStr +
+ (StartIndex + maskedLength < input.Length ?
+ input.Substring(StartIndex + maskedLength) : "");
+ }
+}
\ No newline at end of file
diff --git a/Admin.NET/Admin.NET.Core/Extension/ObjectExtension.cs b/Admin.NET/Admin.NET.Core/Extension/ObjectExtension.cs
index d448de26..8bf8a359 100644
--- a/Admin.NET/Admin.NET.Core/Extension/ObjectExtension.cs
+++ b/Admin.NET/Admin.NET.Core/Extension/ObjectExtension.cs
@@ -11,6 +11,16 @@ namespace Admin.NET.Core;
///
public static partial class ObjectExtension
{
+ ///
+ /// 类型属性列表映射表
+ ///
+ private static readonly ConcurrentDictionary PropertyCache = new();
+
+ ///
+ /// 脱敏特性缓存映射表
+ ///
+ private static readonly ConcurrentDictionary AttributeCache = new();
+
///
/// 判断类型是否实现某个泛型
///
@@ -272,4 +282,39 @@ public static partial class ObjectExtension
var json = JSON.Serialize(obj);
return JSON.Deserialize(json);
}
+
+ ///
+ /// 对带有特性字段进行脱敏处理
+ ///
+ public static T MaskSensitiveData(this T obj) where T : class
+ {
+ if (obj == null) return null;
+
+ var type = typeof(T);
+
+ // 获取或缓存属性集合
+ var properties = PropertyCache.GetOrAdd(type, t =>
+ t.GetProperties(BindingFlags.Public | BindingFlags.Instance)
+ .Where(p => p.PropertyType == typeof(string) && p.GetCustomAttribute() != null)
+ .ToArray());
+
+ // 并行处理可写属性
+ Parallel.ForEach(properties, prop =>
+ {
+ if (!prop.CanWrite) return;
+
+ // 获取或缓存特性
+ var maskAttr = AttributeCache.GetOrAdd(prop, p => p.GetCustomAttribute());
+
+ if (maskAttr == null) return;
+
+ // 处理非空字符串
+ if (prop.GetValue(obj) is string { Length: > 0 } value)
+ {
+ prop.SetValue(obj, maskAttr.Mask(value));
+ }
+ });
+
+ return obj;
+ }
}
\ No newline at end of file
diff --git a/Admin.NET/Admin.NET.Core/SqlSugar/SqlSugarPagedList.cs b/Admin.NET/Admin.NET.Core/SqlSugar/SqlSugarPagedList.cs
index c53ddc58..706eb441 100644
--- a/Admin.NET/Admin.NET.Core/SqlSugar/SqlSugarPagedList.cs
+++ b/Admin.NET/Admin.NET.Core/SqlSugar/SqlSugarPagedList.cs
@@ -113,6 +113,36 @@ public static class SqlSugarPagedExtensions
return CreateSqlSugarPagedList(items, total, pageIndex, pageSize);
}
+ ///
+ /// 脱敏分页拓展
+ ///
+ /// 对象
+ /// 当前页码,从1开始
+ /// 页码容量
+ ///
+ public static async Task> ToPagedListDataMaskAsync(this ISugarQueryable query, int pageIndex, int pageSize) where TEntity : class
+ {
+ RefAsync total = 0;
+ var items = await query.ToPageListAsync(pageIndex, pageSize, total);
+ items.ForEach(x => x.MaskSensitiveData());
+ return CreateSqlSugarPagedList(items, total, pageIndex, pageSize);
+ }
+
+ ///
+ /// 脱敏分页拓展
+ ///
+ /// 集合对象
+ /// 当前页码,从1开始
+ /// 页码容量
+ ///
+ public static SqlSugarPagedList ToPagedListDataMask(this IEnumerable list, int pageIndex, int pageSize) where TEntity : class
+ {
+ var total = list.Count();
+ var items = list.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
+ items.ForEach(x => x.MaskSensitiveData());
+ return CreateSqlSugarPagedList(items, total, pageIndex, pageSize);
+ }
+
///
/// 分页拓展
///