Blame view

netcore/src/Infrastructure/NCC/SensitiveDetection/Extensions/SensitiveDetectionServiceCollectionExtensions.cs 2.67 KB
de2bd2f9   “wangming”   项目初始化
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
  using NCC.Dependency;
  using NCC.SensitiveDetection;
  using System;
  
  namespace Microsoft.Extensions.DependencyInjection
  {
      /// <summary>
      /// 脱敏词汇处理服务
      /// </summary>
      [SuppressSniffer]
      public static class SensitiveDetectionServiceCollectionExtensions
      {
          /// <summary>
          /// 添加脱敏词汇服务
          /// <para>需要在入口程序集目录下创建 sensitive-words.txt</para>
          /// </summary>
          /// <param name="mvcBuilder"></param>
          /// <returns></returns>
          public static IMvcBuilder AddSensitiveDetection(this IMvcBuilder mvcBuilder)
          {
              var services = mvcBuilder.Services;
              services.AddSensitiveDetection();
  
              return mvcBuilder;
          }
  
          /// <summary>
          /// 添加脱敏词汇服务
          /// </summary>
          /// <typeparam name="TSensitiveDetectionProvider"></typeparam>
          /// <param name="mvcBuilder"></param>
          /// <param name="handle"></param>
          /// <returns></returns>
          public static IMvcBuilder AddSensitiveDetection<TSensitiveDetectionProvider>(this IMvcBuilder mvcBuilder, Action<IServiceCollection> handle = default)
              where TSensitiveDetectionProvider : class, ISensitiveDetectionProvider
          {
              var services = mvcBuilder.Services;
  
              // 注册脱敏词汇服务
              services.AddSensitiveDetection<TSensitiveDetectionProvider>(handle);
  
              return mvcBuilder;
          }
  
          /// <summary>
          /// 添加脱敏词汇服务
          /// <para>需要在入口程序集目录下创建 sensitive-words.txt</para>
          /// </summary>
          /// <param name="services"></param>
          /// <returns></returns>
          public static IServiceCollection AddSensitiveDetection(this IServiceCollection services)
          {
              return services.AddSensitiveDetection<SensitiveDetectionProvider>();
          }
  
          /// <summary>
          /// 添加脱敏词汇服务
          /// </summary>
          /// <typeparam name="TSensitiveDetectionProvider"></typeparam>
          /// <param name="services"></param>
          /// <param name="handle"></param>
          /// <returns></returns>
          public static IServiceCollection AddSensitiveDetection<TSensitiveDetectionProvider>(this IServiceCollection services, Action<IServiceCollection> handle = default)
              where TSensitiveDetectionProvider : class, ISensitiveDetectionProvider
          {
              // 注册脱敏词汇服务
              services.AddSingleton<ISensitiveDetectionProvider, TSensitiveDetectionProvider>();
  
              // 自定义配置
              handle?.Invoke(services);
  
              return services;
          }
      }
  }