feat:鉴权打通simple admin
This commit is contained in:
parent
9f75b8b906
commit
e83d9cf460
@ -11,6 +11,7 @@
|
|||||||
<ProjectReference Include="..\InterfaceForward.Application\InterfaceForward.Application.csproj" />
|
<ProjectReference Include="..\InterfaceForward.Application\InterfaceForward.Application.csproj" />
|
||||||
<ProjectReference Include="..\InterfaceForward.ServiceDefaults\InterfaceForward.ServiceDefaults.csproj"/>
|
<ProjectReference Include="..\InterfaceForward.ServiceDefaults\InterfaceForward.ServiceDefaults.csproj"/>
|
||||||
<PackageReference Include="Fake.Autofac" Version="8.0.0-preview18" />
|
<PackageReference Include="Fake.Autofac" Version="8.0.0-preview18" />
|
||||||
|
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.21" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="8.0.18" />
|
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="8.0.18" />
|
||||||
<PackageReference Include="Serilog.AspNetCore" Version="9.0.0" />
|
<PackageReference Include="Serilog.AspNetCore" Version="9.0.0" />
|
||||||
<PackageReference Include="Serilog.Sinks.Elasticsearch" Version="10.0.0" />
|
<PackageReference Include="Serilog.Sinks.Elasticsearch" Version="10.0.0" />
|
||||||
|
|||||||
@ -2,19 +2,23 @@
|
|||||||
using Fake.AspNetCore.Auditing;
|
using Fake.AspNetCore.Auditing;
|
||||||
using Fake.AspNetCore.Mvc.Conventions;
|
using Fake.AspNetCore.Mvc.Conventions;
|
||||||
using Fake.AspNetCore.Mvc.Filters;
|
using Fake.AspNetCore.Mvc.Filters;
|
||||||
|
using Fake.Authorization;
|
||||||
using Fake.Autofac;
|
using Fake.Autofac;
|
||||||
using Fake.Modularity;
|
using Fake.Modularity;
|
||||||
using InterfaceForward.Application;
|
using InterfaceForward.Application;
|
||||||
using InterfaceForward.Application.Filters;
|
using InterfaceForward.Application.Filters;
|
||||||
using InterfaceForward.Domain.Shared;
|
using InterfaceForward.Domain.Shared;
|
||||||
using InterfaceForward.Domain.Shared.Helpers;
|
using InterfaceForward.Domain.Shared.Helpers;
|
||||||
|
using Microsoft.AspNetCore.Authorization;
|
||||||
using Microsoft.AspNetCore.HttpOverrides;
|
using Microsoft.AspNetCore.HttpOverrides;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.AspNetCore.Mvc.Authorization;
|
||||||
using Microsoft.OpenApi.Models;
|
using Microsoft.OpenApi.Models;
|
||||||
using Swashbuckle.AspNetCore.SwaggerGen;
|
using Swashbuckle.AspNetCore.SwaggerGen;
|
||||||
|
|
||||||
namespace InterfaceForward.Api;
|
namespace InterfaceForward.Api;
|
||||||
|
|
||||||
|
[DependsOn(typeof(FakeAuthorizationModule))]
|
||||||
[DependsOn(typeof(FakeAutofacModule))]
|
[DependsOn(typeof(FakeAutofacModule))]
|
||||||
[DependsOn(typeof(InterfaceForwardApplicationModule))]
|
[DependsOn(typeof(InterfaceForwardApplicationModule))]
|
||||||
public class InterfaceForwardApiModule : FakeModule
|
public class InterfaceForwardApiModule : FakeModule
|
||||||
@ -33,7 +37,14 @@ public class InterfaceForwardApiModule : FakeModule
|
|||||||
options.ScanApplicationServices<InterfaceForwardApplicationModule>();
|
options.ScanApplicationServices<InterfaceForwardApplicationModule>();
|
||||||
});
|
});
|
||||||
|
|
||||||
services.AddMvc().AddNewtonsoftJson();
|
// 全局授权配置:所有接口默认需要授权
|
||||||
|
services.AddMvc(options =>
|
||||||
|
{
|
||||||
|
var policy = new AuthorizationPolicyBuilder()
|
||||||
|
.RequireAuthenticatedUser()
|
||||||
|
.Build();
|
||||||
|
options.Filters.Add(new AuthorizeFilter(policy));
|
||||||
|
}).AddNewtonsoftJson();
|
||||||
|
|
||||||
//Json格式化全局处理
|
//Json格式化全局处理
|
||||||
context.Services.Configure<MvcNewtonsoftJsonOptions>(options =>
|
context.Services.Configure<MvcNewtonsoftJsonOptions>(options =>
|
||||||
@ -93,6 +104,19 @@ public class InterfaceForwardApiModule : FakeModule
|
|||||||
options.KnownNetworks.Clear();
|
options.KnownNetworks.Clear();
|
||||||
options.KnownProxies.Clear();
|
options.KnownProxies.Clear();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
services.AddAuthentication(options =>
|
||||||
|
{
|
||||||
|
options.DefaultAuthenticateScheme = "Bearer";
|
||||||
|
options.DefaultChallengeScheme = "Bearer";
|
||||||
|
}).AddJwtBearer(options =>
|
||||||
|
{
|
||||||
|
var jwtSettings = configuration.GetSection("JWTSettings").Get<JWTSettingsOptions>();
|
||||||
|
options.TokenValidationParameters = JWTEncryption.CreateTokenValidationParameters(jwtSettings);
|
||||||
|
});
|
||||||
|
|
||||||
|
// 添加授权服务
|
||||||
|
services.AddAuthorization();
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void ConfigureApplication(ApplicationConfigureContext context)
|
public override void ConfigureApplication(ApplicationConfigureContext context)
|
||||||
@ -112,13 +136,14 @@ public class InterfaceForwardApiModule : FakeModule
|
|||||||
|
|
||||||
// Configure the HTTP request pipeline.
|
// Configure the HTTP request pipeline.
|
||||||
app.UseFakeSwagger();
|
app.UseFakeSwagger();
|
||||||
|
|
||||||
|
app.UseCors(DefaultCorsPolicyName);
|
||||||
|
|
||||||
app.UseRouting();
|
app.UseRouting();
|
||||||
|
|
||||||
app.UseCors(DefaultCorsPolicyName);
|
|
||||||
|
|
||||||
app.UseAuthentication();
|
app.UseAuthentication();
|
||||||
app.UseAuthorization();
|
app.UseAuthorization();
|
||||||
|
|
||||||
app.UseForwardedHeaders();//Nginx代理的话获取真实IP
|
app.UseForwardedHeaders();//Nginx代理的话获取真实IP
|
||||||
|
|
||||||
app.MapControllers();
|
app.MapControllers();
|
||||||
|
|||||||
27
src/InterfaceForward.Api/JWTEncryption.cs
Normal file
27
src/InterfaceForward.Api/JWTEncryption.cs
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
using System.Text;
|
||||||
|
using Microsoft.IdentityModel.Tokens;
|
||||||
|
|
||||||
|
namespace InterfaceForward.Api;
|
||||||
|
|
||||||
|
public class JWTEncryption
|
||||||
|
{
|
||||||
|
/// <summary>生成Token验证参数</summary>
|
||||||
|
/// <param name="jwtSettings"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
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
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
39
src/InterfaceForward.Api/JWTSettingsOptions.cs
Normal file
39
src/InterfaceForward.Api/JWTSettingsOptions.cs
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
namespace InterfaceForward.Api;
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>Jwt 配置</summary>
|
||||||
|
public sealed class JWTSettingsOptions
|
||||||
|
{
|
||||||
|
/// <summary>验证签发方密钥</summary>
|
||||||
|
public bool ValidateIssuerSigningKey { get; set; }
|
||||||
|
|
||||||
|
/// <summary>签发方密钥</summary>
|
||||||
|
public string IssuerSigningKey { get; set; }
|
||||||
|
|
||||||
|
/// <summary>验证签发方</summary>
|
||||||
|
public bool ValidateIssuer { get; set; }
|
||||||
|
|
||||||
|
/// <summary>签发方</summary>
|
||||||
|
public string ValidIssuer { get; set; }
|
||||||
|
|
||||||
|
/// <summary>验证签收方</summary>
|
||||||
|
public bool ValidateAudience { get; set; }
|
||||||
|
|
||||||
|
/// <summary>签收方</summary>
|
||||||
|
public string ValidAudience { get; set; }
|
||||||
|
|
||||||
|
/// <summary>验证生存期</summary>
|
||||||
|
public bool ValidateLifetime { get; set; }
|
||||||
|
|
||||||
|
/// <summary>过期时间容错值,解决服务器端时间不同步问题(秒)</summary>
|
||||||
|
public long ClockSkew { get; set; }
|
||||||
|
|
||||||
|
/// <summary>过期时间(分钟)</summary>
|
||||||
|
public long ExpiredTime { get; set; }
|
||||||
|
|
||||||
|
/// <summary>加密算法</summary>
|
||||||
|
public string Algorithm { get; set; }
|
||||||
|
|
||||||
|
/// <summary>验证过期时间,设置 false 永不过期</summary>
|
||||||
|
public bool RequireExpirationTime { get; set; } = true;
|
||||||
|
}
|
||||||
@ -67,5 +67,17 @@
|
|||||||
"Password": "itd!@#123",
|
"Password": "itd!@#123",
|
||||||
"ShowLogInfo": false,
|
"ShowLogInfo": false,
|
||||||
"IndexPrefix": ""
|
"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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -11,6 +11,7 @@ using InterfaceForward.Repositories.Interface.Entitys;
|
|||||||
using InterfaceForward.Repositories.Interface.Services;
|
using InterfaceForward.Repositories.Interface.Services;
|
||||||
using InterfaceForward.Repositories.Parameter.Services;
|
using InterfaceForward.Repositories.Parameter.Services;
|
||||||
using InterfaceForward.Repositories.Parameter.VO;
|
using InterfaceForward.Repositories.Parameter.VO;
|
||||||
|
using Microsoft.AspNetCore.Authorization;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
namespace InterfaceForward.Application.Services.Parameter;
|
namespace InterfaceForward.Application.Services.Parameter;
|
||||||
@ -45,6 +46,7 @@ public class ParameterService : ApplicationService
|
|||||||
/// <param name="onlyLeafs">只要叶子节点,默认false,返回所有节点</param>
|
/// <param name="onlyLeafs">只要叶子节点,默认false,返回所有节点</param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
[HttpGet("Parameter/GetParameterList")]
|
[HttpGet("Parameter/GetParameterList")]
|
||||||
|
[Authorize]
|
||||||
public async Task<IEnumerable<InterfaceParameterOutputValueObject>> GetParameterListAsync([Required] bool isInPara,
|
public async Task<IEnumerable<InterfaceParameterOutputValueObject>> GetParameterListAsync([Required] bool isInPara,
|
||||||
[Required] int id, bool onlyLeafs = false)
|
[Required] int id, bool onlyLeafs = false)
|
||||||
{
|
{
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user