美文网首页
Ocelot中文文档-认证

Ocelot中文文档-认证

作者: loogn | 来源:发表于2018-05-05 15:13 被阅读521次

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

{
    var authenticationProviderKey = "TestKey";

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

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

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

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

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

JWT令牌

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

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

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

    services.AddOcelot();
}

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

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

Identity Server 承载令牌

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

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将获得类型范围的所有用户声明(从令牌中),并确保用户具有列表中的所有范围。

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

previous
next

相关文章

  • Ocelot中文文档-认证

    为了验证ReRoutes并随后使用Ocelot的任何基于声明的功能,如授权或使用令牌中的值修改请求。 用户必须像往...

  • Ocelot中文文档-授权

    Ocelot支持基于声明的授权。 这意味着如果您有要授权的路由,您可以将以下内容添加到您的ReRoute配置中。 ...

  • Ocelot中文文档-缓存

    目前Ocelot使用CacheManager项目提供了一些非常基本的缓存。这是一个了不起的项目,它解决了很多缓存问...

  • Ocelot中文文档-日志

    目前,Ocelot使用标准的日志记录接口ILoggerFactory/ILogger 。 在IOcelotL...

  • Ocelot中文文档-GraphQL

    好吧!你明白我的意思Ocelot并不直接支持GraphQL,但有这么多人问起它,我想表明整合graphql-dot...

  • Ocelot中文文档-管理

    Ocelot支持在运行时通过一个认证的Http API修改配置。有两种方式对其验证, 使用Ocelot的内置Ide...

  • Ocelot中文文档-Websockets

    Ocelot额外支持代理websockets。这个功能在问题 212中被提出。 为了是Ocelot代理websoc...

  • Ocelot中文文档-跟踪

    Ocelot使用一个杰出的项目Butterfly 提供了跟踪功能。 为了使用跟踪,请阅读Butterfly的文档。...

  • Ocelot中文文档-路由

    Ocelot的主要功能是接管进入的http请求并把它们转发给下游服务。目前是以另一个http请求的形式(将来可能是...

  • Ocelot中文文档-入门

    Ocelot只能用于.NET Core,目前是为netcoreapp2.0构建的,这个文档可能会帮你了解Ocelo...

网友评论

      本文标题:Ocelot中文文档-认证

      本文链接:https://www.haomeiwen.com/subject/vgelrftx.html