【问题标题】:Multiple Api Resource多个 API 资源
【发布时间】:2017-06-08 20:29:48
【问题描述】:

计划设置一个 IdentityServer 并为其配置多个资源,例如:

internal static IEnumerable<ApiResource> GetApiResources()
{
        return new List<ApiResource>
        {
            new ApiResource()
            {
                Name = "API 1",
                DisplayName = "API 1",
                Description = "API 1 Access",
                Scopes = new List<Scope>()
                {
                    new Scope("public"), new Scope("write")
                }
            },
            new ApiResource
            {
                Name = "API 2",
                DisplayName = "API 2",
                Description = "API 2 Access",
                Scopes = new List<Scope>
                {
                    new Scope("public"), new Scope("write")
                }
            }
        };
}

那么客户端 1 将只能访问 API 1,而客户端 2 将只能访问 API 2。两个客户端都将具有公共范围。

上面的方法会起作用吗,还是我应该更改范围的名称并使其对每个 API 资源都是唯一的?

为多个 API 使用 1 个身份/授权服务器是个坏主意吗?

【问题讨论】:

    标签: identityserver3 identityserver4


    【解决方案1】:

    这种设计的主要问题是客户端 1 和客户端 2 在它们的不记名令牌中都具有相同的范围。即两个客户端都可以访问任一 API 资源。您可以通过 API 执行以下操作“命名空间”您的范围:

    internal static IEnumerable<ApiResource> GetApiResources()
    {
            return new List<ApiResource>
            {
                new ApiResource()
                {
                    Name = "API 1",
                    DisplayName = "API 1",
                    Description = "API 1 Access",
                    Scopes = new List<Scope>()
                    {
                        new Scope("api1.public"), new Scope("api1.write")
                    }
                },
                new ApiResource
                {
                    Name = "API 2",
                    DisplayName = "API 2",
                    Description = "API 2 Access",
                    Scopes = new List<Scope>
                    {
                        new Scope("api2.public"), new Scope("api2.write")
                    }
                }
            };
    }
    

    话虽如此,将 1 个授权服务器用于多个 API/资源并没有错。将 1 个授权服务器用于多个 API 是将您的授权问题分离到身份服务器的优势之一。

    【讨论】:

    • 这也是我的想法,但客户/消费者是否不必将范围配置为:“scope=api2.public”。这可以接受吗?公开api名称?
    • 绝对,这很好。如果您不希望公众 (Scope.ShowInDiscoveryDocument = false) 容易看到发现文档 (.well-known/openid-configuration) 中的范围,则可以隐藏范围,但最终每个客户端都需要暴露于其特定集合范围。
    猜你喜欢
    • 1970-01-01
    • 2018-01-13
    • 2013-09-03
    • 2019-08-14
    • 2016-01-27
    • 2021-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多