完善授权与认证模块

This commit is contained in:
橙子
2023-01-19 17:58:46 +08:00
parent f88655e214
commit 400b14cd75
6 changed files with 37 additions and 11 deletions

View File

@@ -11,7 +11,13 @@ namespace Yi.Framework.Auth.JwtBearer.Authorization
{ {
public bool IsPass(string permission, ICurrentUser currentUser) public bool IsPass(string permission, ICurrentUser currentUser)
{ {
return true; if (currentUser.Permission is not null)
{
return currentUser.Permission.Contains(permission);
}
return false;
} }
} }
} }

View File

@@ -39,7 +39,7 @@ namespace Yi.Framework.Auth.JwtBearer.Authorization
if (!result) if (!result)
{ {
throw new AuthException(message: "您无权限访问该接口"); throw new AuthException(message: $"您无权限访问该接口-{ context.HttpContext.Request.Path.Value}");
} }
} }

View File

@@ -32,10 +32,10 @@ namespace Yi.Framework.Auth.JwtBearer
{ {
option.AddScheme<YiJwtAuthenticationHandler>(YiJwtAuthenticationHandler.YiJwtSchemeName, YiJwtAuthenticationHandler.YiJwtSchemeName); option.AddScheme<YiJwtAuthenticationHandler>(YiJwtAuthenticationHandler.YiJwtSchemeName, YiJwtAuthenticationHandler.YiJwtSchemeName);
}); });
services.AddSingleton<PermissionAttribute>(_=>new PermissionAttribute(string.Empty)); services.AddSingleton<PermissionAttribute>();
services.AddControllers(options => { //services.AddControllers(options => {
options.Filters.Add<PermissionAttribute>(); // options.Filters.Add<PermissionAttribute>();
}); //});
} }
} }
} }

View File

@@ -50,6 +50,26 @@ namespace Yi.Framework.Core.Extensions
ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver() 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) catch (Exception ex)
{ {