c# - 下游主机的 Ocelot 占位符

标签 c# gateway ocelot

我有一个使用 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/

相关文章:

Magento 支付流程

asp.net - Ocelot + consul + 我的 web api (.Net 5) 在 docker 中通过 HTTPS

c# - 我们可以在没有 azure 或机器人模拟器的情况下在自己的客户端中测试机器人吗?

c# - 为什么 MouseEventArgs 在等式中使用时不起作用

google-kubernetes-engine - 连接到 Istio 入口网关出现 404 错误

c# - 如何在 Ocelot 中实现 cors header

c# - Ocelot - 更改网关中的上游请求主体不会导致下游请求发生变化

c# - 将数据从单个应用程序的多个实例写入一个文件,C#,Visual Studio 2008

c# - 为什么 C# 3 允许将文字零 (0) 隐式转换为任何枚举?

javascript - 识别使用 Braintree dropin ui 进行购买的客户