From fc74a000a6a24cef93b463534e51858448c0de18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A9=99=E5=AD=90?= <454313500@qq.com> Date: Wed, 18 Jan 2023 19:42:13 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0jwt=E8=AE=A4=E8=AF=81?= =?UTF-8?q?=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Yi.Framework.Net6/Yi.Framework.Data/Class1.cs | 7 ++ .../Yi.Framework.Data/DataFilter.cs | 106 +++++++++++++++++ .../Yi.Framework.Data/IDataFilter.cs | 31 +++++ .../Yi.Framework.Data.csproj | 9 ++ Yi.Framework.Net6/Yi.Framework.sln | 18 ++- ....Framework.Authentication.JwtBearer.csproj | 13 ++ .../YiJwtAuthenticationHandler.cs | 111 ++++++++++++++++++ .../Student/StudentService.cs | 2 + .../test/Yi.Framework.Web/Program.cs | 10 ++ .../test/Yi.Framework.Web/TimeTest.txt | 3 + .../Yi.Framework.Web/Yi.Framework.Web.csproj | 1 + .../test/Yi.Framework.Web/yi-sqlsugar-dev.db | Bin 12288 -> 12288 bytes 12 files changed, 309 insertions(+), 2 deletions(-) create mode 100644 Yi.Framework.Net6/Yi.Framework.Data/Class1.cs create mode 100644 Yi.Framework.Net6/Yi.Framework.Data/DataFilter.cs create mode 100644 Yi.Framework.Net6/Yi.Framework.Data/IDataFilter.cs create mode 100644 Yi.Framework.Net6/Yi.Framework.Data/Yi.Framework.Data.csproj create mode 100644 Yi.Framework.Net6/src/framework/Yi.Framework.Authentication.JwtBearer/Yi.Framework.Authentication.JwtBearer.csproj create mode 100644 Yi.Framework.Net6/src/framework/Yi.Framework.Authentication.JwtBearer/YiJwtAuthenticationHandler.cs diff --git a/Yi.Framework.Net6/Yi.Framework.Data/Class1.cs b/Yi.Framework.Net6/Yi.Framework.Data/Class1.cs new file mode 100644 index 00000000..dc095265 --- /dev/null +++ b/Yi.Framework.Net6/Yi.Framework.Data/Class1.cs @@ -0,0 +1,7 @@ +namespace Yi.Framework.Data +{ + public class Class1 + { + + } +} \ No newline at end of file diff --git a/Yi.Framework.Net6/Yi.Framework.Data/DataFilter.cs b/Yi.Framework.Net6/Yi.Framework.Data/DataFilter.cs new file mode 100644 index 00000000..bc4aaf46 --- /dev/null +++ b/Yi.Framework.Net6/Yi.Framework.Data/DataFilter.cs @@ -0,0 +1,106 @@ +//using System; +//using System.Collections.Concurrent; +//using System.Collections.Generic; +//using System.Linq; +//using System.Text; +//using System.Threading.Tasks; + +//namespace Yi.Framework.Data +//{ +// public class DataFilter : IDataFilter +// { +// private readonly ConcurrentDictionary _filters; + +// private readonly IServiceProvider _serviceProvider; + +// public DataFilter(IServiceProvider serviceProvider) +// { +// _serviceProvider = serviceProvider; +// _filters = new ConcurrentDictionary(); +// } + +// public IDisposable Enable() +// where TFilter : class +// { +// return GetFilter().Enable(); +// } + +// public IDisposable Disable() +// where TFilter : class +// { +// return GetFilter().Disable(); +// } + +// public bool IsEnabled() +// where TFilter : class +// { +// return GetFilter().IsEnabled; +// } + +// private IDataFilter GetFilter() +// where TFilter : class +// { +// return _filters.GetOrAdd( +// typeof(TFilter), +// valueFactory: (k) => _serviceProvider.GetRequiredService>() +// ) as IDataFilter; +// } +// } + +// public class DataFilter : IDataFilter +// where TFilter : class +// { +// public bool IsEnabled +// { +// get +// { +// EnsureInitialized(); +// return _filter.Value.IsEnabled; +// } +// } + +// private readonly AbpDataFilterOptions _options; + +// private readonly AsyncLocal _filter; + +// public DataFilter(IOptions options) +// { +// _options = options.Value; +// _filter = new AsyncLocal(); +// } + +// public IDisposable Enable() +// { +// if (IsEnabled) +// { +// return NullDisposable.Instance; +// } + +// _filter.Value.IsEnabled = true; + +// return new DisposeAction(() => Disable()); +// } + +// public IDisposable Disable() +// { +// if (!IsEnabled) +// { +// return NullDisposable.Instance; +// } + +// _filter.Value.IsEnabled = false; + +// return new DisposeAction(() => Enable()); +// } + +// private void EnsureInitialized() +// { +// if (_filter.Value != null) +// { +// return; +// } + +// _filter.Value = _options.DefaultStates.GetOrDefault(typeof(TFilter))?.Clone() ?? new DataFilterState(true); +// } +// } +//} diff --git a/Yi.Framework.Net6/Yi.Framework.Data/IDataFilter.cs b/Yi.Framework.Net6/Yi.Framework.Data/IDataFilter.cs new file mode 100644 index 00000000..61a4f92a --- /dev/null +++ b/Yi.Framework.Net6/Yi.Framework.Data/IDataFilter.cs @@ -0,0 +1,31 @@ +//using System; +//using System.Collections.Generic; +//using System.Linq; +//using System.Text; +//using System.Threading.Tasks; + +//namespace Yi.Framework.Data +//{ +// public interface IDataFilter +// where TFilter : class +// { +// IDisposable Enable(); + +// IDisposable Disable(); + +// bool IsEnabled { get; } +// } + +// public interface IDataFilter +// { +// IDisposable Enable() +// where TFilter : class; + +// IDisposable Disable() +// where TFilter : class; + +// bool IsEnabled() +// where TFilter : class; +// } + +//} diff --git a/Yi.Framework.Net6/Yi.Framework.Data/Yi.Framework.Data.csproj b/Yi.Framework.Net6/Yi.Framework.Data/Yi.Framework.Data.csproj new file mode 100644 index 00000000..132c02c5 --- /dev/null +++ b/Yi.Framework.Net6/Yi.Framework.Data/Yi.Framework.Data.csproj @@ -0,0 +1,9 @@ + + + + net6.0 + enable + enable + + + diff --git a/Yi.Framework.Net6/Yi.Framework.sln b/Yi.Framework.Net6/Yi.Framework.sln index 8e36159b..a6020569 100644 --- a/Yi.Framework.Net6/Yi.Framework.sln +++ b/Yi.Framework.Net6/Yi.Framework.sln @@ -43,9 +43,13 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Yi.Framework.Core.Sqlsugar" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Yi.Framework.Core.AutoMapper", "src\framework\Yi.Framework.Core.AutoMapper\Yi.Framework.Core.AutoMapper.csproj", "{DFD34702-2EF6-4ECC-AE6E-9A1A3885BD26}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Yi.Framework.Office.Excel", "src\module\Yi.Framework.Office.Excel\Yi.Framework.Office.Excel.csproj", "{4EEC6607-F0D8-4277-9463-104DA7E184B6}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Yi.Framework.Office.Excel", "src\module\Yi.Framework.Office.Excel\Yi.Framework.Office.Excel.csproj", "{4EEC6607-F0D8-4277-9463-104DA7E184B6}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Yi.Framework.WeChatPay", "src\module\Yi.Framework.WeChatPay\Yi.Framework.WeChatPay.csproj", "{588D0DA9-303A-4FF0-A2D1-83037E2B269F}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Yi.Framework.WeChatPay", "src\module\Yi.Framework.WeChatPay\Yi.Framework.WeChatPay.csproj", "{588D0DA9-303A-4FF0-A2D1-83037E2B269F}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Yi.Framework.Data", "Yi.Framework.Data\Yi.Framework.Data.csproj", "{DEE07142-32CE-4B5F-A5A3-452064EBF4A2}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Yi.Framework.Authentication.JwtBearer", "src\framework\Yi.Framework.Authentication.JwtBearer\Yi.Framework.Authentication.JwtBearer.csproj", "{D40C583D-58BE-422D-9A57-94DECB751EB4}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -129,6 +133,14 @@ Global {588D0DA9-303A-4FF0-A2D1-83037E2B269F}.Debug|Any CPU.Build.0 = Debug|Any CPU {588D0DA9-303A-4FF0-A2D1-83037E2B269F}.Release|Any CPU.ActiveCfg = Release|Any CPU {588D0DA9-303A-4FF0-A2D1-83037E2B269F}.Release|Any CPU.Build.0 = Release|Any CPU + {DEE07142-32CE-4B5F-A5A3-452064EBF4A2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {DEE07142-32CE-4B5F-A5A3-452064EBF4A2}.Debug|Any CPU.Build.0 = Debug|Any CPU + {DEE07142-32CE-4B5F-A5A3-452064EBF4A2}.Release|Any CPU.ActiveCfg = Release|Any CPU + {DEE07142-32CE-4B5F-A5A3-452064EBF4A2}.Release|Any CPU.Build.0 = Release|Any CPU + {D40C583D-58BE-422D-9A57-94DECB751EB4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D40C583D-58BE-422D-9A57-94DECB751EB4}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D40C583D-58BE-422D-9A57-94DECB751EB4}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D40C583D-58BE-422D-9A57-94DECB751EB4}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -153,6 +165,8 @@ Global {DFD34702-2EF6-4ECC-AE6E-9A1A3885BD26} = {5F2B846D-96CE-400A-878E-220498F4EE31} {4EEC6607-F0D8-4277-9463-104DA7E184B6} = {EEF5F221-0E32-4A3D-B647-B4B5E7305806} {588D0DA9-303A-4FF0-A2D1-83037E2B269F} = {EEF5F221-0E32-4A3D-B647-B4B5E7305806} + {DEE07142-32CE-4B5F-A5A3-452064EBF4A2} = {5F2B846D-96CE-400A-878E-220498F4EE31} + {D40C583D-58BE-422D-9A57-94DECB751EB4} = {5F2B846D-96CE-400A-878E-220498F4EE31} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {6C1A3808-0F4F-43FB-A9FE-5F27A3BB2ECF} diff --git a/Yi.Framework.Net6/src/framework/Yi.Framework.Authentication.JwtBearer/Yi.Framework.Authentication.JwtBearer.csproj b/Yi.Framework.Net6/src/framework/Yi.Framework.Authentication.JwtBearer/Yi.Framework.Authentication.JwtBearer.csproj new file mode 100644 index 00000000..7c552323 --- /dev/null +++ b/Yi.Framework.Net6/src/framework/Yi.Framework.Authentication.JwtBearer/Yi.Framework.Authentication.JwtBearer.csproj @@ -0,0 +1,13 @@ + + + + net6.0 + enable + enable + + + + + + + diff --git a/Yi.Framework.Net6/src/framework/Yi.Framework.Authentication.JwtBearer/YiJwtAuthenticationHandler.cs b/Yi.Framework.Net6/src/framework/Yi.Framework.Authentication.JwtBearer/YiJwtAuthenticationHandler.cs new file mode 100644 index 00000000..7a0f5135 --- /dev/null +++ b/Yi.Framework.Net6/src/framework/Yi.Framework.Authentication.JwtBearer/YiJwtAuthenticationHandler.cs @@ -0,0 +1,111 @@ +using Microsoft.AspNetCore.Authentication; +using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.Primitives; +using System.Net; +using System.Security.Claims; + +namespace Yi.Framework.Authentication.JwtBearer +{ + public class YiJwtAuthenticationHandler : IAuthenticationHandler + { + public YiJwtAuthenticationHandler() + { + + } + public const string YiJwtSchemeName = "YiJwtAuth"; + + private AuthenticationScheme _scheme; + private HttpContext _context; + /// + /// 初始化数据 + /// + /// + /// + /// + public Task InitializeAsync(AuthenticationScheme scheme, HttpContext context) + { + _scheme = scheme; + _context = context; + return Task.CompletedTask; + } + + + /// + /// 生成认证票据 + /// + /// + /// + /// + private AuthenticationTicket GetAuthTicket(string name, string role) + { + var claimsIdentity = new ClaimsIdentity(new Claim[] + { + new Claim(ClaimTypes.Name, name), + new Claim(ClaimTypes.Role, role), + }, YiJwtSchemeName); + var principal = new ClaimsPrincipal(claimsIdentity); + return new AuthenticationTicket(principal, _scheme.Name); + } + + /// + /// 处理操作 + /// + /// + /// + public Task AuthenticateAsync() + { + AuthenticateResult result; + _context.Request.Headers.TryGetValue("Authorization", out StringValues values); + string valStr = values.ToString(); + if (!string.IsNullOrWhiteSpace(valStr)) + { + //认证模拟basic认证:cusAuth YWRtaW46YWRtaW4= + string[] authVal = System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(valStr.Substring(YiJwtSchemeName.Length + 1))).Split(':'); + //var loginInfo = new Dto.LoginDto() { Username = authVal[0], Password = authVal[1] }; + //var validVale = _userService.IsValid(loginInfo); + bool validVale = true; + if (!validVale) + result = AuthenticateResult.Fail("未登陆"); + else + { + //这里应该将token进行效验,然后加入解析的claim中即可 + + var ticket = GetAuthTicket("cc", "admin"); + result = AuthenticateResult.Success(ticket); + } + } + else + { + result = AuthenticateResult.Fail("未登陆"); + } + return Task.FromResult(result); + } + + /// + /// 未登录时的处理 + /// + /// + /// + /// + public Task ChallengeAsync(AuthenticationProperties? properties) + { + _context.Request.Headers.TryGetValue("Authorization", out StringValues values); + _context.Response.StatusCode = (int)HttpStatusCode.Unauthorized; + return Task.CompletedTask; + } + + /// + /// 权限不足的处理 + /// + /// + /// + /// + public Task ForbidAsync(AuthenticationProperties? properties) + { + _context.Response.StatusCode = (int)HttpStatusCode.Forbidden; + return Task.CompletedTask; + } + + + } +} \ No newline at end of file diff --git a/Yi.Framework.Net6/test/Yi.Framework.Application/Student/StudentService.cs b/Yi.Framework.Net6/test/Yi.Framework.Application/Student/StudentService.cs index 7d97e51a..15f1354b 100644 --- a/Yi.Framework.Net6/test/Yi.Framework.Application/Student/StudentService.cs +++ b/Yi.Framework.Net6/test/Yi.Framework.Application/Student/StudentService.cs @@ -15,6 +15,7 @@ using Yi.Framework.Domain.Student.Entities; using Yi.Framework.Ddd.Services; using Yi.Framework.Core.Attributes; using Yi.Framework.Uow; +using Microsoft.AspNetCore.Authorization; namespace Yi.Framework.Application.Student { @@ -41,6 +42,7 @@ namespace Yi.Framework.Application.Student /// Uow /// /// + [Authorize] public async Task PostUow() { StudentGetOutputDto res = new(); diff --git a/Yi.Framework.Net6/test/Yi.Framework.Web/Program.cs b/Yi.Framework.Net6/test/Yi.Framework.Web/Program.cs index 4ff8c565..e5b27fbd 100644 --- a/Yi.Framework.Net6/test/Yi.Framework.Web/Program.cs +++ b/Yi.Framework.Net6/test/Yi.Framework.Web/Program.cs @@ -1,4 +1,5 @@ using AspNetCore.Microsoft.AspNetCore.Hosting; +using Yi.Framework.Authentication.JwtBearer; using Yi.Framework.Core.Autofac.Extensions; using Yi.Framework.Core.Autofac.Modules; using Yi.Framework.Core.Extensions; @@ -13,6 +14,12 @@ builder.WebHost.UseStartUrlsServer(builder.Configuration); //ģ builder.UseYiModules(typeof(YiFrameworkWebModule)); +builder.Services.AddAuthentication(YiJwtAuthenticationHandler.YiJwtSchemeName); + +builder.Services.AddAuthentication(option => +{ + option.AddScheme(YiJwtAuthenticationHandler.YiJwtSchemeName, YiJwtAuthenticationHandler.YiJwtSchemeName); +}); //autofacģ,Ҫģ builder.Host.ConfigureAutoFacContainer(container => { @@ -25,6 +32,9 @@ var t = app.Services.GetService(); //ȫִмҪ app.UseErrorHandlingServer(); + +app.UseAuthentication(); +app.UseAuthorization(); app.MapControllers(); app.Run(); diff --git a/Yi.Framework.Net6/test/Yi.Framework.Web/TimeTest.txt b/Yi.Framework.Net6/test/Yi.Framework.Web/TimeTest.txt index 0c96cabe..f4cd8375 100644 --- a/Yi.Framework.Net6/test/Yi.Framework.Web/TimeTest.txt +++ b/Yi.Framework.Net6/test/Yi.Framework.Web/TimeTest.txt @@ -33,3 +33,6 @@ 2023:01:17-23:24:07本次运行启动时间为:1836毫秒 2023:01:17-23:29:20本次运行启动时间为:1958毫秒 2023:01:17-23:45:25本次运行启动时间为:2016毫秒 +2023:01:18-19:34:47本次运行启动时间为:2631毫秒 +2023:01:18-19:36:58本次运行启动时间为:2551毫秒 +2023:01:18-19:40:12本次运行启动时间为:1978毫秒 diff --git a/Yi.Framework.Net6/test/Yi.Framework.Web/Yi.Framework.Web.csproj b/Yi.Framework.Net6/test/Yi.Framework.Web/Yi.Framework.Web.csproj index a4a4a1d9..110113fb 100644 --- a/Yi.Framework.Net6/test/Yi.Framework.Web/Yi.Framework.Web.csproj +++ b/Yi.Framework.Net6/test/Yi.Framework.Web/Yi.Framework.Web.csproj @@ -7,6 +7,7 @@ + diff --git a/Yi.Framework.Net6/test/Yi.Framework.Web/yi-sqlsugar-dev.db b/Yi.Framework.Net6/test/Yi.Framework.Web/yi-sqlsugar-dev.db index 13bbae92710adc19c9925af7ffe6bd91555ecbae..0270e62e06fa8146d851d1f8763589ab9f22cb19 100644 GIT binary patch delta 101 zcmZojXh@hK%_uxk#+gxgW5N=CW+6V|$?O8kOnkx{3!n4KN(-^DsWXY?a8$)Q2{629 zXnZzz!_&!2jV;a1*q?7Ukh{XjEW|H9nO#AdiC=uPp~7oEULIyPMwkYn$twDH0FXf& AaR2}S delta 54 zcmZojXh@hK%_uZc#+gxQW5N=CW