Startup.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. // 麻省理工学院许可证
  2. //
  3. // 版权所有 (c) 2021-2023 zuohuaijun,大名科技(天津)有限公司 联系电话/微信:18020030720 QQ:515096995
  4. //
  5. // 特此免费授予获得本软件的任何人以处理本软件的权利,但须遵守以下条件:在所有副本或重要部分的软件中必须包括上述版权声明和本许可声明。
  6. //
  7. // 软件按“原样”提供,不提供任何形式的明示或暗示的保证,包括但不限于对适销性、适用性和非侵权的保证。
  8. // 在任何情况下,作者或版权持有人均不对任何索赔、损害或其他责任负责,无论是因合同、侵权或其他方式引起的,与软件或其使用或其他交易有关。
  9. using Admin.NET.Core;
  10. using Admin.NET.Core.Service;
  11. using AspNetCoreRateLimit;
  12. using Furion;
  13. using Furion.SpecificationDocument;
  14. using Furion.VirtualFileServer;
  15. using IGeekFan.AspNetCore.Knife4jUI;
  16. using Microsoft.AspNetCore.Builder;
  17. using Microsoft.AspNetCore.Hosting;
  18. using Microsoft.AspNetCore.HttpOverrides;
  19. using Microsoft.Extensions.DependencyInjection;
  20. using Microsoft.Extensions.Hosting;
  21. using Newtonsoft.Json;
  22. using OnceMi.AspNetCore.OSS;
  23. using System;
  24. namespace Admin.NET.Web.Core;
  25. public class Startup : AppStartup
  26. {
  27. public void ConfigureServices(IServiceCollection services)
  28. {
  29. // 配置选项
  30. services.AddProjectOptions();
  31. // 缓存注册
  32. services.AddCache();
  33. // SqlSugar
  34. services.AddSqlSugar();
  35. // JWT
  36. services.AddJwt<JwtHandler>(enableGlobalAuthorize: true)
  37. // 添加 Signature 身份验证
  38. .AddSignatureAuthentication(options =>
  39. {
  40. options.Events = SysOpenAccessService.GetSignatureAuthenticationEventImpl();
  41. });
  42. // 允许跨域
  43. services.AddCorsAccessor();
  44. // 远程请求
  45. services.AddRemoteRequest();
  46. // 任务队列
  47. services.AddTaskQueue();
  48. // 任务调度
  49. services.AddSchedule(options =>
  50. {
  51. options.AddPersistence<DbJobPersistence>(); // 添加作业持久化器
  52. });
  53. // 脱敏检测
  54. services.AddSensitiveDetection();
  55. // Json序列化设置
  56. static void SetNewtonsoftJsonSetting(JsonSerializerSettings setting)
  57. {
  58. setting.DateFormatHandling = DateFormatHandling.IsoDateFormat;
  59. setting.DateTimeZoneHandling = DateTimeZoneHandling.Local;
  60. setting.DateFormatString = "yyyy-MM-dd HH:mm:ss"; // 时间格式化
  61. setting.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; // 忽略循环引用
  62. // setting.ContractResolver = new CamelCasePropertyNamesContractResolver(); // 解决动态对象属性名大写
  63. // setting.NullValueHandling = NullValueHandling.Ignore; // 忽略空值
  64. // setting.Converters.AddLongTypeConverters(); // long转string(防止js精度溢出) 超过16位开启
  65. // setting.MetadataPropertyHandling = MetadataPropertyHandling.Ignore; // 解决DateTimeOffset异常
  66. // setting.DateParseHandling = DateParseHandling.None; // 解决DateTimeOffset异常
  67. // setting.Converters.Add(new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles.AssumeUniversal }); // 解决DateTimeOffset异常
  68. };
  69. services.AddControllersWithViews()
  70. .AddAppLocalization()
  71. .AddNewtonsoftJson(options => SetNewtonsoftJsonSetting(options.SerializerSettings))
  72. //.AddXmlSerializerFormatters()
  73. //.AddXmlDataContractSerializerFormatters()
  74. .AddInjectWithUnifyResult<AdminResultProvider>();
  75. // 三方授权登录OAuth
  76. services.AddOAuth();
  77. // ElasticSearch
  78. services.AddElasticSearch();
  79. // 配置Nginx转发获取客户端真实IP
  80. // 注1:如果负载均衡不是在本机通过 Loopback 地址转发请求的,一定要加上options.KnownNetworks.Clear()和options.KnownProxies.Clear()
  81. // 注2:如果设置环境变量 ASPNETCORE_FORWARDEDHEADERS_ENABLED 为 True,则不需要下面的配置代码
  82. services.Configure<ForwardedHeadersOptions>(options =>
  83. {
  84. options.ForwardedHeaders = ForwardedHeaders.All;
  85. options.KnownNetworks.Clear();
  86. options.KnownProxies.Clear();
  87. });
  88. // 限流服务
  89. services.AddInMemoryRateLimiting();
  90. services.AddSingleton<IRateLimitConfiguration, RateLimitConfiguration>();
  91. // 事件总线
  92. services.AddEventBus(options =>
  93. {
  94. options.UseUtcTimestamp = false;
  95. // 不启用事件日志
  96. options.LogEnabled = false;
  97. // 事件执行器(失败重试)
  98. options.AddExecutor<RetryEventHandlerExecutor>();
  99. //// 替换事件源存储器
  100. //options.ReplaceStorer(serviceProvider =>
  101. //{
  102. // var redisCache = serviceProvider.GetService<ICache>();
  103. // // 创建默认内存通道事件源对象,可自定义队列路由key,比如这里是 eventbus
  104. // return new RedisEventSourceStorer(redisCache, "eventbus", 3000);
  105. //});
  106. });
  107. // OSS对象存储
  108. var ossOpt = App.GetConfig<OSSProviderOptions>("OSSProvider", true);
  109. services.AddOSSService(Enum.GetName(ossOpt.Provider), "OSSProvider");
  110. // 模板引擎
  111. services.AddViewEngine();
  112. // 即时通讯
  113. services.AddSignalR(SetNewtonsoftJsonSetting);
  114. // 系统日志
  115. services.AddLoggingSetup();
  116. // 验证码
  117. services.AddCaptcha();
  118. // 控制台logo
  119. services.AddConsoleLogo();
  120. }
  121. public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
  122. {
  123. if (env.IsDevelopment())
  124. {
  125. app.UseDeveloperExceptionPage();
  126. app.UseForwardedHeaders();
  127. }
  128. else
  129. {
  130. app.UseExceptionHandler("/Home/Error");
  131. app.UseForwardedHeaders();
  132. app.UseHsts();
  133. }
  134. // 添加状态码拦截中间件
  135. app.UseUnifyResultStatusCodes();
  136. // 配置多语言
  137. app.UseAppLocalization();
  138. //// 启用HTTPS
  139. //app.UseHttpsRedirection();
  140. // 特定文件类型(文件后缀)处理
  141. var contentTypeProvider = FS.GetFileExtensionContentTypeProvider();
  142. // contentTypeProvider.Mappings[".文件后缀"] = "MIME 类型";
  143. app.UseStaticFiles(new StaticFileOptions
  144. {
  145. ContentTypeProvider = contentTypeProvider
  146. });
  147. app.UseRouting();
  148. app.UseCorsAccessor();
  149. // 限流组件(在跨域之后)
  150. app.UseIpRateLimiting();
  151. app.UseClientRateLimiting();
  152. app.UseAuthentication();
  153. app.UseAuthorization();
  154. // 任务调度看板
  155. app.UseScheduleUI();
  156. // 配置Swagger-Knife4UI(路由前缀一致代表独立,不同则代表共存)
  157. app.UseKnife4UI(options =>
  158. {
  159. options.RoutePrefix = "kapi";
  160. foreach (var groupInfo in SpecificationDocumentBuilder.GetOpenApiGroups())
  161. {
  162. options.SwaggerEndpoint("/" + groupInfo.RouteTemplate, groupInfo.Title);
  163. }
  164. });
  165. app.UseInject(string.Empty);
  166. app.UseEndpoints(endpoints =>
  167. {
  168. // 注册集线器
  169. endpoints.MapHubs();
  170. endpoints.MapControllerRoute(
  171. name: "default",
  172. pattern: "{controller=Home}/{action=Index}/{id?}");
  173. });
  174. }
  175. }