0

我有一个 API 网关,在本例中称为 Gateway.Api。在Startup课堂上,我有以下内容:

public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddOcelot(Configuration);
        services.AddMvc();

        var appSettingSection = Configuration.GetSection("AppSettings");
        services.Configure<AppSettings>(appSettingSection);

        var appSettings = appSettingSection.Get<AppSettings>();
        var key = Encoding.ASCII.GetBytes(appSettings.Secret);

        services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
            .AddJwtBearer(options =>
            {
                options.SaveToken = true;
                options.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateIssuerSigningKey = true,
                    IssuerSigningKey = new SymmetricSecurityKey(key),
                    ValidateIssuer = false,
                    ValidateAudience = false
                };
        });
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseAuthentication();
        app.UseOcelot().Wait();
        app.UseMvc();
    }

如您所见,它定义了身份验证方案。

使用Ocelot我的以下配置文件Gateway.Api

{
  "ReRoutes": [
    {
      "DownstreamPathTemplate": "/api/customer",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",
          "Port": 50366
        }
      ],
      "UpstreamPathTemplate": "/api/customer",
      "UpstreamHttpMethod": [ "Get" ],
      "AuthenticationOptions": {
        "AuthenticationProviderKey": "Bearer",
        "AllowedScopes": []
      }
    },
    {
      "DownstreamPathTemplate": "/api/user/authenticate",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",
          "Port": 50353
        }
      ],
      "UpstreamPathTemplate": "/api/user/authenticate",
      "UpstreamHttpMethod": [ "Post" ]
    }
  ],
  "GlobalConfiguration": {
    "UseServiceDiscovery": false
  }
}

当我尝试在没有令牌的情况下访问http://localhost:50333/api/customer(Gateway.Api 的端口为 50333)时,我收到 401 响应,证明配置文件和身份验证都有效。

除了客户微服务,我还有一个身份微服务,它允许用户使用有效的用户名和密码进行身份验证,然后发出一个令牌。然后使用此令牌调用客户服务,我得到一个成功的响应(200 OK)。

现在由于某种原因,如果我直接访问客户服务而不使用网关(所以http://localhost:50366/api/customer)我能够在没有令牌的情况下获得成功的响应。

下面是客户微服务:

[Route("api/[controller]")]
public class CustomerController : Controller
{
    [HttpGet]
    public IEnumerable<string> Get()
    {
        var customers = new string[] {
            "test",
            "test"
        };

        return customers;
    }
}

这是否意味着我必须为每个微服务Startup类添加一个身份验证方案?如果是这样,这不是矫枉过正吗?

我所做的尝试是[Authorize]在 Customer 微服务中的操作上使用一个属性,但这会引发一个异常,即它们不是默认的身份验证方案。

4

2 回答 2

1

这是开发环境,因此您可以直接访问 url。您的客户服务不知道网关。在实际生产环境中,您通常只会暴露 API 网关,其余服务位于防火墙(私有子网)后面。只有 API 网关可以访问它们。访问服务的唯一方法是通过网关。但是,如果您想公开服务,则必须进行单独的服务身份验证。

无论如何 向您想要保护的服务添加身份验证总是一个好主意。

于 2019-05-13T23:39:23.460 回答
0

我们这样理解,我们为什么要使用API​​ Gateway?

使用 API Gateway 的原因有很多,其中之一是:

这样我们就可以在 API 网关添加认证,而不是在许多微服务中添加认证代码。

在生产服务器机器中,我们只为最终用户开放 API Gateway 端口,用户不知道其他微服务托管在哪里,也无法通过尝试其他端口来访问,因为其他端口未打开!

此外,我们可以将 micros 服务放在另一台机器上,该机器只能通过将其 IP 地址列入白名单来托管 API 网关的机器访问。

但是在这种情况下,您信任您的开发人员和 DevOps 团队,否则您可以对高价值微服务进行进一步的身份验证,并且此身份验证与最终用户使用的身份验证没有什么不同,您将在此处对 API 网关进行身份验证。

于 2019-05-23T10:03:21.660 回答