diff --git a/ZR.Admin.WebApi/Extensions/CorsExtension.cs b/ZR.Admin.WebApi/Extensions/CorsExtension.cs new file mode 100644 index 0000000..1240d80 --- /dev/null +++ b/ZR.Admin.WebApi/Extensions/CorsExtension.cs @@ -0,0 +1,27 @@ +namespace ZR.Admin.WebApi.Extensions +{ + public static class CorsExtension + { + /// + /// 跨域配置 + /// + /// + /// + public static void AddCors(this IServiceCollection services, IConfiguration configuration) + { + var corsUrls = configuration["corsUrls"]?.Split(',', StringSplitOptions.RemoveEmptyEntries); + + //配置跨域 + services.AddCors(c => + { + c.AddPolicy("Policy", policy => + { + policy.WithOrigins(corsUrls ?? Array.Empty()) + .AllowAnyHeader()//允许任意头 + .AllowCredentials()//允许cookie + .AllowAnyMethod();//允许任意方法 + }); + }); + } + } +} diff --git a/ZR.Admin.WebApi/Program.cs b/ZR.Admin.WebApi/Program.cs index e984fb4..f6a9cad 100644 --- a/ZR.Admin.WebApi/Program.cs +++ b/ZR.Admin.WebApi/Program.cs @@ -9,6 +9,7 @@ using ZR.Admin.WebApi.Middleware; using ZR.Admin.WebApi.Hubs; using ZR.Common.Cache; using AspNetCoreRateLimit; +using Microsoft.IdentityModel.Tokens; var builder = WebApplication.CreateBuilder(args); @@ -20,19 +21,8 @@ builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); //注入HttpContextAccessor builder.Services.AddSingleton(); -var corsUrls = builder.Configuration["corsUrls"]?.Split(',', StringSplitOptions.RemoveEmptyEntries); - -//配置跨域 -builder.Services.AddCors(c => -{ - c.AddPolicy("Policy", policy => - { - policy.WithOrigins(corsUrls ?? Array.Empty()) - .AllowAnyHeader()//允许任意头 - .AllowCredentials()//允许cookie - .AllowAnyMethod();//允许任意方法 - }); -}); +// 跨域配置 +builder.Services.AddCors(builder.Configuration); // 显示logo builder.Services.AddLogo(); //注入SignalR实时通讯,默认用json传输 @@ -58,6 +48,19 @@ builder.Services.AddAuthentication(options => .AddJwtBearer(o => { o.TokenValidationParameters = JwtUtil.ValidParameters(); + o.Events = new JwtBearerEvents + { + OnAuthenticationFailed = context => + { + // 如果过期,把过期信息添加到头部 + if (context.Exception.GetType() == typeof(SecurityTokenExpiredException)) + { + context.Response.Headers.Add("Token-Expired", "true"); + } + + return Task.CompletedTask; + } + }; }); //InternalApp.InternalServices = builder.Services; @@ -66,7 +69,7 @@ builder.Services.AddAppService(); //开启计划任务 builder.Services.AddTaskSchedulers(); //初始化db -DbExtension.AddDb(builder.Configuration); +builder.Services.AddDb(builder.Configuration); //注册REDIS 服务 var openRedis = builder.Configuration["RedisServer:open"];