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.
311 lines
10 KiB
311 lines
10 KiB
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.Swashbuckle;
|
|
using Volo.Abp.Users;
|
|
using Win_in.Sfs.Auth;
|
|
using Win_in.Sfs.Basedata.Application.Contracts;
|
|
using Win_in.Sfs.FileStorage;
|
|
using Win_in.Sfs.Label.Application.Contracts;
|
|
using Win_in.Sfs.Message.Application.Contracts;
|
|
using Win_in.Sfs.Shared.Host;
|
|
using Win_in.Sfs.Wms.Inventory.Application.Contracts;
|
|
|
|
// using Win_in.Sfs.Wms.Pda.Authenticaitons;
|
|
using Win_in.Sfs.Wms.Store.Application.Contracts;
|
|
|
|
namespace Win_in.Sfs.Wms.Pda;
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
[DependsOn(
|
|
typeof(AbpIdentityHttpApiClientModule),
|
|
typeof(AbpAspNetCoreMvcClientModule),
|
|
typeof(AbpHttpClientIdentityModelWebModule),
|
|
typeof(SharedHostModule)
|
|
)]
|
|
[DependsOn(
|
|
typeof(AbpAspNetCoreSerilogModule),
|
|
typeof(AbpAspNetCoreMvcUiMultiTenancyModule),
|
|
typeof(AbpAutofacModule),
|
|
typeof(AbpAspNetCoreSerilogModule),
|
|
typeof(AbpSwashbuckleModule),
|
|
typeof(AbpHttpClientModule),
|
|
typeof(AbpAutoMapperModule)
|
|
)]
|
|
[DependsOn(
|
|
typeof(AbpIdentityApplicationContractsModule),
|
|
typeof(BasedataApplicationContractsModule),
|
|
typeof(InventoryApplicationContractsModule),
|
|
typeof(StoreApplicationContractsModule),
|
|
typeof(LabelApplicationContractsModule),
|
|
typeof(MessageApplicationContractsModule),
|
|
typeof(AuthApplicationContractsModule)
|
|
)]
|
|
public class PdaHttpApiHostModule : AbpModule
|
|
{
|
|
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();
|
|
|
|
ConfigureSwaggerServices(context, configuration);
|
|
|
|
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)
|
|
{
|
|
context.Services.AddHttpClientProxies(
|
|
typeof(AuthApplicationContractsModule).Assembly,
|
|
"Auth"
|
|
);
|
|
context.Services.AddHttpClientProxies(
|
|
typeof(AbpAccountApplicationContractsModule).Assembly,
|
|
"Auth"
|
|
);
|
|
context.Services.AddHttpClientProxies(
|
|
typeof(MessageApplicationContractsModule).Assembly,
|
|
"Message"
|
|
);
|
|
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(LabelApplicationContractsModule).Assembly,
|
|
"Label"
|
|
);
|
|
context.Services.AddHttpClientProxies(
|
|
typeof(AbpIdentityApplicationContractsModule).Assembly,
|
|
"Default"
|
|
);
|
|
context.Services.AddHttpClientProxies(
|
|
typeof(FileStorageApplicationContractsModule).Assembly,
|
|
"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();
|
|
}
|
|
}
|
|
|