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.
340 lines
13 KiB
340 lines
13 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Net.Http;
|
|
using System.Reflection;
|
|
using Microsoft.AspNetCore.Builder;
|
|
using Microsoft.AspNetCore.Cors;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.AspNetCore.Mvc.ApiExplorer;
|
|
using Microsoft.AspNetCore.Mvc.Formatters;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Hosting;
|
|
using Win_in.Sfs.Scp.WebApi.EntityFrameworkCore;
|
|
using Win_in.Sfs.Scp.WebApi.MultiTenancy;
|
|
using Volo.Abp.AspNetCore.Mvc.UI.Theme.Basic;
|
|
using Microsoft.OpenApi.Models;
|
|
using Serilog;
|
|
using Swashbuckle.AspNetCore.SwaggerGen;
|
|
using Volo.Abp;
|
|
using Volo.Abp.Account;
|
|
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.UI.Bundling;
|
|
using Volo.Abp.AspNetCore.Mvc.UI.Theme.Basic.Bundling;
|
|
using Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared;
|
|
using Volo.Abp.AspNetCore.Serilog;
|
|
using Volo.Abp.Autofac;
|
|
using Volo.Abp.Localization;
|
|
using Volo.Abp.Modularity;
|
|
using Volo.Abp.Swashbuckle;
|
|
using Volo.Abp.UI.Navigation.Urls;
|
|
using Volo.Abp.VirtualFileSystem;
|
|
using Win_in.Sfs.Scp.v1.EntityFrameworkCore;
|
|
|
|
namespace Win_in.Sfs.Scp.WebApi
|
|
{
|
|
[DependsOn(
|
|
typeof(WebApiHttpApiModule),
|
|
typeof(AbpAutofacModule),
|
|
typeof(AbpAspNetCoreMultiTenancyModule),
|
|
typeof(WebApiApplicationModule),
|
|
typeof(WebApiEntityFrameworkCoreModule),
|
|
typeof(AbpAspNetCoreMvcUiBasicThemeModule),
|
|
typeof(AbpAspNetCoreAuthenticationJwtBearerModule),
|
|
typeof(AbpAccountWebIdentityServerModule),
|
|
typeof(AbpAspNetCoreSerilogModule),
|
|
typeof(AbpSwashbuckleModule)
|
|
)]
|
|
[DependsOn(
|
|
typeof(V1ScpEntityFrameworkCoreModule))]
|
|
public class WebApiHttpApiHostModule : AbpModule
|
|
{
|
|
public override void ConfigureServices(ServiceConfigurationContext context)
|
|
{
|
|
var configuration = context.Services.GetConfiguration();
|
|
var hostingEnvironment = context.Services.GetHostingEnvironment();
|
|
|
|
|
|
|
|
ConfigureBundles();
|
|
ConfigureUrls(configuration);
|
|
ConfigureConventionalControllers();
|
|
ConfigureAuthentication(context, configuration);
|
|
ConfigureLocalization();
|
|
ConfigureVirtualFileSystem(context);
|
|
ConfigureCors(context, configuration);
|
|
ConfigureSwaggerServices(context, configuration);
|
|
|
|
ConfigureAuthorization(context,configuration);
|
|
}
|
|
|
|
private static void ConfigureAuthorization(ServiceConfigurationContext context, IConfiguration configuration)
|
|
{
|
|
var alwaysAllowAuthorization = Convert.ToBoolean(configuration["AlwaysAllowAuthorization"]);
|
|
if (alwaysAllowAuthorization)
|
|
{
|
|
//绕过授权服务
|
|
context.Services.AddAlwaysAllowAuthorization();
|
|
}
|
|
}
|
|
|
|
private void ConfigureBundles()
|
|
{
|
|
Configure<AbpBundlingOptions>(options =>
|
|
{
|
|
options.StyleBundles.Configure(
|
|
BasicThemeBundles.Styles.Global,
|
|
bundle => { bundle.AddFiles("/global-styles.css"); }
|
|
);
|
|
});
|
|
}
|
|
|
|
private 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<WebApiDomainSharedModule>(
|
|
Path.Combine(hostingEnvironment.ContentRootPath,
|
|
$"..{Path.DirectorySeparatorChar}Win_in.Sfs.Scp.WebApi.Domain.Shared"));
|
|
options.FileSets.ReplaceEmbeddedByPhysical<WebApiDomainModule>(
|
|
Path.Combine(hostingEnvironment.ContentRootPath,
|
|
$"..{Path.DirectorySeparatorChar}Win_in.Sfs.Scp.WebApi.Domain"));
|
|
options.FileSets.ReplaceEmbeddedByPhysical<WebApiApplicationContractsModule>(
|
|
Path.Combine(hostingEnvironment.ContentRootPath,
|
|
$"..{Path.DirectorySeparatorChar}Win_in.Sfs.Scp.WebApi.Application.Contracts"));
|
|
options.FileSets.ReplaceEmbeddedByPhysical<WebApiApplicationModule>(
|
|
Path.Combine(hostingEnvironment.ContentRootPath,
|
|
$"..{Path.DirectorySeparatorChar}Win_in.Sfs.Scp.WebApi.Application"));
|
|
});
|
|
}
|
|
}
|
|
|
|
private void ConfigureConventionalControllers()
|
|
{
|
|
Configure<AbpAspNetCoreMvcOptions>(options =>
|
|
{
|
|
options.ConventionalControllers.Create(typeof(WebApiApplicationModule).Assembly);
|
|
});
|
|
}
|
|
|
|
private void ConfigureAuthentication(ServiceConfigurationContext context, IConfiguration configuration)
|
|
{
|
|
context.Services.AddAuthentication()
|
|
.AddJwtBearer(options =>
|
|
{
|
|
options.Authority = configuration["AuthServer:Authority"];
|
|
options.RequireHttpsMetadata = Convert.ToBoolean(configuration["AuthServer:RequireHttpsMetadata"]);
|
|
options.Audience = "WebApi";
|
|
options.BackchannelHttpHandler = new HttpClientHandler
|
|
{
|
|
ServerCertificateCustomValidationCallback =
|
|
HttpClientHandler.DangerousAcceptAnyServerCertificateValidator
|
|
};
|
|
});
|
|
}
|
|
|
|
private void ConfigureSwaggerServices(ServiceConfigurationContext context, IConfiguration configuration)
|
|
{
|
|
// context.Services.AddAbpSwaggerGenWithOAuth(
|
|
// configuration["AuthServer:Authority"],
|
|
// new Dictionary<string, string>
|
|
// {
|
|
// {"WebApi", "WebApi API"}
|
|
// },
|
|
// options =>
|
|
// {
|
|
// options.SwaggerDoc("v1", new OpenApiInfo {Title = "WebApi API", Version = "v1"});
|
|
// options.DocInclusionPredicate((docName, description) => true);
|
|
// options.CustomSchemaIds(type => type.FullName);
|
|
// });
|
|
|
|
context.Services.AddAbpSwaggerGenWithOAuth(
|
|
configuration["AuthServer:Authority"],
|
|
new Dictionary<string, string>
|
|
{
|
|
{ "WebApi", "WebApi API" }
|
|
},
|
|
options =>
|
|
{
|
|
//遍历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(IsIncludeInDoc);
|
|
|
|
//反射注入全部Application和Application.Contract程序集说明
|
|
GetXmlFiles().ForEach(file =>
|
|
{
|
|
options.IncludeXmlComments(file);
|
|
Log.Information(" TEST-SWAGGER PATH ==> " + file);
|
|
});
|
|
|
|
options.DocumentFilter<HiddenApiFilter>();
|
|
options.CustomSchemaIds(type => type.FullName);
|
|
});
|
|
}
|
|
|
|
/// <summary>
|
|
/// 判断是否应该加入当前分组
|
|
/// </summary>
|
|
/// <param name="docName"></param>
|
|
/// <param name="apiDesc"></param>
|
|
/// <returns></returns>
|
|
private bool IsIncludeInDoc(string docName, ApiDescription apiDesc)
|
|
{
|
|
if (!apiDesc.TryGetMethodInfo(out MethodInfo methodInfo))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
var groups = methodInfo.ReflectedType.GetCustomAttributes(true)
|
|
.OfType<ApiExplorerSettingsAttribute>()
|
|
.Select(attr => attr.GroupName)
|
|
.ToList();
|
|
|
|
if (docName == SwaggerGroupConsts.Default)
|
|
{
|
|
if (groups.Count == 0)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
return apiDesc.GroupName == SwaggerGroupConsts.Default;
|
|
}
|
|
|
|
return docName == apiDesc.GroupName;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取当前目录下的xml文档
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
private List<string> GetXmlFiles()
|
|
{
|
|
var basePath = Path.GetDirectoryName(typeof(Program).Assembly.Location);
|
|
Log.Information(" TEST- base Path PATH ==> " + basePath);
|
|
//Console.WriteLine(" TEST- base Path PATH ==> " + basePath);
|
|
var files1 = Directory.GetFiles(basePath, "*.Application*.xml");
|
|
return files1.ToList();
|
|
}
|
|
|
|
|
|
private void ConfigureLocalization()
|
|
{
|
|
Configure<AbpLocalizationOptions>(options =>
|
|
{
|
|
options.Languages.Add(new LanguageInfo("en", "en", "English"));
|
|
options.Languages.Add(new LanguageInfo("zh-Hans", "zh-Hans", "简体中文"));
|
|
});
|
|
}
|
|
|
|
private void ConfigureCors(ServiceConfigurationContext context, IConfiguration configuration)
|
|
{
|
|
context.Services.AddCors(options =>
|
|
{
|
|
options.AddDefaultPolicy( builder =>
|
|
{
|
|
builder
|
|
.WithOrigins(
|
|
configuration["App:CorsOrigins"]
|
|
.Split(",", StringSplitOptions.RemoveEmptyEntries)
|
|
.Select(o => o.RemovePostFix("/"))
|
|
.ToArray()
|
|
)
|
|
.WithAbpExposedHeaders()
|
|
.SetIsOriginAllowedToAllowWildcardSubdomains()
|
|
.AllowAnyHeader()
|
|
.AllowAnyMethod()
|
|
.AllowCredentials();
|
|
});
|
|
});
|
|
}
|
|
|
|
public override void OnApplicationInitialization(ApplicationInitializationContext context)
|
|
{
|
|
var app = context.GetApplicationBuilder();
|
|
var env = context.GetEnvironment();
|
|
|
|
if (env.IsDevelopment())
|
|
{
|
|
app.UseDeveloperExceptionPage();
|
|
}
|
|
|
|
app.UseAbpRequestLocalization();
|
|
|
|
if (!env.IsDevelopment())
|
|
{
|
|
app.UseErrorPage();
|
|
}
|
|
|
|
app.UseCorrelationId();
|
|
app.UseStaticFiles();
|
|
app.UseRouting();
|
|
app.UseCors();
|
|
app.UseAuthentication();
|
|
app.UseJwtTokenMiddleware();
|
|
|
|
if (MultiTenancyConsts.IsEnabled)
|
|
{
|
|
app.UseMultiTenancy();
|
|
}
|
|
|
|
app.UseUnitOfWork();
|
|
app.UseIdentityServer();
|
|
app.UseAuthorization();
|
|
|
|
app.UseSwagger();
|
|
app.UseAbpSwaggerUI(options =>
|
|
{
|
|
// c.SwaggerEndpoint("/swagger/v1/swagger.json", "WebApi API");
|
|
//
|
|
// var configuration = context.ServiceProvider.GetRequiredService<IConfiguration>();
|
|
// c.OAuthClientId(configuration["AuthServer:SwaggerClientId"]);
|
|
// c.OAuthClientSecret(configuration["AuthServer:SwaggerClientSecret"]);
|
|
// c.OAuthScopes("WebApi");
|
|
|
|
foreach (var groupName in typeof(SwaggerGroupConsts).GetFields()
|
|
.Select(f => f.GetValue(null).ToString()))
|
|
{
|
|
Log.Information(groupName);
|
|
options.SwaggerEndpoint($"/swagger/{groupName}/swagger.json", $"{groupName} API");
|
|
}
|
|
var configuration = context.GetConfiguration();
|
|
options.OAuthClientId(configuration["AuthServer:SwaggerClientId"]);
|
|
options.OAuthClientSecret(configuration["AuthServer:SwaggerClientSecret"]);
|
|
options.OAuthScopes("WebApi");
|
|
|
|
});
|
|
|
|
app.UseAuditing();
|
|
app.UseAbpSerilogEnrichers();
|
|
app.UseConfiguredEndpoints();
|
|
}
|
|
}
|
|
}
|
|
|