增加安全验证测试

This commit is contained in:
falcon 2022-11-29 15:42:38 +08:00
parent 7157dac6bf
commit ca1209c310
2 changed files with 17 additions and 3 deletions

View File

@ -18,7 +18,21 @@ namespace Falcon.SugarApi.JWT
/// <summary> /// <summary>
/// 用户需要具有的角色 /// 用户需要具有的角色
/// </summary> /// </summary>
public List<string> Roles { get; set; } = new List<string>(); public string[] Roles { get; set; }
/// <summary>
/// 只需要登录即可通过验证
/// </summary>
public ApiAuthorizationAttribute() {
this.Roles = new string[] { };
}
/// <summary>
/// 需要登录并且具有一定的角色
/// </summary>
/// <param name="roles">角色组</param>
public ApiAuthorizationAttribute(params string[] roles) {
this.Roles = roles;
}
/// <inheritdoc/> /// <inheritdoc/>
public void OnAuthorization(AuthorizationFilterContext context) { public void OnAuthorization(AuthorizationFilterContext context) {
@ -41,7 +55,7 @@ namespace Falcon.SugarApi.JWT
Unauthorized(context); Unauthorized(context);
return; return;
} }
if (this.Roles != null && this.Roles.Count > 0 && !userLogin.UserInRoles(user, this.Roles)) { if (this.Roles != null && this.Roles.Length > 0 && !userLogin.UserInRoles(user, this.Roles)) {
Unauthorized(context); Unauthorized(context);
return; return;
} }

View File

@ -34,6 +34,6 @@ namespace Falcon.SugarApi.JWT
/// <param name="userInfo">用户信息</param> /// <param name="userInfo">用户信息</param>
/// <param name="roles">需要具有的角色组</param> /// <param name="roles">需要具有的角色组</param>
/// <returns>True具有,False不具有</returns> /// <returns>True具有,False不具有</returns>
bool UserInRoles(LoginUserInfo userInfo, List<string> roles); bool UserInRoles(LoginUserInfo userInfo, params string[] roles);
} }
} }