2020-10-22 14:59:36 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
2021-05-07 23:38:53 +08:00
|
|
|
|
using System.Reflection;
|
2020-10-22 14:59:36 +08:00
|
|
|
|
using Autofac;
|
|
|
|
|
using IdentityServer4.AccessTokenValidation;
|
2021-01-04 22:17:49 +08:00
|
|
|
|
using Infrastructure;
|
2020-10-22 14:59:36 +08:00
|
|
|
|
using Infrastructure.Extensions.AutofacManager;
|
2021-05-08 16:29:11 +08:00
|
|
|
|
using Infrastructure.Middleware;
|
2020-10-22 14:59:36 +08:00
|
|
|
|
using Microsoft.AspNetCore.Builder;
|
|
|
|
|
using Microsoft.AspNetCore.DataProtection;
|
2020-12-27 00:00:28 +08:00
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2020-10-22 14:59:36 +08:00
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using Microsoft.Extensions.FileProviders;
|
|
|
|
|
using Microsoft.Extensions.Hosting;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using Microsoft.OpenApi.Models;
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using OpenAuth.App;
|
|
|
|
|
using OpenAuth.App.HostedService;
|
|
|
|
|
using OpenAuth.Repository;
|
|
|
|
|
using OpenAuth.WebApi.Model;
|
2023-08-26 23:52:30 +08:00
|
|
|
|
using SqlSugar;
|
2020-10-22 14:59:36 +08:00
|
|
|
|
using Swashbuckle.AspNetCore.SwaggerUI;
|
|
|
|
|
|
|
|
|
|
namespace OpenAuth.WebApi
|
|
|
|
|
{
|
|
|
|
|
public class Startup
|
|
|
|
|
{
|
|
|
|
|
public IHostEnvironment Environment { get; }
|
|
|
|
|
public IConfiguration Configuration { get; }
|
|
|
|
|
|
|
|
|
|
public Startup(IConfiguration configuration, IHostEnvironment environment)
|
|
|
|
|
{
|
|
|
|
|
Configuration = configuration;
|
|
|
|
|
Environment = environment;
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-07 23:38:53 +08:00
|
|
|
|
|
2020-10-22 14:59:36 +08:00
|
|
|
|
// This method gets called by the runtime. Use this method to add services to the container.
|
|
|
|
|
public void ConfigureServices(IServiceCollection services)
|
|
|
|
|
{
|
2021-06-11 23:40:55 +08:00
|
|
|
|
//在startup中需要强制创建log4net
|
|
|
|
|
var loggerFactory = LoggerFactory.Create(builder => { builder.AddLog4Net(); });
|
2021-05-08 12:05:51 +08:00
|
|
|
|
ILogger logger = loggerFactory.CreateLogger<Startup>();
|
2021-05-07 23:38:53 +08:00
|
|
|
|
|
|
|
|
|
var identityServer =
|
|
|
|
|
((ConfigurationSection) Configuration.GetSection("AppSetting:IdentityServerUrl")).Value;
|
2020-10-22 14:59:36 +08:00
|
|
|
|
if (!string.IsNullOrEmpty(identityServer))
|
|
|
|
|
{
|
|
|
|
|
services.AddAuthorization();
|
|
|
|
|
|
|
|
|
|
services.AddAuthentication(IdentityServerAuthenticationDefaults.AuthenticationScheme)
|
|
|
|
|
.AddJwtBearer(options =>
|
|
|
|
|
{
|
|
|
|
|
options.Authority = identityServer;
|
2021-05-07 23:38:53 +08:00
|
|
|
|
options.RequireHttpsMetadata = false; // 指定是否为HTTPS
|
2020-10-22 14:59:36 +08:00
|
|
|
|
options.Audience = "openauthapi";
|
2021-05-07 23:38:53 +08:00
|
|
|
|
});
|
2020-10-22 14:59:36 +08:00
|
|
|
|
}
|
2021-06-11 23:40:55 +08:00
|
|
|
|
|
2021-05-25 00:49:12 +08:00
|
|
|
|
// 添加MiniProfiler服务
|
|
|
|
|
services.AddMiniProfiler(options =>
|
|
|
|
|
{
|
|
|
|
|
// 设定访问分析结果URL的路由基地址
|
|
|
|
|
options.RouteBasePath = "/profiler";
|
2021-06-11 23:40:55 +08:00
|
|
|
|
|
2021-05-25 00:49:12 +08:00
|
|
|
|
options.ColorScheme = StackExchange.Profiling.ColorScheme.Auto;
|
|
|
|
|
options.PopupRenderPosition = StackExchange.Profiling.RenderPosition.BottomLeft;
|
|
|
|
|
options.PopupShowTimeWithChildren = true;
|
|
|
|
|
options.PopupShowTrivial = true;
|
|
|
|
|
options.SqlFormatter = new StackExchange.Profiling.SqlFormatters.InlineFormatter();
|
2021-05-26 01:14:41 +08:00
|
|
|
|
// options.IgnoredPaths.Add("/swagger/");
|
2021-06-11 23:40:55 +08:00
|
|
|
|
}).AddEntityFramework(); //显示SQL语句及耗时
|
2020-10-22 14:59:36 +08:00
|
|
|
|
|
2021-05-07 23:38:53 +08:00
|
|
|
|
//添加swagger
|
2020-10-22 14:59:36 +08:00
|
|
|
|
services.AddSwaggerGen(option =>
|
|
|
|
|
{
|
2021-05-07 23:38:53 +08:00
|
|
|
|
foreach (var controller in GetControllers())
|
2020-10-22 14:59:36 +08:00
|
|
|
|
{
|
2021-06-11 23:40:55 +08:00
|
|
|
|
var groupname = GetSwaggerGroupName(controller);
|
|
|
|
|
|
|
|
|
|
option.SwaggerDoc(groupname, new OpenApiInfo
|
2021-05-07 23:38:53 +08:00
|
|
|
|
{
|
|
|
|
|
Version = "v1",
|
2021-06-11 23:40:55 +08:00
|
|
|
|
Title = groupname,
|
2021-05-07 23:38:53 +08:00
|
|
|
|
Description = "by yubaolee"
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-22 14:59:36 +08:00
|
|
|
|
logger.LogInformation($"api doc basepath:{AppContext.BaseDirectory}");
|
|
|
|
|
foreach (var name in Directory.GetFiles(AppContext.BaseDirectory, "*.*",
|
2023-09-01 01:44:25 +08:00
|
|
|
|
SearchOption.AllDirectories).Where(f => Path.GetExtension(f).ToLower() == ".xml"))
|
2020-10-22 14:59:36 +08:00
|
|
|
|
{
|
2021-05-07 23:38:53 +08:00
|
|
|
|
option.IncludeXmlComments(name, includeControllerXmlComments: true);
|
2021-01-04 22:17:49 +08:00
|
|
|
|
// logger.LogInformation($"find api file{name}");
|
2020-10-22 14:59:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
option.OperationFilter<GlobalHttpHeaderOperationFilter>(); // 添加httpHeader参数
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(identityServer))
|
|
|
|
|
{
|
|
|
|
|
//接入identityserver
|
|
|
|
|
option.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme
|
|
|
|
|
{
|
|
|
|
|
Type = SecuritySchemeType.OAuth2,
|
|
|
|
|
Description = "OAuth2登陆授权",
|
|
|
|
|
Flows = new OpenApiOAuthFlows
|
|
|
|
|
{
|
|
|
|
|
Implicit = new OpenApiOAuthFlow
|
|
|
|
|
{
|
|
|
|
|
AuthorizationUrl = new Uri($"{identityServer}/connect/authorize"),
|
|
|
|
|
Scopes = new Dictionary<string, string>
|
|
|
|
|
{
|
2021-05-07 23:38:53 +08:00
|
|
|
|
{"openauthapi", "同意openauth.webapi 的访问权限"} //指定客户端请求的api作用域。 如果为空,则客户端无法访问
|
2020-10-22 14:59:36 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
option.OperationFilter<AuthResponsesOperationFilter>();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
services.Configure<AppSetting>(Configuration.GetSection("AppSetting"));
|
2021-05-08 16:29:11 +08:00
|
|
|
|
services.AddControllers(option => { option.Filters.Add<OpenAuthFilter>(); })
|
|
|
|
|
.ConfigureApiBehaviorOptions(options =>
|
|
|
|
|
{
|
|
|
|
|
// 禁用自动模态验证
|
2021-05-09 21:07:04 +08:00
|
|
|
|
// options.SuppressModelStateInvalidFilter = true;
|
|
|
|
|
|
|
|
|
|
//启动WebAPI自动模态验证,处理返回值
|
2024-11-29 17:34:53 +08:00
|
|
|
|
options.InvalidModelStateResponseFactory = (ActionContext context) =>
|
2021-05-09 21:07:04 +08:00
|
|
|
|
{
|
|
|
|
|
var problems = new CustomBadRequest(context);
|
|
|
|
|
|
|
|
|
|
return new BadRequestObjectResult(problems);
|
|
|
|
|
};
|
2021-05-08 16:29:11 +08:00
|
|
|
|
}).AddNewtonsoftJson(options =>
|
|
|
|
|
{
|
|
|
|
|
//忽略循环引用
|
|
|
|
|
options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
|
|
|
|
|
//不使用驼峰样式的key
|
|
|
|
|
//options.SerializerSettings.ContractResolver = new DefaultContractResolver();
|
|
|
|
|
options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
|
|
|
|
|
});
|
2020-10-22 14:59:36 +08:00
|
|
|
|
services.AddMemoryCache();
|
|
|
|
|
services.AddCors();
|
|
|
|
|
// todo:如果正式 环境请用下面的方式限制随意访问跨域
|
|
|
|
|
// var origins = new []
|
|
|
|
|
// {
|
|
|
|
|
// "http://localhost:1803",
|
|
|
|
|
// "http://localhost:52789"
|
|
|
|
|
// };
|
|
|
|
|
// if (Environment.IsProduction())
|
|
|
|
|
// {
|
|
|
|
|
// origins = new []
|
|
|
|
|
// {
|
2021-07-22 19:33:55 +08:00
|
|
|
|
// "http://demo.openauth.net.cn:1803",
|
|
|
|
|
// "http://demo.openauth.net.cn:52789"
|
2020-10-22 14:59:36 +08:00
|
|
|
|
// };
|
|
|
|
|
// }
|
|
|
|
|
// services.AddCors(option=>option.AddPolicy("cors", policy =>
|
|
|
|
|
// policy.AllowAnyHeader().AllowAnyMethod().AllowCredentials().WithOrigins(origins)));
|
2021-01-27 13:42:38 +08:00
|
|
|
|
|
2020-10-22 14:59:36 +08:00
|
|
|
|
//在startup里面只能通过这种方式获取到appsettings里面的值,不能用IOptions😰
|
2021-05-07 23:38:53 +08:00
|
|
|
|
var dbtypes = ((ConfigurationSection) Configuration.GetSection("AppSetting:DbTypes")).GetChildren()
|
2021-04-21 18:02:01 +08:00
|
|
|
|
.ToDictionary(x => x.Key, x => x.Value);
|
2021-01-04 22:17:49 +08:00
|
|
|
|
var connectionString = Configuration.GetConnectionString("OpenAuthDBContext");
|
2021-04-21 18:02:01 +08:00
|
|
|
|
logger.LogInformation($"系统配置的数据库类型:{JsonHelper.Instance.Serialize(dbtypes)},连接字符串:{connectionString}");
|
2021-01-27 13:42:38 +08:00
|
|
|
|
services.AddDbContext<OpenAuthDBContext>();
|
2020-10-22 14:59:36 +08:00
|
|
|
|
|
|
|
|
|
services.AddHttpClient();
|
|
|
|
|
|
|
|
|
|
services.AddDataProtection().PersistKeysToFileSystem(new DirectoryInfo(Configuration["DataProtection"]));
|
2023-09-01 01:44:25 +08:00
|
|
|
|
|
|
|
|
|
var sqlsugarTypes = UtilMethods.EnumToDictionary<SqlSugar.DbType>();
|
|
|
|
|
var dbType = sqlsugarTypes.FirstOrDefault(it =>
|
|
|
|
|
dbtypes.ToDictionary(u => u.Key, v => v.Value.ToLower()).ContainsValue(it.Key));
|
|
|
|
|
|
2023-08-26 23:52:30 +08:00
|
|
|
|
services.AddScoped<ISqlSugarClient>(s =>
|
|
|
|
|
{
|
2023-09-01 01:44:25 +08:00
|
|
|
|
var sqlSugar = new SqlSugarClient(new ConnectionConfig()
|
2023-08-26 23:52:30 +08:00
|
|
|
|
{
|
2023-09-01 01:44:25 +08:00
|
|
|
|
DbType = dbType.Value,
|
|
|
|
|
ConnectionString = connectionString,
|
2025-02-23 21:11:42 +08:00
|
|
|
|
IsAutoCloseConnection = true
|
2023-09-01 01:44:25 +08:00
|
|
|
|
}, db => { db.Aop.OnLogExecuting = (sql, pars) => { logger.LogInformation(sql); }; });
|
2025-02-23 21:11:42 +08:00
|
|
|
|
|
|
|
|
|
if(dbType.Value != SqlSugar.DbType.PostgreSQL){
|
|
|
|
|
return sqlSugar;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 配置bool类型转换为smallint
|
|
|
|
|
sqlSugar.Aop.OnExecutingChangeSql = (sql, parameters) =>
|
|
|
|
|
{
|
|
|
|
|
foreach (var param in parameters)
|
|
|
|
|
{
|
|
|
|
|
if (param.Value is bool boolValue)
|
|
|
|
|
{
|
|
|
|
|
param.DbType = System.Data.DbType.Int16;
|
|
|
|
|
// 将 bool 转换为 smallint
|
|
|
|
|
param.Value = boolValue ? (short)1 : (short)0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// 返回修改后的 SQL 和参数
|
|
|
|
|
return new System.Collections.Generic.KeyValuePair<string, SugarParameter[]>(sql, parameters);
|
|
|
|
|
};
|
2023-08-26 23:52:30 +08:00
|
|
|
|
return sqlSugar;
|
|
|
|
|
});
|
2021-05-07 23:38:53 +08:00
|
|
|
|
|
2021-05-25 00:49:12 +08:00
|
|
|
|
|
2020-10-22 14:59:36 +08:00
|
|
|
|
//设置定时启动的任务
|
|
|
|
|
services.AddHostedService<QuartzService>();
|
|
|
|
|
}
|
2021-05-07 23:38:53 +08:00
|
|
|
|
|
2020-10-22 14:59:36 +08:00
|
|
|
|
public void ConfigureContainer(ContainerBuilder builder)
|
|
|
|
|
{
|
|
|
|
|
AutofacExt.InitAutofac(builder);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
2020-12-27 00:00:28 +08:00
|
|
|
|
public void Configure(IApplicationBuilder app, IHostEnvironment env, ILoggerFactory loggerFactory)
|
2020-10-22 14:59:36 +08:00
|
|
|
|
{
|
2020-12-27 00:00:28 +08:00
|
|
|
|
loggerFactory.AddLog4Net();
|
2021-05-07 23:38:53 +08:00
|
|
|
|
|
2020-10-22 14:59:36 +08:00
|
|
|
|
if (env.IsDevelopment())
|
|
|
|
|
{
|
|
|
|
|
app.UseDeveloperExceptionPage();
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-26 00:09:31 +08:00
|
|
|
|
app.UseMiniProfiler();
|
|
|
|
|
|
2020-10-22 14:59:36 +08:00
|
|
|
|
//可以访问根目录下面的静态文件
|
2021-04-06 10:21:05 +08:00
|
|
|
|
var staticfile = new StaticFileOptions
|
|
|
|
|
{
|
|
|
|
|
FileProvider = new PhysicalFileProvider(AppContext.BaseDirectory),
|
|
|
|
|
OnPrepareResponse = (ctx) =>
|
|
|
|
|
{
|
|
|
|
|
//可以在这里为静态文件添加其他http头信息,默认添加跨域信息
|
|
|
|
|
ctx.Context.Response.Headers["Access-Control-Allow-Origin"] = "*";
|
|
|
|
|
}
|
|
|
|
|
};
|
2020-10-22 14:59:36 +08:00
|
|
|
|
app.UseStaticFiles(staticfile);
|
2021-05-07 23:38:53 +08:00
|
|
|
|
|
2020-10-22 14:59:36 +08:00
|
|
|
|
|
|
|
|
|
//todo:测试可以允许任意跨域,正式环境要加权限
|
|
|
|
|
app.UseCors(builder => builder.AllowAnyOrigin()
|
|
|
|
|
.AllowAnyMethod()
|
|
|
|
|
.AllowAnyHeader());
|
2021-05-07 23:38:53 +08:00
|
|
|
|
|
2020-10-22 14:59:36 +08:00
|
|
|
|
app.UseRouting();
|
|
|
|
|
app.UseAuthentication();
|
2021-05-07 23:38:53 +08:00
|
|
|
|
|
2021-05-08 16:29:11 +08:00
|
|
|
|
// 启用日志追踪记录和异常友好提示
|
|
|
|
|
app.UseLogMiddleware();
|
|
|
|
|
|
2021-05-07 23:38:53 +08:00
|
|
|
|
app.UseEndpoints(endpoints => { endpoints.MapControllers(); });
|
|
|
|
|
|
2020-10-22 14:59:36 +08:00
|
|
|
|
//配置ServiceProvider
|
|
|
|
|
AutofacContainerModule.ConfigServiceProvider(app.ApplicationServices);
|
2021-06-11 23:40:55 +08:00
|
|
|
|
|
|
|
|
|
|
2021-05-07 23:38:53 +08:00
|
|
|
|
app.UseSwagger();
|
2020-10-22 14:59:36 +08:00
|
|
|
|
|
|
|
|
|
// Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.),
|
|
|
|
|
// specifying the Swagger JSON endpoint.
|
|
|
|
|
app.UseSwaggerUI(c =>
|
2021-06-11 23:40:55 +08:00
|
|
|
|
{
|
|
|
|
|
c.IndexStream = () =>
|
2023-09-01 01:44:25 +08:00
|
|
|
|
IntrospectionExtensions.GetTypeInfo(GetType()).Assembly
|
|
|
|
|
.GetManifestResourceStream("OpenAuth.WebApi.index.html");
|
2021-06-11 23:40:55 +08:00
|
|
|
|
|
2021-05-07 23:38:53 +08:00
|
|
|
|
foreach (var controller in GetControllers())
|
|
|
|
|
{
|
2021-06-11 23:40:55 +08:00
|
|
|
|
var groupname = GetSwaggerGroupName(controller);
|
|
|
|
|
|
|
|
|
|
c.SwaggerEndpoint($"/swagger/{groupname}/swagger.json", groupname);
|
2021-05-07 23:38:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-11 23:40:55 +08:00
|
|
|
|
c.DocExpansion(DocExpansion.List); //默认展开列表
|
2021-05-07 23:38:53 +08:00
|
|
|
|
c.OAuthClientId("OpenAuth.WebApi"); //oauth客户端名称
|
2020-10-22 14:59:36 +08:00
|
|
|
|
c.OAuthAppName("开源版webapi认证"); // 描述
|
|
|
|
|
});
|
|
|
|
|
}
|
2021-06-11 23:40:55 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 获取控制器对应的swagger分组值
|
|
|
|
|
/// </summary>
|
|
|
|
|
private string GetSwaggerGroupName(Type controller)
|
|
|
|
|
{
|
|
|
|
|
var groupname = controller.Name.Replace("Controller", "");
|
|
|
|
|
var apisetting = controller.GetCustomAttribute(typeof(ApiExplorerSettingsAttribute));
|
|
|
|
|
if (apisetting != null)
|
|
|
|
|
{
|
|
|
|
|
groupname = ((ApiExplorerSettingsAttribute) apisetting).GroupName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return groupname;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 获取所有的控制器
|
|
|
|
|
/// </summary>
|
|
|
|
|
private List<Type> GetControllers()
|
|
|
|
|
{
|
|
|
|
|
Assembly asm = Assembly.GetExecutingAssembly();
|
|
|
|
|
|
|
|
|
|
var controlleractionlist = asm.GetTypes()
|
|
|
|
|
.Where(type => typeof(ControllerBase).IsAssignableFrom(type))
|
|
|
|
|
.OrderBy(x => x.Name).ToList();
|
|
|
|
|
return controlleractionlist;
|
|
|
|
|
}
|
2020-10-22 14:59:36 +08:00
|
|
|
|
}
|
2021-05-07 23:38:53 +08:00
|
|
|
|
}
|