diff --git a/src/InterfaceForward.Api/InterfaceForward.Api.csproj b/src/InterfaceForward.Api/InterfaceForward.Api.csproj
index b84b7b3..d81b7d8 100644
--- a/src/InterfaceForward.Api/InterfaceForward.Api.csproj
+++ b/src/InterfaceForward.Api/InterfaceForward.Api.csproj
@@ -11,6 +11,7 @@
+
diff --git a/src/InterfaceForward.Api/InterfaceForwardApiModule.cs b/src/InterfaceForward.Api/InterfaceForwardApiModule.cs
index ff5b99b..9e86e11 100644
--- a/src/InterfaceForward.Api/InterfaceForwardApiModule.cs
+++ b/src/InterfaceForward.Api/InterfaceForwardApiModule.cs
@@ -2,19 +2,23 @@
using Fake.AspNetCore.Auditing;
using Fake.AspNetCore.Mvc.Conventions;
using Fake.AspNetCore.Mvc.Filters;
+using Fake.Authorization;
using Fake.Autofac;
using Fake.Modularity;
using InterfaceForward.Application;
using InterfaceForward.Application.Filters;
using InterfaceForward.Domain.Shared;
using InterfaceForward.Domain.Shared.Helpers;
+using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.HttpOverrides;
using Microsoft.AspNetCore.Mvc;
+using Microsoft.AspNetCore.Mvc.Authorization;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;
namespace InterfaceForward.Api;
+[DependsOn(typeof(FakeAuthorizationModule))]
[DependsOn(typeof(FakeAutofacModule))]
[DependsOn(typeof(InterfaceForwardApplicationModule))]
public class InterfaceForwardApiModule : FakeModule
@@ -33,7 +37,14 @@ public class InterfaceForwardApiModule : FakeModule
options.ScanApplicationServices();
});
- services.AddMvc().AddNewtonsoftJson();
+ // 全局授权配置:所有接口默认需要授权
+ services.AddMvc(options =>
+ {
+ var policy = new AuthorizationPolicyBuilder()
+ .RequireAuthenticatedUser()
+ .Build();
+ options.Filters.Add(new AuthorizeFilter(policy));
+ }).AddNewtonsoftJson();
//Json格式化全局处理
context.Services.Configure(options =>
@@ -93,6 +104,19 @@ public class InterfaceForwardApiModule : FakeModule
options.KnownNetworks.Clear();
options.KnownProxies.Clear();
});
+
+ services.AddAuthentication(options =>
+ {
+ options.DefaultAuthenticateScheme = "Bearer";
+ options.DefaultChallengeScheme = "Bearer";
+ }).AddJwtBearer(options =>
+ {
+ var jwtSettings = configuration.GetSection("JWTSettings").Get();
+ options.TokenValidationParameters = JWTEncryption.CreateTokenValidationParameters(jwtSettings);
+ });
+
+ // 添加授权服务
+ services.AddAuthorization();
}
public override void ConfigureApplication(ApplicationConfigureContext context)
@@ -112,13 +136,14 @@ public class InterfaceForwardApiModule : FakeModule
// Configure the HTTP request pipeline.
app.UseFakeSwagger();
+
+ app.UseCors(DefaultCorsPolicyName);
app.UseRouting();
-
- app.UseCors(DefaultCorsPolicyName);
app.UseAuthentication();
app.UseAuthorization();
+
app.UseForwardedHeaders();//Nginx代理的话获取真实IP
app.MapControllers();
diff --git a/src/InterfaceForward.Api/JWTEncryption.cs b/src/InterfaceForward.Api/JWTEncryption.cs
new file mode 100644
index 0000000..2b44a85
--- /dev/null
+++ b/src/InterfaceForward.Api/JWTEncryption.cs
@@ -0,0 +1,27 @@
+using System.Text;
+using Microsoft.IdentityModel.Tokens;
+
+namespace InterfaceForward.Api;
+
+public class JWTEncryption
+{
+ /// 生成Token验证参数
+ ///
+ ///
+ public static TokenValidationParameters CreateTokenValidationParameters(
+ JWTSettingsOptions jwtSettings)
+ {
+ return new TokenValidationParameters()
+ {
+ ValidateIssuerSigningKey = jwtSettings.ValidateIssuerSigningKey,
+ IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtSettings.IssuerSigningKey)),
+ ValidateIssuer = jwtSettings.ValidateIssuer,
+ ValidIssuer = jwtSettings.ValidIssuer,
+ ValidateAudience = jwtSettings.ValidateAudience,
+ ValidAudience = jwtSettings.ValidAudience,
+ ValidateLifetime = jwtSettings.ValidateLifetime,
+ ClockSkew = TimeSpan.FromSeconds(jwtSettings.ClockSkew),
+ RequireExpirationTime = jwtSettings.RequireExpirationTime
+ };
+ }
+}
\ No newline at end of file
diff --git a/src/InterfaceForward.Api/JWTSettingsOptions.cs b/src/InterfaceForward.Api/JWTSettingsOptions.cs
new file mode 100644
index 0000000..660f536
--- /dev/null
+++ b/src/InterfaceForward.Api/JWTSettingsOptions.cs
@@ -0,0 +1,39 @@
+namespace InterfaceForward.Api;
+
+
+/// Jwt 配置
+public sealed class JWTSettingsOptions
+{
+ /// 验证签发方密钥
+ public bool ValidateIssuerSigningKey { get; set; }
+
+ /// 签发方密钥
+ public string IssuerSigningKey { get; set; }
+
+ /// 验证签发方
+ public bool ValidateIssuer { get; set; }
+
+ /// 签发方
+ public string ValidIssuer { get; set; }
+
+ /// 验证签收方
+ public bool ValidateAudience { get; set; }
+
+ /// 签收方
+ public string ValidAudience { get; set; }
+
+ /// 验证生存期
+ public bool ValidateLifetime { get; set; }
+
+ /// 过期时间容错值,解决服务器端时间不同步问题(秒)
+ public long ClockSkew { get; set; }
+
+ /// 过期时间(分钟)
+ public long ExpiredTime { get; set; }
+
+ /// 加密算法
+ public string Algorithm { get; set; }
+
+ /// 验证过期时间,设置 false 永不过期
+ public bool RequireExpirationTime { get; set; } = true;
+}
diff --git a/src/InterfaceForward.Api/appsettings.json b/src/InterfaceForward.Api/appsettings.json
index 9788cd1..28be5f1 100644
--- a/src/InterfaceForward.Api/appsettings.json
+++ b/src/InterfaceForward.Api/appsettings.json
@@ -67,5 +67,17 @@
"Password": "itd!@#123",
"ShowLogInfo": false,
"IndexPrefix": ""
+ },
+ "JWTSettings": {
+ "ValidateIssuerSigningKey": true, // 是否验证密钥,bool 类型,默认true
+ "IssuerSigningKey": "3c1cbc3f546eda35168c3aa3cb91780fbe703f0996c1d133ea96dc85c70bbc0a", // 密钥,string 类型,必须是复杂密钥,长度大于16
+ "ValidateIssuer": true, // 是否验证签发方,bool 类型,默认true
+ "ValidIssuer": "SimpleAdmin", // 签发方,string 类型
+ "ValidateAudience": true, // 是否验证签收方,bool 类型,默认true
+ "ValidAudience": "SimpleAdmin", // 签收方,string 类型
+ "ValidateLifetime": true, // 是否验证过期时间,bool 类型,默认true,建议true
+ "ExpiredTime": 50000, // 过期时间,long 类型,单位分钟,默认20分钟
+ "ClockSkew": 10, // 过期时间容错值,long 类型,单位秒,默认5秒
+ "Algorithm": "HS256" // 加密算法,string 类型,默认 HS256
}
}
diff --git a/src/InterfaceForward.Application/Services/Parameter/ParameterService.cs b/src/InterfaceForward.Application/Services/Parameter/ParameterService.cs
index 9099d07..73179a0 100644
--- a/src/InterfaceForward.Application/Services/Parameter/ParameterService.cs
+++ b/src/InterfaceForward.Application/Services/Parameter/ParameterService.cs
@@ -11,6 +11,7 @@ using InterfaceForward.Repositories.Interface.Entitys;
using InterfaceForward.Repositories.Interface.Services;
using InterfaceForward.Repositories.Parameter.Services;
using InterfaceForward.Repositories.Parameter.VO;
+using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace InterfaceForward.Application.Services.Parameter;
@@ -45,6 +46,7 @@ public class ParameterService : ApplicationService
/// 只要叶子节点,默认false,返回所有节点
///
[HttpGet("Parameter/GetParameterList")]
+ [Authorize]
public async Task> GetParameterListAsync([Required] bool isInPara,
[Required] int id, bool onlyLeafs = false)
{