You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
357 lines
13 KiB
357 lines
13 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Net.Http.Headers;
|
|
using System.Reflection;
|
|
using Microsoft.AspNetCore.Builder;
|
|
using Microsoft.AspNetCore.Cors;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.AspNetCore.Mvc.ApiExplorer;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Hosting;
|
|
using Polly;
|
|
using Serilog;
|
|
using Swashbuckle.AspNetCore.SwaggerGen;
|
|
using Volo.Abp;
|
|
using Volo.Abp.Account.Web;
|
|
using Volo.Abp.AspNetCore.Authentication.JwtBearer;
|
|
using Volo.Abp.AspNetCore.MultiTenancy;
|
|
using Volo.Abp.AspNetCore.Mvc;
|
|
using Volo.Abp.AspNetCore.Mvc.AntiForgery;
|
|
using Volo.Abp.AspNetCore.Mvc.UI.Bundling;
|
|
using Volo.Abp.AspNetCore.Mvc.UI.Theme.Basic;
|
|
using Volo.Abp.AspNetCore.Mvc.UI.Theme.Basic.Bundling;
|
|
using Volo.Abp.AspNetCore.Serilog;
|
|
using Volo.Abp.Autofac;
|
|
using Volo.Abp.AutoMapper;
|
|
using Volo.Abp.Domain;
|
|
using Volo.Abp.Http.Client;
|
|
using Volo.Abp.Identity;
|
|
using Volo.Abp.Localization;
|
|
using Volo.Abp.Modularity;
|
|
using Volo.Abp.SettingManagement.EntityFrameworkCore;
|
|
using Volo.Abp.Swashbuckle;
|
|
using Volo.Abp.VirtualFileSystem;
|
|
using Win_in.Sfs.Basedata.Application.Contracts;
|
|
using Win_in.Sfs.Shared.Domain.Shared;
|
|
using Win_in.Sfs.Shared.Host;
|
|
using Win_in.Sfs.Wms.DataExchange.Authenticaitons;
|
|
using Win_in.Sfs.Wms.DataExchange.EntityFrameworkCore;
|
|
using Win_in.Sfs.Wms.Inventory.Application.Contracts;
|
|
using Win_in.Sfs.Wms.Store.Application.Contracts;
|
|
|
|
namespace Win_in.Sfs.Wms.DataExchange;
|
|
|
|
[DependsOn(
|
|
typeof(AbpAutofacModule),
|
|
typeof(AbpAspNetCoreMultiTenancyModule),
|
|
typeof(AbpAspNetCoreMvcUiBasicThemeModule),
|
|
typeof(AbpAspNetCoreAuthenticationJwtBearerModule),
|
|
typeof(AbpSettingManagementEntityFrameworkCoreModule),
|
|
typeof(AbpAccountWebIdentityServerModule),
|
|
typeof(AbpAspNetCoreSerilogModule),
|
|
typeof(AbpHttpClientModule),
|
|
typeof(AbpSwashbuckleModule)
|
|
)]
|
|
[DependsOn(
|
|
typeof(DataExchangeHttpApiModule),
|
|
typeof(DataExchangeApplicationModule),
|
|
typeof(DataExchangeEntityFrameworkCoreModule)
|
|
)]
|
|
[DependsOn(
|
|
typeof(AbpIdentityApplicationContractsModule),
|
|
typeof(BasedataApplicationContractsModule),
|
|
typeof(InventoryApplicationContractsModule),
|
|
typeof(StoreApplicationContractsModule)
|
|
)]
|
|
public class DataExchangeHttpApiHostModule : AbpModule
|
|
{
|
|
public override void PreConfigureServices(ServiceConfigurationContext context)
|
|
{
|
|
context.Services.AddAutoMapperObjectMapper<AbpDddDomainModule>();
|
|
Configure<AbpAutoMapperOptions>(options => { options.AddMaps<DataExchangeHttpApiHostModule>(validate: true); });
|
|
|
|
PreConfigure<AbpHttpClientBuilderOptions>(options =>
|
|
{
|
|
//Polly 重试3次
|
|
options.ProxyClientBuildActions.Add((remoteServiceName, clientBuilder) =>
|
|
{
|
|
clientBuilder.AddTransientHttpErrorPolicy(policyBuilder =>
|
|
policyBuilder.WaitAndRetryAsync(
|
|
3,
|
|
i => TimeSpan.FromSeconds(Math.Pow(2, i))
|
|
)
|
|
);
|
|
});
|
|
|
|
//默认添加Authorization Header: Bearer Token
|
|
options.ProxyClientActions.Add((a, s, h) =>
|
|
{
|
|
var httpAuthorizationHandler = s.GetService<HttpAuthorizationHandler>();
|
|
if (httpAuthorizationHandler != null && httpAuthorizationHandler.IsLoggedIn())
|
|
{
|
|
h.DefaultRequestHeaders.Authorization =
|
|
new AuthenticationHeaderValue("Bearer", httpAuthorizationHandler.GetCurrentBearer());
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
public override void ConfigureServices(ServiceConfigurationContext context)
|
|
{
|
|
var configuration = context.Services.GetConfiguration();
|
|
var hostingEnvironment = context.Services.GetHostingEnvironment();
|
|
|
|
context.SetConsoleTitleOfWebApp("DataExchange.Host");
|
|
|
|
context.Services.Configure<AuthenticationOptions>(configuration.GetSection("Authentication"));
|
|
|
|
//?? LYF 新加
|
|
Configure<AbpAntiForgeryOptions>(options =>
|
|
{
|
|
options.AutoValidate = false;
|
|
});
|
|
|
|
ConfigureHttpClientProxies(context);
|
|
|
|
ConfigureBundles();
|
|
ConfigureUrls(configuration);
|
|
ConfigureConventionalControllers();
|
|
ConfigureAuthentication(context, configuration);
|
|
ConfigureLocalization();
|
|
ConfigureVirtualFileSystem(context);
|
|
ConfigureCors(context, configuration);
|
|
//ConfigureSwaggerServices(context, configuration);
|
|
//ConfigureAuthorization(context,configuration);
|
|
ConfigureSwaggerServices22(context.Services);
|
|
}
|
|
|
|
private static void ConfigureHttpClientProxies(ServiceConfigurationContext context)
|
|
{
|
|
context.Services.AddHttpClientProxies(
|
|
typeof(BasedataApplicationContractsModule).Assembly,
|
|
"BaseData"
|
|
);
|
|
context.Services.AddHttpClientProxies(
|
|
typeof(InventoryApplicationContractsModule).Assembly,
|
|
"Inventory"
|
|
);
|
|
context.Services.AddHttpClientProxies(
|
|
typeof(StoreApplicationContractsModule).Assembly,
|
|
"Store"
|
|
);
|
|
context.Services.AddHttpClientProxies(
|
|
typeof(AbpIdentityApplicationContractsModule).Assembly,
|
|
"Default"
|
|
);
|
|
}
|
|
|
|
private void ConfigureBundles()
|
|
{
|
|
Configure<AbpBundlingOptions>(options =>
|
|
{
|
|
options.StyleBundles.Configure(
|
|
BasicThemeBundles.Styles.Global,
|
|
bundle => { bundle.AddFiles("/global-styles.css"); }
|
|
);
|
|
});
|
|
}
|
|
|
|
private static void ConfigureUrls(IConfiguration configuration)
|
|
{
|
|
// Configure<AppUrlOptions>(options =>
|
|
// {
|
|
// options.Applications["MVC"].RootUrl = configuration["App:SelfUrl"];
|
|
// options.RedirectAllowedUrls.AddRange(configuration["App:RedirectAllowedUrls"].Split(','));
|
|
//
|
|
// options.Applications["Angular"].RootUrl = configuration["App:ClientUrl"];
|
|
// options.Applications["Angular"].Urls[AccountUrlNames.PasswordReset] = "account/reset-password";
|
|
// });
|
|
}
|
|
|
|
private void ConfigureVirtualFileSystem(ServiceConfigurationContext context)
|
|
{
|
|
var hostingEnvironment = context.Services.GetHostingEnvironment();
|
|
|
|
if (hostingEnvironment.IsDevelopment())
|
|
{
|
|
Configure<AbpVirtualFileSystemOptions>(options =>
|
|
{
|
|
// options.FileSets.ReplaceEmbeddedByPhysical<DataExchangeDomainSharedModule>(
|
|
// Path.Combine(hostingEnvironment.ContentRootPath,
|
|
// $"..{Path.DirectorySeparatorChar}Win_in.Sfs.Wms.DataExchange.Domain.Shared"));
|
|
// options.FileSets.ReplaceEmbeddedByPhysical<DataExchangeDomainModule>(
|
|
// Path.Combine(hostingEnvironment.ContentRootPath,
|
|
// $"..{Path.DirectorySeparatorChar}Win_in.Sfs.Wms.DataExchange.Domain"));
|
|
// options.FileSets.ReplaceEmbeddedByPhysical<DataExchangeApplicationContractsModule>(
|
|
// Path.Combine(hostingEnvironment.ContentRootPath,
|
|
// $"..{Path.DirectorySeparatorChar}Win_in.Sfs.Wms.DataExchange.Application.Contracts"));
|
|
// options.FileSets.ReplaceEmbeddedByPhysical<DataExchangeApplicationModule>(
|
|
// Path.Combine(hostingEnvironment.ContentRootPath,
|
|
// $"..{Path.DirectorySeparatorChar}Win_in.Sfs.Wms.DataExchange.Application"));
|
|
});
|
|
}
|
|
}
|
|
|
|
private void ConfigureConventionalControllers()
|
|
{
|
|
Configure<AbpAspNetCoreMvcOptions>(options =>
|
|
{
|
|
options.ConventionalControllers.Create(typeof(DataExchangeApplicationModule).Assembly);
|
|
});
|
|
}
|
|
|
|
private static void ConfigureAuthentication(ServiceConfigurationContext context, IConfiguration configuration)
|
|
{
|
|
var isAlwaysAllowAuthorization = configuration.GetValue<bool>("AlwaysAllowAuthorization");
|
|
if (isAlwaysAllowAuthorization)
|
|
{
|
|
//绕过授权服务,用于测试
|
|
context.Services.AddAlwaysAllowAuthorization();
|
|
}
|
|
else
|
|
{
|
|
context.Services.AddAuthentication()
|
|
.AddJwtBearer(options =>
|
|
{
|
|
options.Authority = configuration["AuthServer:Authority"];
|
|
options.RequireHttpsMetadata = Convert.ToBoolean(configuration["AuthServer:RequireHttpsMetadata"]);
|
|
options.Audience = "Auth";
|
|
});
|
|
}
|
|
}
|
|
|
|
private void ConfigureLocalization()
|
|
{
|
|
Configure<AbpLocalizationOptions>(options =>
|
|
{
|
|
options.Languages.Add(new LanguageInfo("en", "en", "English"));
|
|
options.Languages.Add(new LanguageInfo("zh-Hans", "zh-Hans", "简体中文"));
|
|
});
|
|
}
|
|
|
|
private static void ConfigureCors(ServiceConfigurationContext context, IConfiguration configuration)
|
|
{
|
|
var origins = configuration.GetSection("App:CorsOrigins").Get<string[]>();
|
|
|
|
context.Services.AddCors(options =>
|
|
{
|
|
options.AddDefaultPolicy(builder =>
|
|
{
|
|
builder
|
|
.WithOrigins(
|
|
origins
|
|
.Select(o => o.RemovePostFix("/"))
|
|
.ToArray()
|
|
)
|
|
.WithAbpExposedHeaders()
|
|
.SetIsOriginAllowedToAllowWildcardSubdomains()
|
|
.AllowAnyHeader()
|
|
.AllowAnyMethod()
|
|
.AllowCredentials();
|
|
});
|
|
});
|
|
}
|
|
|
|
public override void OnApplicationInitialization(ApplicationInitializationContext context)
|
|
{
|
|
var app = context.GetApplicationBuilder();
|
|
var env = context.GetEnvironment();
|
|
var configuration = context.GetConfiguration();
|
|
|
|
// if (env.IsDevelopment())
|
|
{
|
|
app.UseDeveloperExceptionPage();
|
|
}
|
|
|
|
app.UseAbpRequestLocalization();
|
|
|
|
// if (!env.IsDevelopment())
|
|
// {
|
|
// app.UseErrorPage();
|
|
// }
|
|
|
|
app.UseCorrelationId();
|
|
app.UseStaticFiles();
|
|
app.UseRouting();
|
|
app.UseCors();
|
|
app.UseAuthentication();
|
|
app.UseJwtTokenMiddleware();
|
|
|
|
app.UseUnitOfWork();
|
|
//app.UseIdentityServer();
|
|
app.UseAuthorization();
|
|
|
|
app.UseSwagger();
|
|
app.UseAbpSwaggerUI(options =>
|
|
{
|
|
// c.SwaggerEndpoint("/swagger/v1/swagger.json", "DataExchange API");
|
|
//
|
|
// var configuration = context.ServiceProvider.GetRequiredService<IConfiguration>();
|
|
// c.OAuthClientId(configuration["AuthServer:SwaggerClientId"]);
|
|
// c.OAuthClientSecret(configuration["AuthServer:SwaggerClientSecret"]);
|
|
// c.OAuthScopes("DataExchange");
|
|
|
|
foreach (var groupName in typeof(SwaggerGroupConsts).GetFields()
|
|
.Select(f => f.GetValue(null)?.ToString()))
|
|
{
|
|
Log.Information(groupName);
|
|
options.SwaggerEndpoint($"/swagger/{groupName}/swagger.json", $"{groupName} API");
|
|
}
|
|
options.OAuthClientId(configuration["AuthServer:SwaggerClientId"]);
|
|
options.OAuthClientSecret(configuration["AuthServer:SwaggerClientSecret"]);
|
|
options.OAuthScopes("DataExchange");
|
|
});
|
|
app.UseAuditing();
|
|
app.UseAbpSerilogEnrichers();
|
|
app.UseConfiguredEndpoints();
|
|
|
|
//初始化Settings
|
|
context.ServiceProvider.InitSettings();
|
|
}
|
|
|
|
#region 配置Swagger
|
|
|
|
private void ConfigureSwaggerServices22(IServiceCollection services)
|
|
{
|
|
//services.AddSwaggerGen(
|
|
// options =>
|
|
// {
|
|
// //options.SwaggerDoc("v1", new OpenApiInfo { Title = "NewJit API", Version = "v1" });
|
|
// //options.DocInclusionPredicate((docName, description) => true);
|
|
// //options.CustomSchemaIds(type => type.FullName);
|
|
|
|
// //遍历Swagger分组,注册SwaggerDoc
|
|
// foreach (var groupName in typeof(SwaggerGroupConsts).GetFields()
|
|
// .Select(f => f.GetValue(null)?.ToString()))
|
|
// {
|
|
// options.SwaggerDoc(groupName,
|
|
// new OpenApiInfo { Title = $"{groupName} API", Version = groupName });
|
|
// }
|
|
|
|
// //根据APIExplorerSetting判断是否包含于Swagger文档当前分组
|
|
// //options.DocInclusionPredicate((docName, description) => true);
|
|
// options.DocInclusionPredicate(IsIncludeInDoc22);
|
|
|
|
// //反射注入全部Application和Application.Contract程序集说明
|
|
// var xmlapppath = Path.Combine(AppContext.BaseDirectory, "Win_in.Sfs.Wms.DataExchange.Application.xml");
|
|
// if (File.Exists(xmlapppath))
|
|
// {
|
|
// options.IncludeXmlComments(xmlapppath);
|
|
// }
|
|
|
|
// xmlapppath = Path.Combine(AppContext.BaseDirectory, "Win_in.Sfs.Wms.DataExchange.Application.Contracts.xml");
|
|
// if (File.Exists(xmlapppath))
|
|
// {
|
|
// options.IncludeXmlComments(xmlapppath);
|
|
// }
|
|
|
|
// options.CustomSchemaIds(type => type.FullName);
|
|
// }
|
|
//);
|
|
}
|
|
|
|
#endregion 配置Swagger
|
|
}
|
|
|