我有一个使用 Ocelot
在 .NET Core 3.1
中运行的 API 网关。一切都按预期正常工作。
现在我正在尝试在中间件过程中替换下游主机。
这是我的configuration.json:
{
"Routes": [
{
"DownstreamPathTemplate": "/api/product/getProduct",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": "44300"
}
],
"UpstreamPathTemplate": "/getProduct"
}
],
"GlobalConfiguration": {
"BaseUrl": "https://localhost:62012/"
}
}
我想在运行时更改 DownstreamHostAndPorts
中的 Host
,因为此时从 jwt
中获取声明我会知道因此,用户所属的租户知道将请求路由到哪里。
更清楚地说,对网关的请求来自 http://localhost:62012/api/getProduct,
然后,我从发出此调用的请求中的 jwt
获取租户,然后将请求重定向到相关的 api
,如下所示
http://tenant1.com/api/product/getProduct 或者 http://tenant2.com/api/product/getProduct
最佳答案
您需要创建自定义中间件并将其注入(inject)到Authentication
中间件之后。最好的扩展点是 PreAuthorizationMiddleware
。假设您有一项服务可以通过用户声明解析租户 uri,如下所示:
public interface ITenantHostResolver
{
Task<Uri> Resolve(ClaimsPrincipal claimsPrincipal);
}
在您的 Startup
类中注入(inject)将覆盖下游设置的中间件:
public void Configure(IApplicationBuilder app)
{
var conf = new OcelotPipelineConfiguration
{
PreAuthorizationMiddleware = async (httpContext, next) =>
{
if(!httpContext.Request.Path.Equals("/api/product/getProduct"))
{
await next.Invoke();
return;
}
var claimsPrincipal = httpContext.User;
var tenantHostResolver = httpContext.RequestServices.GetRequiredService<ITenantHostResolver>();
var tenantHostAndPort = await tenantHostResolver.Resolve(claimsPrincipal);
var downstreamRequest = httpContext.Items.DownstreamRequest();
downstreamRequest.Host = tenantHostAndPort.Host;
downstreamRequest.Port = tenantHostAndPort.Port;
downstreamRequest.Scheme = tenantHostAndPort.Scheme;
await next.Invoke();
}
};
app.UseCustomOcelot(conf).Wait();
}
关于c# - 下游主机的 Ocelot 占位符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64824085/