我有一个应用程序接口网关,在本例中称为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响应,它证明配置文件和身份验证是有效的。
除了客户微服务,我还有一个身份微服务,它允许用户使用有效的用户名和密码进行身份验证,然后将颁发令牌。使用这个令牌呼叫客服,我得到了一个成功的响应(200OK)。
现在,由于某些原因,如果我不使用网关(所以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类添加身份验证方案?如果是这样的话,这是不是有点过头了?
我确实尝试了在Customer微服务中的操作上使用[Authorize]属性,但是这抛出了一个异常,即它们不是默认的身份验证方案。
发布于 2019-05-14 07:39:23
这是开发环境,所以你可以直接访问url。您的客服对网关一无所知。在实际的生产环境中,您通常只会暴露API网关,其余的服务位于防火墙(私有子网)之后。只有API网关才能访问它们。访问服务的唯一方法是通过网关。但是,如果您想公开服务,则必须进行单独的服务身份验证。
无论如何,向您想要保护的服务添加身份验证始终是个好主意。
发布于 2019-05-23 18:03:22
让我们这样理解,为什么我们要使用API Gateway?
使用API Gateway的原因有很多,其中之一是:
这样我们就可以在API网关上添加身份验证,而不是在许多微服务中添加身份验证代码。
在生产服务器机器中,我们只为最终用户开放API网关端口,用户不知道其他micros服务托管在哪里,也不能通过尝试其他端口进行访问,因为其他端口没有打开!
此外,我们还可以将micros服务放在另一台机器上,这台机器只能通过将托管API Gateway的机器的IP地址列入白名单来访问。
但是在这种情况下,您信任您的开发人员和DevOps团队,否则您可以对高价值微服务进行进一步的身份验证,此身份验证与最终用户使用的身份验证没有什么不同,这里您将对API Gateway进行身份验证。
https://stackoverflow.com/questions/56120753
复制相似问题