From 400b14cd75afade00d85215b531d564c0ff127a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A9=99=E5=AD=90?= <454313500@qq.com> Date: Thu, 19 Jan 2023 17:58:46 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E6=8E=88=E6=9D=83=E4=B8=8E?= =?UTF-8?q?=E8=AE=A4=E8=AF=81=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Authorization/DefaultPermissionHandler.cs | 8 +++++- .../Authorization/PermissionAttribute.cs | 2 +- .../YiFrameworkAuthJwtBearerModule.cs | 8 +++--- .../Extensions/ErrorHandExtensions.cs | 24 ++++++++++++++++-- .../Student/StudentService.cs | 6 ++--- .../test/Yi.Framework.Web/TimeTest.txt | Bin 3905 -> 4149 bytes 6 files changed, 37 insertions(+), 11 deletions(-) diff --git a/Yi.Framework.Net6/src/framework/Yi.Framework.Auth.JwtBearer/Authorization/DefaultPermissionHandler.cs b/Yi.Framework.Net6/src/framework/Yi.Framework.Auth.JwtBearer/Authorization/DefaultPermissionHandler.cs index 5696c25f..c31d6607 100644 --- a/Yi.Framework.Net6/src/framework/Yi.Framework.Auth.JwtBearer/Authorization/DefaultPermissionHandler.cs +++ b/Yi.Framework.Net6/src/framework/Yi.Framework.Auth.JwtBearer/Authorization/DefaultPermissionHandler.cs @@ -11,7 +11,13 @@ namespace Yi.Framework.Auth.JwtBearer.Authorization { public bool IsPass(string permission, ICurrentUser currentUser) { - return true; + if (currentUser.Permission is not null) + { + return currentUser.Permission.Contains(permission); + + } + + return false; } } } diff --git a/Yi.Framework.Net6/src/framework/Yi.Framework.Auth.JwtBearer/Authorization/PermissionAttribute.cs b/Yi.Framework.Net6/src/framework/Yi.Framework.Auth.JwtBearer/Authorization/PermissionAttribute.cs index cb77203f..88d5b4b4 100644 --- a/Yi.Framework.Net6/src/framework/Yi.Framework.Auth.JwtBearer/Authorization/PermissionAttribute.cs +++ b/Yi.Framework.Net6/src/framework/Yi.Framework.Auth.JwtBearer/Authorization/PermissionAttribute.cs @@ -39,7 +39,7 @@ namespace Yi.Framework.Auth.JwtBearer.Authorization if (!result) { - throw new AuthException(message: "您无权限访问该接口"); + throw new AuthException(message: $"您无权限访问该接口-{ context.HttpContext.Request.Path.Value}"); } } diff --git a/Yi.Framework.Net6/src/framework/Yi.Framework.Auth.JwtBearer/YiFrameworkAuthJwtBearerModule.cs b/Yi.Framework.Net6/src/framework/Yi.Framework.Auth.JwtBearer/YiFrameworkAuthJwtBearerModule.cs index dd8608b6..7016597f 100644 --- a/Yi.Framework.Net6/src/framework/Yi.Framework.Auth.JwtBearer/YiFrameworkAuthJwtBearerModule.cs +++ b/Yi.Framework.Net6/src/framework/Yi.Framework.Auth.JwtBearer/YiFrameworkAuthJwtBearerModule.cs @@ -32,10 +32,10 @@ namespace Yi.Framework.Auth.JwtBearer { option.AddScheme(YiJwtAuthenticationHandler.YiJwtSchemeName, YiJwtAuthenticationHandler.YiJwtSchemeName); }); - services.AddSingleton(_=>new PermissionAttribute(string.Empty)); - services.AddControllers(options => { - options.Filters.Add(); - }); + services.AddSingleton(); + //services.AddControllers(options => { + // options.Filters.Add(); + //}); } } } diff --git a/Yi.Framework.Net6/src/framework/Yi.Framework.Core/Extensions/ErrorHandExtensions.cs b/Yi.Framework.Net6/src/framework/Yi.Framework.Core/Extensions/ErrorHandExtensions.cs index ede66151..6421c847 100644 --- a/Yi.Framework.Net6/src/framework/Yi.Framework.Core/Extensions/ErrorHandExtensions.cs +++ b/Yi.Framework.Net6/src/framework/Yi.Framework.Core/Extensions/ErrorHandExtensions.cs @@ -40,8 +40,8 @@ namespace Yi.Framework.Core.Extensions var result = new ExceptionModle { - Message= businessEx.Message, - Details= businessEx.Details, + Message = businessEx.Message, + Details = businessEx.Details, }; //业务错误,不记录日志 await context.Response.WriteAsync(JsonConvert.SerializeObject(result, new JsonSerializerSettings() @@ -50,6 +50,26 @@ namespace Yi.Framework.Core.Extensions ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver() })); + } + catch (AuthException ex) + { + context.Response.ContentType = "application/json;charset=utf-8"; + //系统错误,记录日志 + _logger.LogError(ex, $"授权失败:{ex.Message}"); + //await _errorHandle.Invoer(context, ex); + context.Response.StatusCode =(int)ex.Code; + //系统错误,需要记录 + var result = new ExceptionModle + { + Message = ex.Message, + Details = "授权失败", + }; + await context.Response.WriteAsync(JsonConvert.SerializeObject(result, new JsonSerializerSettings() + { + //设置首字母小写 + ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver() + })); + } catch (Exception ex) { 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 4a3c8d88..cd408c39 100644 --- a/Yi.Framework.Net6/test/Yi.Framework.Application/Student/StudentService.cs +++ b/Yi.Framework.Net6/test/Yi.Framework.Application/Student/StudentService.cs @@ -43,14 +43,14 @@ namespace Yi.Framework.Application.Student _studentManager = studentManager; _unitOfWorkManager = unitOfWorkManager; _jwtTokenManager = jwtTokenManager; - _currentUser=currentUser; + _currentUser = currentUser; } /// /// 测试token /// /// - public string GetToken() + public string GetToken() { var claimDic = new Dictionary() { { TokenTypeConst.Id, "123" }, { TokenTypeConst.UserName, "cc" } }; return _jwtTokenManager.CreateToken(claimDic); @@ -64,7 +64,7 @@ namespace Yi.Framework.Application.Student [Permission(AuthStudentConst.查询)] public async Task PostUow() { - var o= _currentUser; + var o = _currentUser; StudentGetOutputDto res = new(); using (var uow = _unitOfWorkManager.CreateContext()) { diff --git a/Yi.Framework.Net6/test/Yi.Framework.Web/TimeTest.txt b/Yi.Framework.Net6/test/Yi.Framework.Web/TimeTest.txt index 7352107d7a3452b72cb8445d2f0c54b6b2ac7ebe..1a073dde49553c8387218ea524674fb6c10114bb 100644 GIT binary patch delta 89 zcmX>ow^d<-Bmd-1P9Ao1D-&}oW0T2^jPgvTmXkMfi2{X8jjT*8Cx2vDU^JQBz$wZC m6alK>R%9`>Ff*Lo$SDF+1Jq^?Qp06p`fUB`=gTMYasdE8c^OCm delta 7 Ocmdn0a8Pc8BR>EPm;$u`