Ocelot中文文档-认证

原文: Ocelot中文文档-认证

为了验证ReRoutes并随后使用Ocelot的任何基于声明的功能,如受权或使用令牌中的值修改请求。 用户必须像往常同样在他们的Startup.cs中注册认证服务,但他们给每一个注册提供了一个方案(认证提供商密钥),例如html

public void ConfigureServices(IServiceCollection services)
{
    var authenticationProviderKey = "TestKey";

    services.AddAuthentication()
        .AddJwtBearer(authenticationProviderKey, x =>
        {
        });
}

在此示例中,TestKey是此提供程序已注册的方案。 而后,咱们将其映射到配置中的ReRoute,例如api

"ReRoutes": [{
        "DownstreamHostAndPorts": [
            {
                "Host": "localhost",
                "Port": 51876,
            }
        ],
        "DownstreamPathTemplate": "/",
        "UpstreamPathTemplate": "/",
        "UpstreamHttpMethod": ["Post"],
        "ReRouteIsCaseSensitive": false,
        "DownstreamScheme": "http",
        "AuthenticationOptions": {
            "AuthenticationProviderKey": "TestKey",
            "AllowedScopes": []
        }
    }]

当Ocelot运行时,它会查看此ReRoutes的AuthenticationOptions.AuthenticationProviderKey并检查是否存在给定密钥注册的身份验证提供程序。 若是没有,那么Ocelot不会启动,若是有的话ReRoute将在执行时使用该提供者。ide

若是ReRoute配置了认证,Ocelot在执行认证中间件时将调用与其相关的任何验证方案。 若是请求认证失败,Ocelot返回http状态码401。code

JWT令牌

若是您想使用JWT令牌进行身份验证,例如Auth0等提供商,您能够使用正常的方式注册你的身份验证中间件。server

public void ConfigureServices(IServiceCollection services)
{
    var authenticationProviderKey = "TestKey";

    services.AddAuthentication()
        .AddJwtBearer(authenticationProviderKey, x =>
        {
            x.Authority = "test";
            x.Audience = "test";
        });

    services.AddOcelot();
}

而后将身份验证提供程序密钥映射到配置中的ReRoute,例如jwt

"ReRoutes": [{
        "DownstreamHostAndPorts": [
            {
                "Host": "localhost",
                "Port": 51876,
            }
        ],
        "DownstreamPathTemplate": "/",
        "UpstreamPathTemplate": "/",
        "UpstreamHttpMethod": ["Post"],
        "ReRouteIsCaseSensitive": false,
        "DownstreamScheme": "http",
        "AuthenticationOptions": {
            "AuthenticationProviderKey": "TestKey",
            "AllowedScopes": []
        }
    }]

Identity Server 承载令牌

为了使用IdentityServer承载令牌,请按照惯例在ConfigureServices 中使用方案(密钥)注册您的IdentityServer服务。 若是您不明白如何操做,请访问IdentityServer文档。htm

public void ConfigureServices(IServiceCollection services)
{
    var authenticationProviderKey = "TestKey";
    var options = o =>
        {
            o.Authority = "https://whereyouridentityserverlives.com";
            o.ApiName = "api";
            o.SupportedTokens = SupportedTokens.Both;
            o.ApiSecret = "secret";
        };

    services.AddAuthentication()
        .AddIdentityServerAuthentication(authenticationProviderKey, options);

    services.AddOcelot();
}

而后将身份验证提供程序密钥映射到配置中的ReRoute,例如中间件

"ReRoutes": [{
        "DownstreamHostAndPorts": [
            {
                "Host": "localhost",
                "Port": 51876,
            }
        ],
        "DownstreamPathTemplate": "/",
        "UpstreamPathTemplate": "/",
        "UpstreamHttpMethod": ["Post"],
        "ReRouteIsCaseSensitive": false,
        "DownstreamScheme": "http",
        "AuthenticationOptions": {
            "AuthenticationProviderKey": "TestKey",
            "AllowedScopes": []
        }
    }]

容许的范围

若是将范围添加到AllowedScopes,Ocelot将得到类型范围的全部用户声明(从令牌中),并确保用户具备列表中的全部范围。blog

这是一种基于范围限制对ReRoute访问的方式。文档

相关文章
相关标签/搜索