|
|
@ -1,33 +1,16 @@ |
|
|
|
using System; |
|
|
|
using System.Collections.Generic; |
|
|
|
using System.IO; |
|
|
|
using System.Linq; |
|
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer; |
|
|
|
using Microsoft.AspNetCore.Builder; |
|
|
|
using Microsoft.AspNetCore.Cors; |
|
|
|
using Microsoft.Extensions.Configuration; |
|
|
|
using Microsoft.Extensions.DependencyInjection; |
|
|
|
using Microsoft.Extensions.DependencyInjection.Extensions; |
|
|
|
using Microsoft.OpenApi.Models; |
|
|
|
using Polly; |
|
|
|
using Volo.Abp; |
|
|
|
using Volo.Abp.Account; |
|
|
|
using Volo.Abp.AspNetCore.Mvc.AntiForgery; |
|
|
|
using Volo.Abp.AspNetCore.Mvc.Client; |
|
|
|
using Volo.Abp.AspNetCore.Mvc.UI.MultiTenancy; |
|
|
|
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.Http.Client.Authentication; |
|
|
|
using Volo.Abp.Http.Client.IdentityModel.Web; |
|
|
|
using Volo.Abp.Identity; |
|
|
|
using Volo.Abp.Localization; |
|
|
|
using Volo.Abp.Modularity; |
|
|
|
using Volo.Abp.MultiTenancy; |
|
|
|
using Volo.Abp.SettingManagement.EntityFrameworkCore; |
|
|
|
using Volo.Abp.Swashbuckle; |
|
|
|
using Volo.Abp.Users; |
|
|
|
using Win_in.Sfs.Auth; |
|
|
|
using Win_in.Sfs.Basedata.Application.Contracts; |
|
|
|
using Win_in.Sfs.FileStorage; |
|
|
@ -48,7 +31,8 @@ namespace Win_in.Sfs.Wms.Pda; |
|
|
|
typeof(AbpIdentityHttpApiClientModule), |
|
|
|
typeof(AbpAspNetCoreMvcClientModule), |
|
|
|
typeof(AbpHttpClientIdentityModelWebModule), |
|
|
|
typeof(SharedHostModule) |
|
|
|
typeof(SharedHostModule), |
|
|
|
typeof(AbpSettingManagementEntityFrameworkCoreModule) |
|
|
|
)] |
|
|
|
[DependsOn( |
|
|
|
typeof(AbpAspNetCoreSerilogModule), |
|
|
@ -70,84 +54,10 @@ namespace Win_in.Sfs.Wms.Pda; |
|
|
|
)] |
|
|
|
public class PdaHttpApiHostModule : ModuleBase<PdaHttpApiHostModule> |
|
|
|
{ |
|
|
|
private bool _isMultiTenancy = true; |
|
|
|
|
|
|
|
///// <summary>
|
|
|
|
///// 预配置服务
|
|
|
|
///// </summary>
|
|
|
|
///// <param name="context"></param>
|
|
|
|
//public override void PreConfigureServices(ServiceConfigurationContext context)
|
|
|
|
//{
|
|
|
|
// context.Services.AddAutoMapperObjectMapper<AbpDddDomainModule>();
|
|
|
|
// Configure<AbpAutoMapperOptions>(options => { options.AddMaps<PdaHttpApiHostModule>(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))
|
|
|
|
// )
|
|
|
|
// );
|
|
|
|
// });
|
|
|
|
// });
|
|
|
|
//}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
///
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="context"></param>
|
|
|
|
public override void PostConfigureServices(ServiceConfigurationContext context) |
|
|
|
{ |
|
|
|
// 这里必须手动替换一下
|
|
|
|
context.Services.Replace(ServiceDescriptor.Transient(typeof(IExternalUserLookupServiceProvider), typeof(HttpClientExternalUserLookupServiceProvider))); |
|
|
|
context.Services.Replace(ServiceDescriptor.Transient<IRemoteServiceHttpClientAuthenticator, SfsHttpContextIdentityModelRemoteServiceHttpClientAuthenticator>()); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 配置服务
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="context"></param>
|
|
|
|
public override void ConfigureServices(ServiceConfigurationContext context) |
|
|
|
{ |
|
|
|
//_ = context.Services.GetHostingEnvironment();
|
|
|
|
//var configuration = context.Services.GetConfiguration();
|
|
|
|
|
|
|
|
//context.SetConsoleTitleOfWebApp("Pda.Host");
|
|
|
|
|
|
|
|
//_isMultiTenancy = Convert.ToBoolean(configuration["IsMultiTenancy"]);
|
|
|
|
|
|
|
|
//ConfigureHttpClientProxies(context);
|
|
|
|
|
|
|
|
//ConfigureMultiTenancy();
|
|
|
|
|
|
|
|
base.ConfigureServices(context); |
|
|
|
GetXmlFiles(); |
|
|
|
|
|
|
|
//ConfigureLocalization();
|
|
|
|
|
|
|
|
//ConfigureAuthentication(context, configuration);
|
|
|
|
|
|
|
|
//ConfigureCors(context, configuration);
|
|
|
|
|
|
|
|
//ConfigureAntiForgery();
|
|
|
|
} |
|
|
|
|
|
|
|
private void ConfigureAntiForgery() |
|
|
|
{ |
|
|
|
Configure<AbpAntiForgeryOptions>(options => |
|
|
|
{ |
|
|
|
options.TokenCookie.SameSite = Microsoft.AspNetCore.Http.SameSiteMode.Lax; |
|
|
|
options.TokenCookie.Expiration = TimeSpan.FromDays(365); |
|
|
|
options.AutoValidateIgnoredHttpMethods.Add("POST"); |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
private static void ConfigureHttpClientProxies(ServiceConfigurationContext context) |
|
|
|
/// <inheritdoc/>
|
|
|
|
protected override void ConfigureHttpClientProxies() |
|
|
|
{ |
|
|
|
var context = this.ServiceConfigurationContext; |
|
|
|
context.Services.AddHttpClientProxies( |
|
|
|
typeof(AuthApplicationContractsModule).Assembly, |
|
|
|
"Auth" |
|
|
@ -185,128 +95,4 @@ public class PdaHttpApiHostModule : ModuleBase<PdaHttpApiHostModule> |
|
|
|
"FileStorage" |
|
|
|
); |
|
|
|
} |
|
|
|
|
|
|
|
private void ConfigureMultiTenancy() |
|
|
|
{ |
|
|
|
Configure<AbpMultiTenancyOptions>(options => { options.IsEnabled = _isMultiTenancy; }); |
|
|
|
} |
|
|
|
|
|
|
|
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(); |
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
private static void ConfigureAuthentication(ServiceConfigurationContext context, IConfiguration configuration) |
|
|
|
{ |
|
|
|
context.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) |
|
|
|
.AddJwtBearer(options => |
|
|
|
{ |
|
|
|
options.Authority = configuration["IdentityClients:Default:Authority"]; |
|
|
|
options.RequireHttpsMetadata = Convert.ToBoolean(configuration["IdentityClients:Default:RequireHttps"]); |
|
|
|
options.Audience = configuration["IdentityClients:Default:Scope"]; |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
private void ConfigureLocalization() |
|
|
|
{ |
|
|
|
Configure<AbpLocalizationOptions>(options => |
|
|
|
{ |
|
|
|
options.Languages.Add(new LanguageInfo("en", "en", "English")); |
|
|
|
options.Languages.Add(new LanguageInfo("zh-Hans", "zh-Hans", "简体中文")); |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 应用初始化
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="context"></param>
|
|
|
|
public override void OnApplicationInitialization(ApplicationInitializationContext context) |
|
|
|
{ |
|
|
|
var app = context.GetApplicationBuilder(); |
|
|
|
var env = context.GetEnvironment(); |
|
|
|
var configuration = context.GetConfiguration(); |
|
|
|
|
|
|
|
app.UseDeveloperExceptionPage(); |
|
|
|
//if (env.IsDevelopment())
|
|
|
|
//{
|
|
|
|
// app.UseDeveloperExceptionPage();
|
|
|
|
//}
|
|
|
|
//else
|
|
|
|
//{
|
|
|
|
// //app.UseErrorPage();
|
|
|
|
// app.UseHsts();
|
|
|
|
//}
|
|
|
|
|
|
|
|
app.UseHttpsRedirection(); |
|
|
|
app.UseCorrelationId(); |
|
|
|
app.UseStaticFiles(); |
|
|
|
app.UseRouting(); |
|
|
|
app.UseCors(); |
|
|
|
app.UseAuthentication(); |
|
|
|
if (_isMultiTenancy) |
|
|
|
{ |
|
|
|
app.UseMultiTenancy(); |
|
|
|
} |
|
|
|
app.UseAbpRequestLocalization(); |
|
|
|
app.UseAuthorization(); |
|
|
|
app.UseSwagger(); |
|
|
|
app.UseAbpSwaggerUI(options => |
|
|
|
{ |
|
|
|
options.SwaggerEndpoint("/swagger/v1/swagger.json", "Support APP API"); |
|
|
|
options.OAuthClientId(configuration["IdentityClients:Default:ClientId"]); |
|
|
|
options.OAuthClientSecret(configuration["IdentityClients:Default:ClientSecret"]); |
|
|
|
options.OAuthScopes(configuration["IdentityClients:Default:Scope"]); |
|
|
|
}); |
|
|
|
app.UseAuditing(); |
|
|
|
app.UseAbpSerilogEnrichers(); |
|
|
|
app.UseConfiguredEndpoints(); |
|
|
|
} |
|
|
|
|
|
|
|
private void ConfigureSwaggerServices(ServiceConfigurationContext context, IConfiguration configuration) |
|
|
|
{ |
|
|
|
context.Services.AddAbpSwaggerGenWithOAuth( |
|
|
|
configuration["IdentityClients:Default:Authority"], |
|
|
|
new Dictionary<string, string> |
|
|
|
{ |
|
|
|
{"Pda", "Pda API"} |
|
|
|
}, |
|
|
|
options => |
|
|
|
{ |
|
|
|
options.SwaggerDoc("v1", new OpenApiInfo { Title = "Pda API", Version = "v1" }); |
|
|
|
options.DocInclusionPredicate((docName, description) => true); |
|
|
|
options.CustomSchemaIds(type => type.FullName); |
|
|
|
|
|
|
|
GetXmlFiles().ForEach(file => |
|
|
|
{ |
|
|
|
options.IncludeXmlComments(file); |
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
/// <summary>
|
|
|
|
/// 获取当前目录下的xml文档
|
|
|
|
/// </summary>
|
|
|
|
/// <returns></returns>
|
|
|
|
private List<string> GetXmlFiles() |
|
|
|
{ |
|
|
|
var basePath = Path.GetDirectoryName(typeof(Program).Assembly.Location); |
|
|
|
var docXmlFiles = Directory.GetFiles(basePath, "*.xml"); |
|
|
|
return docXmlFiles.ToList(); |
|
|
|
} |
|
|
|
} |
|
|
|