feat:打通simple admin
This commit is contained in:
parent
d32c80ef1e
commit
06a216ab5a
27
src/TrackCenter.Api/JWTEncryption.cs
Normal file
27
src/TrackCenter.Api/JWTEncryption.cs
Normal file
@ -0,0 +1,27 @@
|
||||
using System.Text;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
|
||||
namespace TrackCenter.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/TrackCenter.Api/JWTSettingsOptions.cs
Normal file
39
src/TrackCenter.Api/JWTSettingsOptions.cs
Normal file
@ -0,0 +1,39 @@
|
||||
namespace TrackCenter.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;
|
||||
}
|
||||
@ -14,6 +14,7 @@
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="7.0.2"/>
|
||||
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.17.0"/>
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0"/>
|
||||
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.21" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@ -5,6 +5,8 @@ using Fake.Autofac;
|
||||
using Fake.Modularity;
|
||||
using FluentValidation.AspNetCore;
|
||||
using Hangfire;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc.Authorization;
|
||||
using OpenTelemetry.Resources;
|
||||
using OpenTelemetry.Trace;
|
||||
using TrackCenter.Application;
|
||||
@ -13,6 +15,7 @@ using TrackCenter.Domain.Extensions;
|
||||
using TrackCenter.Repository;
|
||||
using TrackCenter.Repository.Otel;
|
||||
using TrackCenter.Repository.Otel.Options;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace TrackCenter.Api;
|
||||
|
||||
@ -34,7 +37,14 @@ public class TrackCenterApiModule : FakeModule
|
||||
options.ScanApplicationServices<TrackCenterApplicationModule>();
|
||||
});
|
||||
|
||||
services.AddMvc().AddNewtonsoftJson();
|
||||
// 全局授权配置:所有接口默认需要授权
|
||||
services.AddMvc(options =>
|
||||
{
|
||||
var policy = new AuthorizationPolicyBuilder()
|
||||
.RequireAuthenticatedUser()
|
||||
.Build();
|
||||
options.Filters.Add(new AuthorizeFilter(policy));
|
||||
}).AddNewtonsoftJson();
|
||||
|
||||
services.AddFakeExceptionFilter()
|
||||
.AddFakeValidationActionFilter()
|
||||
@ -98,6 +108,20 @@ public class TrackCenterApiModule : FakeModule
|
||||
.AllowCredentials();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
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)
|
||||
|
||||
@ -175,5 +175,17 @@
|
||||
"EnableHttpClient": true,//启用HTTP请求 默认 true
|
||||
"EnableSql": true,//启动SQL 默认 true
|
||||
"EnableSqlParameters": true //启动SQL参数,默认 false
|
||||
},
|
||||
"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
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user