update:修改项目名称从Dilon到Ewide
This commit is contained in:
22
Api/Ewide.Web.Core/Dilon.Web.Core.csproj
Normal file
22
Api/Ewide.Web.Core/Dilon.Web.Core.csproj
Normal file
@@ -0,0 +1,22 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net5.0</TargetFramework>
|
||||
<NoWarn>1701;1702;1591</NoWarn>
|
||||
<DocumentationFile>Dilon.Web.Core.xml</DocumentationFile>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Remove="Dilon.Web.Core.xml" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="5.0.5" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Dilon.Application\Dilon.Application.csproj" />
|
||||
<ProjectReference Include="..\Dilon.Database.Migrations\Dilon.Database.Migrations.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
30
Api/Ewide.Web.Core/Dilon.Web.Core.xml
Normal file
30
Api/Ewide.Web.Core/Dilon.Web.Core.xml
Normal file
@@ -0,0 +1,30 @@
|
||||
<?xml version="1.0"?>
|
||||
<doc>
|
||||
<assembly>
|
||||
<name>Dilon.Web.Core</name>
|
||||
</assembly>
|
||||
<members>
|
||||
<member name="M:Dilon.Web.Core.JwtHandler.HandleAsync(Microsoft.AspNetCore.Authorization.AuthorizationHandlerContext)">
|
||||
<summary>
|
||||
重写 Handler 添加自动刷新
|
||||
</summary>
|
||||
<param name="context"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:Dilon.Web.Core.JwtHandler.PipelineAsync(Microsoft.AspNetCore.Authorization.AuthorizationHandlerContext,Microsoft.AspNetCore.Http.DefaultHttpContext)">
|
||||
<summary>
|
||||
授权判断逻辑,授权通过返回 true,否则返回 false
|
||||
</summary>
|
||||
<param name="context"></param>
|
||||
<param name="httpContext"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:Dilon.Web.Core.JwtHandler.CheckAuthorzieAsync(Microsoft.AspNetCore.Http.DefaultHttpContext)">
|
||||
<summary>
|
||||
检查权限
|
||||
</summary>
|
||||
<param name="httpContext"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
</members>
|
||||
</doc>
|
||||
71
Api/Ewide.Web.Core/Handlers/JwtHandler.cs
Normal file
71
Api/Ewide.Web.Core/Handlers/JwtHandler.cs
Normal file
@@ -0,0 +1,71 @@
|
||||
using Dilon.Core;
|
||||
using Dilon.Core.Service;
|
||||
using Furion;
|
||||
using Furion.Authorization;
|
||||
using Furion.DataEncryption;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Ewide.Web.Core
|
||||
{
|
||||
public class JwtHandler : AppAuthorizeHandler
|
||||
{
|
||||
/// <summary>
|
||||
/// 重写 Handler 添加自动刷新
|
||||
/// </summary>
|
||||
/// <param name="context"></param>
|
||||
/// <returns></returns>
|
||||
public override async Task HandleAsync(AuthorizationHandlerContext context)
|
||||
{
|
||||
// 自动刷新Token
|
||||
if (JWTEncryption.AutoRefreshToken(context, context.GetCurrentHttpContext()))
|
||||
{
|
||||
await AuthorizeHandleAsync(context);
|
||||
}
|
||||
else context.Fail(); // 授权失败
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 授权判断逻辑,授权通过返回 true,否则返回 false
|
||||
/// </summary>
|
||||
/// <param name="context"></param>
|
||||
/// <param name="httpContext"></param>
|
||||
/// <returns></returns>
|
||||
public override async Task<bool> PipelineAsync(AuthorizationHandlerContext context, DefaultHttpContext httpContext)
|
||||
{
|
||||
// 此处已经自动验证 Jwt Token的有效性了,无需手动验证
|
||||
return await CheckAuthorzieAsync(httpContext);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 检查权限
|
||||
/// </summary>
|
||||
/// <param name="httpContext"></param>
|
||||
/// <returns></returns>
|
||||
private static async Task<bool> CheckAuthorzieAsync(DefaultHttpContext httpContext)
|
||||
{
|
||||
// 管理员跳过判断
|
||||
var userManager = App.GetService<IUserManager>();
|
||||
if (userManager.SuperAdmin) return true;
|
||||
|
||||
// 路由名称
|
||||
var routeName = httpContext.Request.Path.Value.Substring(1).Replace("/", ":");
|
||||
|
||||
// 默认路由(获取登录用户信息)
|
||||
var defalutRoute = new List<string>()
|
||||
{
|
||||
"getLoginUser"
|
||||
};
|
||||
|
||||
if (defalutRoute.Contains(routeName)) return true;
|
||||
|
||||
// 获取用户权限集合(按钮或API接口)
|
||||
var permissionList = await App.GetService<ISysMenuService>().GetLoginPermissionList(userManager.UserId);
|
||||
|
||||
// 检查授权
|
||||
return permissionList.Contains(routeName);
|
||||
}
|
||||
}
|
||||
}
|
||||
82
Api/Ewide.Web.Core/Startup.cs
Normal file
82
Api/Ewide.Web.Core/Startup.cs
Normal file
@@ -0,0 +1,82 @@
|
||||
using Dilon.Core;
|
||||
using Furion;
|
||||
using Furion.Snowflake;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Serilog;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace Ewide.Web.Core
|
||||
{
|
||||
public class Startup : AppStartup
|
||||
{
|
||||
public void ConfigureServices(IServiceCollection services)
|
||||
{
|
||||
services.AddJwt<JwtHandler>(enableGlobalAuthorize: true);
|
||||
|
||||
services.AddCorsAccessor();
|
||||
|
||||
services.AddRemoteRequest();
|
||||
|
||||
services.AddConfigurableOptions<CacheOptions>();
|
||||
|
||||
services.AddControllersWithViews()
|
||||
.AddMvcFilter<RequestActionFilter>()
|
||||
.AddInjectWithUnifyResult<XnRestfulResultProvider>()
|
||||
// 在管道中增加NewtonsoftJson,防止参数类型严格验证
|
||||
.AddNewtonsoftJson()
|
||||
.AddJsonOptions(options =>
|
||||
{
|
||||
//options.JsonSerializerOptions.DefaultBufferSize = 10_0000;//返回较大数据数据序列化时会截断,原因:默认缓冲区大小(以字节为单位)为16384。
|
||||
options.JsonSerializerOptions.Converters.AddDateFormatString("yyyy-MM-dd HH:mm:ss");
|
||||
//options.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles; // 忽略循环引用 仅.NET 6支持
|
||||
});
|
||||
|
||||
services.AddViewEngine();
|
||||
|
||||
// 设置雪花id的workerId,确保每个实例workerId都应不同
|
||||
var workerId = ushort.Parse(App.Configuration["SnowId:WorkerId"] ?? "1");
|
||||
IDGenerator.SetIdGenerator(new IDGeneratorOptions { WorkerId = workerId });
|
||||
}
|
||||
|
||||
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
||||
{
|
||||
if (env.IsDevelopment())
|
||||
{
|
||||
app.UseDeveloperExceptionPage();
|
||||
}
|
||||
else
|
||||
{
|
||||
app.UseExceptionHandler("/Home/Error");
|
||||
app.UseHsts();
|
||||
}
|
||||
|
||||
// 添加状态码拦截中间件
|
||||
app.UseUnifyResultStatusCodes();
|
||||
|
||||
app.UseHttpsRedirection(); // 强制https
|
||||
app.UseStaticFiles();
|
||||
|
||||
// Serilog请求日志中间件---必须在 UseStaticFiles 和 UseRouting 之间
|
||||
app.UseSerilogRequestLogging();
|
||||
|
||||
app.UseRouting();
|
||||
|
||||
app.UseCorsAccessor();
|
||||
|
||||
app.UseAuthentication();
|
||||
app.UseAuthorization();
|
||||
|
||||
app.UseInject(string.Empty);
|
||||
|
||||
app.UseEndpoints(endpoints =>
|
||||
{
|
||||
endpoints.MapControllerRoute(
|
||||
name: "default",
|
||||
pattern: "{controller=Home}/{action=Index}/{id?}");
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user