2025-06-06 16:00:39 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Autofac;
|
|
|
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
|
|
|
using Microsoft.AspNetCore.Builder;
|
|
|
|
|
using Microsoft.AspNetCore.Hosting;
|
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
using Microsoft.AspNetCore.Http.Features;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc.Controllers;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc.Filters;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc.ModelBinding.Validation;
|
|
|
|
|
using Microsoft.AspNetCore.Server.Kestrel.Core;
|
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using Microsoft.Extensions.FileProviders;
|
|
|
|
|
using Microsoft.Extensions.Hosting;
|
|
|
|
|
using Microsoft.Extensions.Options;
|
|
|
|
|
using Microsoft.IdentityModel.Tokens;
|
|
|
|
|
using Microsoft.OpenApi.Models;
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using Newtonsoft.Json.Serialization;
|
|
|
|
|
using Quartz;
|
|
|
|
|
using Quartz.Impl;
|
|
|
|
|
using Swashbuckle.AspNetCore.SwaggerGen;
|
|
|
|
|
using VOL.Business.IServices;
|
|
|
|
|
using VOL.Business.Services.Norm;
|
|
|
|
|
using VOL.Core.AutoMapper;
|
|
|
|
|
using VOL.Core.Configuration;
|
|
|
|
|
using VOL.Core.Extensions;
|
|
|
|
|
using VOL.Core.Filters;
|
|
|
|
|
using VOL.Core.Middleware;
|
|
|
|
|
using VOL.Core.ObjectActionValidator;
|
|
|
|
|
using VOL.Core.Quartz;
|
|
|
|
|
using VOL.Core.Utilities.PDFHelper;
|
|
|
|
|
using VOL.Core.WorkFlow;
|
|
|
|
|
using VOL.Entity.DomainModels;
|
|
|
|
|
using VOL.WebApi.Controllers.Hubs;
|
|
|
|
|
using VOL.WebApi.Filter;
|
|
|
|
|
using VOL.WebApi.HostedService;
|
|
|
|
|
|
|
|
|
|
namespace VOL.WebApi
|
|
|
|
|
{
|
|
|
|
|
public class Startup
|
|
|
|
|
{
|
|
|
|
|
public Startup(IConfiguration configuration)
|
|
|
|
|
{
|
|
|
|
|
Configuration = configuration;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IConfiguration Configuration { get; }
|
|
|
|
|
private IServiceCollection Services { get; set; }
|
|
|
|
|
// This method gets called by the runtime. Use this method to add services to the container.
|
|
|
|
|
public void ConfigureServices(IServiceCollection services)
|
|
|
|
|
{
|
|
|
|
|
services.Configure<KestrelServerOptions>(options =>
|
|
|
|
|
{
|
|
|
|
|
options.Limits.MinRequestBodyDataRate = new MinDataRate(bytesPerSecond: 10000, gracePeriod: TimeSpan.FromSeconds(200));
|
|
|
|
|
options.Limits.RequestHeadersTimeout = TimeSpan.FromSeconds(600); // 请求头超时
|
|
|
|
|
options.Limits.KeepAliveTimeout = TimeSpan.FromSeconds(600); // Keep-Alive超时
|
|
|
|
|
options.Limits.MaxRequestBodySize = 1024 * 1024 * 1024; // 请求体最大大小
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
//初始化模型验证配置
|
|
|
|
|
services.UseMethodsModelParameters().UseMethodsGeneralParameters();
|
|
|
|
|
services.AddSingleton<IObjectModelValidator>(new NullObjectModelValidator());
|
|
|
|
|
Services = services;
|
|
|
|
|
// services.Replace( ServiceDescriptor.Transient<IControllerActivator, ServiceBasedControllerActivator>());
|
|
|
|
|
services.AddSession();
|
|
|
|
|
services.AddMemoryCache();
|
|
|
|
|
services.AddHttpContextAccessor();
|
|
|
|
|
services.AddMvc(options =>
|
|
|
|
|
{
|
|
|
|
|
options.Filters.Add(typeof(ApiAuthorizeFilter));
|
|
|
|
|
options.Filters.Add(typeof(ActionExecuteFilter));
|
|
|
|
|
options.Filters.Add(typeof(ResultFilter));
|
|
|
|
|
options.Filters.Add(typeof(ExceptionFilter));
|
|
|
|
|
// options.SuppressAsyncSuffixInActionNames = false;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
services.AddControllers(config =>
|
|
|
|
|
{
|
|
|
|
|
//config.Filters.Add<IOTSignatureValidationFilter>();
|
|
|
|
|
}).AddNewtonsoftJson(op =>
|
|
|
|
|
{
|
|
|
|
|
op.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
|
|
|
|
|
op.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
|
2025-06-06 16:55:14 +08:00
|
|
|
|
});
|
2025-06-06 16:00:39 +08:00
|
|
|
|
|
|
|
|
|
// 注册过滤器
|
|
|
|
|
services.AddScoped<IOTSignatureValidationFilter>();
|
|
|
|
|
services.AddScoped<AISignatureValidationFilter>();
|
|
|
|
|
services.AddScoped<ValidateDeviceFilter>();
|
|
|
|
|
|
|
|
|
|
services.AddControllersWithViews(options =>
|
|
|
|
|
{
|
|
|
|
|
options.Filters.Add(typeof(UserIdCheckFilter));
|
|
|
|
|
//options.Filters.Add(typeof(RequestLoggingFilter));
|
|
|
|
|
//options.Filters.Add(typeof(ResponseLoggingFilter));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
Services.AddAuthentication(options =>
|
|
|
|
|
{
|
|
|
|
|
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
|
|
|
|
|
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
|
|
|
|
|
})
|
|
|
|
|
.AddJwtBearer(options =>
|
|
|
|
|
{
|
|
|
|
|
options.TokenValidationParameters = new TokenValidationParameters
|
|
|
|
|
{
|
|
|
|
|
SaveSigninToken = true,//保存token,后台验证token是否生效(重要)
|
|
|
|
|
ValidateIssuer = true,//是否验证Issuer
|
|
|
|
|
ValidateAudience = true,//是否验证Audience
|
|
|
|
|
ValidateLifetime = true,//是否验证失效时间
|
|
|
|
|
ValidateIssuerSigningKey = true,//是否验证SecurityKey
|
|
|
|
|
ValidAudience = AppSetting.Secret.Audience,//Audience
|
|
|
|
|
ValidIssuer = AppSetting.Secret.Issuer,//Issuer,这两项和前面签发jwt的设置一致
|
|
|
|
|
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(AppSetting.Secret.JWT))
|
|
|
|
|
};
|
|
|
|
|
options.Events = new JwtBearerEvents()
|
|
|
|
|
{
|
|
|
|
|
OnChallenge = context =>
|
|
|
|
|
{
|
|
|
|
|
context.HandleResponse();
|
|
|
|
|
context.Response.Clear();
|
|
|
|
|
context.Response.ContentType = "application/json";
|
|
|
|
|
context.Response.StatusCode = 401;
|
|
|
|
|
context.Response.WriteAsync(new { message = "授权未通过", status = false, code = 401 }.Serialize());
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
//必须appsettings.json中配置
|
|
|
|
|
string corsUrls = Configuration["CorsUrls"];
|
|
|
|
|
if (string.IsNullOrEmpty(corsUrls))
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("请配置跨请求的前端Url");
|
|
|
|
|
}
|
|
|
|
|
services.AddCors(options =>
|
|
|
|
|
{
|
|
|
|
|
options.AddDefaultPolicy(
|
|
|
|
|
builder =>
|
|
|
|
|
{
|
|
|
|
|
builder.AllowAnyOrigin()
|
|
|
|
|
.SetPreflightMaxAge(TimeSpan.FromSeconds(2520))
|
|
|
|
|
.AllowAnyHeader().AllowAnyMethod();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
|
|
|
|
|
//services.AddControllers();
|
|
|
|
|
services.AddSwaggerGen(c =>
|
|
|
|
|
{
|
|
|
|
|
//分为2份接口文档
|
2025-06-06 16:55:14 +08:00
|
|
|
|
c.SwaggerDoc("v1", new OpenApiInfo { Title = "VOL.Core后台Api", Version = "v1", Description = "这是对文档的描述。。" });
|
|
|
|
|
c.SwaggerDoc("v2", new OpenApiInfo { Title = "VOL.Core对外三方Api", Version = "v2", Description = "xxx接口文档" });
|
|
|
|
|
c.SwaggerDoc("v3", new OpenApiInfo { Title = "SmartSportsServer-Api", Version = "v3", Description = "SmartSportsServer-Api接口文档" });
|
|
|
|
|
c.SwaggerDoc("v4", new OpenApiInfo { Title = "IOT-Api", Version = "v4", Description = "物联网-Api接口文档" });
|
|
|
|
|
c.SwaggerDoc("v5", new OpenApiInfo { Title = "Ai-Api", Version = "v5", Description = "Ai-Api接口文档" });
|
2025-06-06 16:00:39 +08:00
|
|
|
|
c.SwaggerDoc("v6", new OpenApiInfo { Title = "AiApp-Api", Version = "v6", Description = "AiApp-Api接口文档" });
|
|
|
|
|
|
|
|
|
|
string xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
|
|
|
|
|
string xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
|
|
|
|
|
c.IncludeXmlComments(xmlPath, true);
|
|
|
|
|
c.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, "VOL.WebApi.xml"));
|
|
|
|
|
c.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, "VOL.Model.xml"));
|
|
|
|
|
|
|
|
|
|
c.DocInclusionPredicate((docName, apiDes) =>
|
|
|
|
|
{
|
|
|
|
|
if (!apiDes.TryGetMethodInfo(out MethodInfo method))
|
|
|
|
|
return false;
|
|
|
|
|
/*使用ApiExplorerSettingsAttribute里面的GroupName进行特性标识
|
|
|
|
|
* DeclaringType只能获取controller上的特性
|
|
|
|
|
* 我们这里是想以action的特性为主
|
|
|
|
|
* */
|
|
|
|
|
var version = method.DeclaringType.GetCustomAttributes(true).OfType<ApiExplorerSettingsAttribute>().Select(m => m.GroupName);
|
|
|
|
|
if (docName == "v1" && !version.Any())
|
|
|
|
|
return true;
|
|
|
|
|
//这里获取action的特性
|
|
|
|
|
var actionVersion = method.GetCustomAttributes(true).OfType<ApiExplorerSettingsAttribute>().Select(m => m.GroupName);
|
|
|
|
|
if (actionVersion.Any())
|
|
|
|
|
return actionVersion.Any(v => v == docName);
|
|
|
|
|
return version.Any(v => v == docName);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var security = new Dictionary<string, IEnumerable<string>> { { AppSetting.Secret.Issuer, new string[] { } } };
|
|
|
|
|
c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme()
|
|
|
|
|
{
|
|
|
|
|
Description = "JWT授权token前面需要加上字段Bearer与一个空格,如Bearer token",
|
|
|
|
|
Name = "Authorization",
|
|
|
|
|
In = ParameterLocation.Header,
|
|
|
|
|
Type = SecuritySchemeType.ApiKey,
|
|
|
|
|
BearerFormat = "JWT",
|
|
|
|
|
Scheme = "Bearer"
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
c.AddSecurityRequirement(new OpenApiSecurityRequirement
|
|
|
|
|
{
|
|
|
|
|
{
|
|
|
|
|
new OpenApiSecurityScheme
|
|
|
|
|
{
|
|
|
|
|
Reference = new OpenApiReference {
|
|
|
|
|
Type = ReferenceType.SecurityScheme,
|
|
|
|
|
Id = "Bearer"
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
new string[] { }
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
services.AddControllers()
|
|
|
|
|
.ConfigureApiBehaviorOptions(options =>
|
|
|
|
|
{
|
|
|
|
|
options.SuppressConsumesConstraintForFormFileParameters = true;
|
|
|
|
|
options.SuppressInferBindingSourcesForParameters = true;
|
|
|
|
|
options.SuppressModelStateInvalidFilter = true;
|
|
|
|
|
options.SuppressMapClientErrors = true;
|
|
|
|
|
options.ClientErrorMapping[404].Link =
|
|
|
|
|
"https://*/404";
|
|
|
|
|
});
|
|
|
|
|
services.AddSignalR();
|
|
|
|
|
//services.AddSingleton(typeof(IConverter), new SynchronizedConverter(new PdfTools()));
|
|
|
|
|
//services.AddTransient<IPDFService, PDFService>();
|
|
|
|
|
|
|
|
|
|
services.AddHttpClient();
|
|
|
|
|
//Services.AddTransient<HttpResultfulJob>();
|
|
|
|
|
// Services.AddSingleton<StatResultJob>();
|
|
|
|
|
// services.AddHostedService<QuartzStartup>();
|
|
|
|
|
Services.AddSingleton<ISchedulerFactory, StdSchedulerFactory>();
|
|
|
|
|
Services.AddSingleton<Quartz.Spi.IJobFactory, IOCJobFactory>();
|
|
|
|
|
services.AddSingleton<QuartzStartup>();
|
|
|
|
|
services.AddHostedService<QuartzStartup>();
|
|
|
|
|
services.AddHostedService(provider => provider.GetRequiredService<QuartzStartup>());
|
|
|
|
|
|
|
|
|
|
// 注册具体的 Job
|
|
|
|
|
services.AddSingleton<ActivityStartJob>();
|
|
|
|
|
services.AddSingleton<ActivityEndJob>();
|
|
|
|
|
|
|
|
|
|
services.AddAutoMapper(typeof(MappingProfile));
|
|
|
|
|
|
|
|
|
|
//设置文件上传大小限制
|
|
|
|
|
//设置文件上传大小限制
|
|
|
|
|
services.Configure<FormOptions>(x =>
|
|
|
|
|
{
|
|
|
|
|
x.MultipartBodyLengthLimit = 1024 * 1024 * 1024;//1024M
|
|
|
|
|
});
|
|
|
|
|
services.Configure<KestrelServerOptions>(options =>
|
|
|
|
|
{
|
|
|
|
|
options.Limits.MaxRequestBodySize = 1024 * 1024 * 1024;//1024M
|
|
|
|
|
});
|
|
|
|
|
services.Configure<IISServerOptions>(options =>
|
|
|
|
|
{
|
|
|
|
|
options.MaxRequestBodySize = 1024 * 1024 * 1024;//1024M
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
public void ConfigureContainer(ContainerBuilder builder)
|
|
|
|
|
{
|
|
|
|
|
Services.AddModule(builder, Configuration);
|
|
|
|
|
//初始化流程表,表里面必须有AuditStatus字段
|
|
|
|
|
WorkFlowContainer.Instance
|
|
|
|
|
//name= 流程实例名称
|
|
|
|
|
//filterFields流程实例名称
|
|
|
|
|
.Use<SellOrder>(name: "订单管理",
|
|
|
|
|
filterFields: x => new { x.OrderType, x.Qty, x.CreateID, x.SellNo }, //审批过滤条件的字段
|
|
|
|
|
formFields: x => new { x.OrderType, x.TranNo, x.Qty, x.SellNo, x.Creator }//审批界面显示的字段
|
|
|
|
|
)
|
|
|
|
|
.Use<App_Expert>()
|
|
|
|
|
//run方法必须写在最后位置
|
|
|
|
|
.Run();
|
|
|
|
|
}
|
|
|
|
|
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
|
|
|
|
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
|
|
|
|
{
|
|
|
|
|
//app.UseQuartz(env);
|
|
|
|
|
if (env.IsDevelopment())
|
|
|
|
|
{
|
|
|
|
|
app.UseDeveloperExceptionPage();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
//app.UseQuartz(env);
|
|
|
|
|
}
|
|
|
|
|
//接口输入输出日志中间件
|
|
|
|
|
app.UseMiddleware<RequestResponseLoggingMiddleware>();
|
|
|
|
|
app.UseMiddleware<ExceptionHandlerMiddleWare>();
|
|
|
|
|
app.UseStaticFiles().UseStaticFiles(new StaticFileOptions
|
|
|
|
|
{
|
|
|
|
|
ServeUnknownFileTypes = true
|
|
|
|
|
});
|
|
|
|
|
app.UseDefaultFiles();
|
|
|
|
|
app.Use(HttpRequestMiddleware.Context);
|
|
|
|
|
|
|
|
|
|
//2021.06.27增加创建默认upload文件夹
|
|
|
|
|
string _uploadPath = (env.ContentRootPath + "/Upload").ReplacePath();
|
|
|
|
|
|
|
|
|
|
if (!Directory.Exists(_uploadPath))
|
|
|
|
|
{
|
|
|
|
|
Directory.CreateDirectory(_uploadPath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
app.UseStaticFiles(new StaticFileOptions()
|
|
|
|
|
{
|
|
|
|
|
FileProvider = new PhysicalFileProvider(
|
|
|
|
|
Path.Combine(Directory.GetCurrentDirectory(), @"Upload")),
|
|
|
|
|
//配置访问虚拟目录时文件夹别名
|
|
|
|
|
RequestPath = "/Upload",
|
|
|
|
|
OnPrepareResponse = (Microsoft.AspNetCore.StaticFiles.StaticFileResponseContext staticFile) =>
|
|
|
|
|
{
|
|
|
|
|
//可以在此处读取请求的信息进行权限认证
|
|
|
|
|
// staticFile.File
|
|
|
|
|
// staticFile.Context.Response.StatusCode;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
//配置HttpContext
|
|
|
|
|
app.UseStaticHttpContext();
|
|
|
|
|
|
2025-06-09 17:10:47 +08:00
|
|
|
|
//app.UseSwagger();
|
|
|
|
|
//app.UseSwaggerUI(c =>
|
|
|
|
|
//{
|
|
|
|
|
// //2个下拉框选项 选择对应的文档
|
|
|
|
|
// c.SwaggerEndpoint("/swagger/v1/swagger.json", "VOL.Core后台Api");
|
|
|
|
|
// c.SwaggerEndpoint("/swagger/v2/swagger.json", "测试第三方Api");
|
|
|
|
|
// c.SwaggerEndpoint("/swagger/v3/swagger.json", "SmartSportsServer-Api");
|
|
|
|
|
// c.SwaggerEndpoint("/swagger/v4/swagger.json", "物联网-Api");
|
|
|
|
|
// c.SwaggerEndpoint("/swagger/v5/swagger.json", "Ai-Api");
|
|
|
|
|
// c.SwaggerEndpoint("/swagger/v6/swagger.json", "AiApp-Api");
|
|
|
|
|
// c.RoutePrefix = "";
|
|
|
|
|
//});
|
2025-06-06 16:55:14 +08:00
|
|
|
|
|
2025-06-06 16:00:39 +08:00
|
|
|
|
app.UseRouting();
|
|
|
|
|
app.UseCors();
|
|
|
|
|
app.UseAuthentication();
|
|
|
|
|
app.UseAuthorization();
|
|
|
|
|
app.UseEndpoints(endpoints =>
|
|
|
|
|
{
|
|
|
|
|
endpoints.MapControllers();
|
|
|
|
|
//配置SignalR
|
|
|
|
|
if (AppSetting.UseSignalR)
|
|
|
|
|
{
|
|
|
|
|
string corsUrls = Configuration["CorsUrls"];
|
|
|
|
|
|
|
|
|
|
endpoints.MapHub<HomePageMessageHub>("/message")
|
|
|
|
|
.RequireCors(t =>
|
|
|
|
|
t.WithOrigins(corsUrls.Split(',')).
|
|
|
|
|
AllowAnyMethod().
|
|
|
|
|
AllowAnyHeader().
|
|
|
|
|
AllowCredentials());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Swagger注释帮助类
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class SwaggerDocTag : IDocumentFilter
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 添加附加注释
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="swaggerDoc"></param>
|
|
|
|
|
/// <param name="context"></param>
|
|
|
|
|
public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
|
|
|
|
|
{
|
|
|
|
|
//添加对应的控制器描述
|
|
|
|
|
swaggerDoc.Tags = new List<OpenApiTag>
|
|
|
|
|
{
|
|
|
|
|
new OpenApiTag { Name = "Test", Description = "这是描述" },
|
|
|
|
|
//new OpenApiTag { Name = "你的控制器名字,不带Controller", Description = "控制器描述" },
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|