如何解决如何通过 RouteClaimsRequirement 允许多个角色访问路由
在常规类型的场景中,如果有可用的 Route,仅对“高级”用户说,ocelot.global.json
将具有 RouteClaimsRequirement
,如下所示:
"RouteClaimsRequirement" : { "Role" : "Premium" }
这将被转换为 KeyValuePair<string,string>()
,并且效果很好。
但是,如果我要为 2 类用户打开一条路线,例如。 “常规”和“高级”,我究竟如何才能做到这一点?
解决方法
我找到了一种覆盖默认 Ocelot 中间件的方法。以下是一些有用的代码片段:
首先,在 Startup.cs 的 Configuration() 中覆盖默认的 AuthorizationMiddleware:
public void Configure(IApplicationBuilder app,IWebHostEnvironment env)
{
var config = new OcelotPipelineConfiguration
{
AuthorisationMiddleware
= async (downStreamContext,next) =>
await OcelotJwtMiddleware.CreateAuthorizationFilter(downStreamContext,next)
};
app.UseOcelot(config).Wait();
}
如您所见,我在上面使用了一个自定义的 OcelotJwtMiddleware 类。这是那个类,粘贴:
public static class OcelotJwtMiddleware
{
private static readonly string RoleSeparator = ",";
public static Func<DownstreamContext,Func<Task>,Task> CreateAuthorizationFilter
=> async (downStreamContext,next) =>
{
HttpContext httpContext = downStreamContext.HttpContext;
var token = httpContext.Request.Cookies[JwtManager.AuthorizationTokenKey];
if (token != null && AuthorizeIfValidToken(downStreamContext,token))
{
await next.Invoke();
}
else
{
downStreamContext.DownstreamResponse =
new DownstreamResponse(new HttpResponseMessage(HttpStatusCode.Unauthorized));
}
};
private static bool AuthorizeIfValidToken(DownstreamContext downStreamContext,string jwtToken)
{
IIdentityProvider decodedObject = new JwtManager().Decode<UserToken>(jwtToken);
if (decodedObject != null)
{
return downStreamContext.DownstreamReRoute.RouteClaimsRequirement["Role"]
?.Split(RoleSeparator)
.FirstOrDefault(role => role.Trim() == decodedObject.GetRole()) != default;
}
return false;
}
}
这里的 JwtManager 类只是我使用默认 Jwt NuGet 包制作的小实用程序,没什么特别的。此外,JWT 被存储为 Cookie,这是不安全的,但在这里无关紧要。如果您碰巧复制粘贴了您的代码,您会遇到与此相关的小错误,但只需使用您自己的身份验证令牌实现将其切换出来。 实现这两个代码段后,ocelot.global.json 可以有 RouteClaimsRequirement 如下:
"RouteClaimsRequirement" : { "Role" : "Premium,Regular" }
这将识别在其 Cookie 中使用常规的客户以及使用高级版的客户。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。