diff --git a/Apimanager_backend/Apimanager_backend.csproj b/Apimanager_backend/Apimanager_backend.csproj
index 80575c2..059696b 100644
--- a/Apimanager_backend/Apimanager_backend.csproj
+++ b/Apimanager_backend/Apimanager_backend.csproj
@@ -13,7 +13,12 @@
all
runtime; build; native; contentfiles; analyzers; buildtransitive
+
+
+
+
+
diff --git a/Apimanager_backend/Config/ServiceCollectionExtensions.cs b/Apimanager_backend/Config/ServiceCollectionExtensions.cs
index 4864289..8d90732 100644
--- a/Apimanager_backend/Config/ServiceCollectionExtensions.cs
+++ b/Apimanager_backend/Config/ServiceCollectionExtensions.cs
@@ -1,6 +1,8 @@
-using Apimanager_backend.Services;
+using Apimanager_backend.Dtos;
+using Apimanager_backend.Services;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.IdentityModel.Tokens;
+using Newtonsoft.Json;
using StackExchange.Redis;
using System.ComponentModel;
using System.Runtime.CompilerServices;
@@ -18,6 +20,7 @@ namespace Apimanager_backend.Config
services.AddScoped();
services.AddSingleton();
services.AddSingleton();
+ services.AddSingleton();
return services;
}
public static IServiceCollection AddJWTService(this IServiceCollection services,IConfiguration configuration)
@@ -28,6 +31,7 @@ namespace Apimanager_backend.Config
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
+ //jwt参数
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
@@ -38,11 +42,45 @@ namespace Apimanager_backend.Config
ValidAudience = jwtSettings["Audience"],
IssuerSigningKey = new SymmetricSecurityKey(key)
};
+ //添加自定义响应处理函数
+ options.Events = new JwtBearerEvents
+ {
+ OnChallenge = new Func(JwtTokenErrorEventFunc),
+ OnForbidden = new Func(JwtPermissionEventFunc)
+ };
});
//redis配置
services.AddSingleton(ConnectionMultiplexer.Connect(configuration["Redis:ConnectionString"]));
return services;
}
+ ///
+ /// token无效事件处理函数
+ ///
+ ///
+ ///
+ public async static Task JwtTokenErrorEventFunc(JwtBearerChallengeContext context)
+ {
+ context.Response.ContentType = "application/json";
+ var res = new ResponseBase