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 7352107d..1a073dde 100644 Binary files a/Yi.Framework.Net6/test/Yi.Framework.Web/TimeTest.txt and b/Yi.Framework.Net6/test/Yi.Framework.Web/TimeTest.txt differ