详解 Keycloak 对象 ClientRepresentation 参数配置

Documentation

For Developer Documentation see: https://nikiforovall.github.io/keycloak-authorization-services-dotnet

API Reference

See: https://nikiforovall.github.io/keycloak-authorization-services-dotnet-docs

  • Keycloak.AuthServices.Sdk.Kiota v26.0.5
  • https://www.nuget.org/packages/Keycloak.AuthServices.Sdk.Kiota#readme-body-tab

在这里插入图片描述

  • .net cli 安装命令
dotnet add package Keycloak.AuthServices.Sdk.Kiota --version 26.0.5

ClientRepresentation 对象

namespace Keycloak.AuthServices.Sdk.Kiota.Admin.Models;

public class ClientRepresentation : IAdditionalDataHolder, IParsable

里面有一个内置方法,获取当前模型的反序列化信息,如下所示:

/// <summary>
/// The deserialization information for the current model
/// </summary>
/// <returns>A IDictionary&lt;string, Action&lt;IParseNode&gt;&gt;</returns>
public virtual IDictionary<string, Action<IParseNode>> GetFieldDeserializers()
{
    return new Dictionary<string, Action<IParseNode>>
    {
        { "access", n => { Access = n.GetObjectValue<Keycloak.AuthServices.Sdk.Kiota.Admin.Models.ClientRepresentation_access>(Keycloak.AuthServices.Sdk.Kiota.Admin.Models.ClientRepresentation_access.CreateFromDiscriminatorValue); } },
        { "adminUrl", n => { AdminUrl = n.GetStringValue(); } },
        { "alwaysDisplayInConsole", n => { AlwaysDisplayInConsole = n.GetBoolValue(); } },
        { "attributes", n => { Attributes = n.GetObjectValue<Keycloak.AuthServices.Sdk.Kiota.Admin.Models.ClientRepresentation_attributes>(Keycloak.AuthServices.Sdk.Kiota.Admin.Models.ClientRepresentation_attributes.CreateFromDiscriminatorValue); } },
        { "authenticationFlowBindingOverrides", n => { AuthenticationFlowBindingOverrides = n.GetObjectValue<Keycloak.AuthServices.Sdk.Kiota.Admin.Models.ClientRepresentation_authenticationFlowBindingOverrides>(Keycloak.AuthServices.Sdk.Kiota.Admin.Models.ClientRepresentation_authenticationFlowBindingOverrides.CreateFromDiscriminatorValue); } },
        { "authorizationServicesEnabled", n => { AuthorizationServicesEnabled = n.GetBoolValue(); } },
        { "authorizationSettings", n => { AuthorizationSettings = n.GetObjectValue<Keycloak.AuthServices.Sdk.Kiota.Admin.Models.ResourceServerRepresentation>(Keycloak.AuthServices.Sdk.Kiota.Admin.Models.ResourceServerRepresentation.CreateFromDiscriminatorValue); } },
        { "baseUrl", n => { BaseUrl = n.GetStringValue(); } },
        { "bearerOnly", n => { BearerOnly = n.GetBoolValue(); } },
        { "clientAuthenticatorType", n => { ClientAuthenticatorType = n.GetStringValue(); } },
        { "clientId", n => { ClientId = n.GetStringValue(); } },
        { "clientTemplate", n => { ClientTemplate = n.GetStringValue(); } },
        { "consentRequired", n => { ConsentRequired = n.GetBoolValue(); } },
        { "defaultClientScopes", n => { DefaultClientScopes = n.GetCollectionOfPrimitiveValues<string>()?.ToList(); } },
        { "defaultRoles", n => { DefaultRoles = n.GetCollectionOfPrimitiveValues<string>()?.ToList(); } },
        { "description", n => { Description = n.GetStringValue(); } },
        { "directAccessGrantsEnabled", n => { DirectAccessGrantsEnabled = n.GetBoolValue(); } },
        { "directGrantsOnly", n => { DirectGrantsOnly = n.GetBoolValue(); } },
        { "enabled", n => { Enabled = n.GetBoolValue(); } },
        { "frontchannelLogout", n => { FrontchannelLogout = n.GetBoolValue(); } },
        { "fullScopeAllowed", n => { FullScopeAllowed = n.GetBoolValue(); } },
        { "id", n => { Id = n.GetStringValue(); } },
        { "implicitFlowEnabled", n => { ImplicitFlowEnabled = n.GetBoolValue(); } },
        { "name", n => { Name = n.GetStringValue(); } },
        { "nodeReRegistrationTimeout", n => { NodeReRegistrationTimeout = n.GetIntValue(); } },
        { "notBefore", n => { NotBefore = n.GetIntValue(); } },
        { "optionalClientScopes", n => { OptionalClientScopes = n.GetCollectionOfPrimitiveValues<string>()?.ToList(); } },
        { "origin", n => { Origin = n.GetStringValue(); } },
        { "protocol", n => { Protocol = n.GetStringValue(); } },
        { "protocolMappers", n => { ProtocolMappers = n.GetCollectionOfObjectValues<Keycloak.AuthServices.Sdk.Kiota.Admin.Models.ProtocolMapperRepresentation>(Keycloak.AuthServices.Sdk.Kiota.Admin.Models.ProtocolMapperRepresentation.CreateFromDiscriminatorValue)?.ToList(); } },
        { "publicClient", n => { PublicClient = n.GetBoolValue(); } },
        { "redirectUris", n => { RedirectUris = n.GetCollectionOfPrimitiveValues<string>()?.ToList(); } },
        { "registeredNodes", n => { RegisteredNodes = n.GetObjectValue<Keycloak.AuthServices.Sdk.Kiota.Admin.Models.ClientRepresentation_registeredNodes>(Keycloak.AuthServices.Sdk.Kiota.Admin.Models.ClientRepresentation_registeredNodes.CreateFromDiscriminatorValue); } },
        { "registrationAccessToken", n => { RegistrationAccessToken = n.GetStringValue(); } },
        { "rootUrl", n => { RootUrl = n.GetStringValue(); } },
        { "secret", n => { Secret = n.GetStringValue(); } },
        { "serviceAccountsEnabled", n => { ServiceAccountsEnabled = n.GetBoolValue(); } },
        { "standardFlowEnabled", n => { StandardFlowEnabled = n.GetBoolValue(); } },
        { "surrogateAuthRequired", n => { SurrogateAuthRequired = n.GetBoolValue(); } },
        { "type", n => { Type = n.GetStringValue(); } },
        { "useTemplateConfig", n => { UseTemplateConfig = n.GetBoolValue(); } },
        { "useTemplateMappers", n => { UseTemplateMappers = n.GetBoolValue(); } },
        { "useTemplateScope", n => { UseTemplateScope = n.GetBoolValue(); } },
        { "webOrigins", n => { WebOrigins = n.GetCollectionOfPrimitiveValues<string>()?.ToList(); } },
    };
}

这段代码是使用 Kiota 自动生成的反序列化逻辑,用于将 KeycloakREST API 响应中的 JSON 数据映射到 C# 模型类中。它定义了一个 Dictionary<string, Action<IParseNode>>,其中每个键(如 "access""adminUrl")对应一个字段名,值是一个委托方法,描述了如何从解析节点(IParseNode)中读取该字段的值并赋给模型类的属性。


🧱 类似结构说明

return new Dictionary<string, Action<IParseNode>>
{
    { "jsonFieldName", n => { ClassNameProperty = n.GetValue(); } }
};
  • "jsonFieldName":Keycloak 返回的 JSON 字段名称。
  • n => { ... }:处理该字段的解析逻辑。
  • ClassNameProperty:C# 模型类中对应的属性名。
  • n.GetValue():从 IParseNode 中提取值的方法。

🔍 各参数作用详解

JSON 字段对应属性作用
"access"Access客户端访问权限控制对象,例如是否允许配置、查看等。
"adminUrl"AdminUrl管理界面 URL,用于管理客户端的前端地址。
"alwaysDisplayInConsole"AlwaysDisplayInConsole是否始终在 Keycloak 控制台显示此客户端。
"attributes"Attributes自定义扩展属性,用于存储额外信息。
"authenticationFlowBindingOverrides"AuthenticationFlowBindingOverrides覆盖认证流程绑定设置,如自定义登录流程。
"authorizationServicesEnabled"AuthorizationServicesEnabled是否启用授权服务(基于 UMA)。
"authorizationSettings"AuthorizationSettings授权服务器相关配置,如资源保护策略。
"baseUrl"BaseUrl客户端的基础 URL,用于重定向或链接生成。
"bearerOnly"BearerOnly是否为 Bearer-only 客户端(仅接受 Token 认证)。
"clientAuthenticatorType"ClientAuthenticatorType客户端身份验证方式,如 client-secret。
"clientId"ClientId客户端唯一标识符,用于 OAuth2 流程中的客户端 ID。
"clientTemplate"ClientTemplate使用的客户端模板名称(用于继承配置)。
"consentRequired"ConsentRequired是否需要用户授权确认(OAuth2 中的同意页面)。
"defaultClientScopes"DefaultClientScopes默认分配给客户端的 scope 列表。
"defaultRoles"DefaultRoles客户端默认角色列表。
"description"Description客户端描述信息。
"directAccessGrantsEnabled"DirectAccessGrantsEnabled是否启用直接访问授权(Resource Owner Password Credentials)。
"directGrantsOnly"DirectGrantsOnly是否只允许直接授权模式。
"enabled"Enabled客户端是否启用。
"frontchannelLogout"FrontchannelLogout是否启用前端通道注销(Frontchannel Logout)。
"fullScopeAllowed"FullScopeAllowed是否允许访问所有 scope。
"id"Id客户端内部唯一 ID(通常为 UUID)。
"implicitFlowEnabled"ImplicitFlowEnabled是否启用隐式授权流程(Implicit Flow)。
"name"Name客户端显示名称。
"nodeReRegistrationTimeout"NodeReRegistrationTimeout集群节点重新注册超时时间(秒)。
"notBefore"NotBefore客户端生效前的时间戳(Unix 时间戳)。
"optionalClientScopes"OptionalClientScopes可选的客户端 scope 列表。
"origin"Origin客户端来源(如 SSO 提供商)。
"protocol"Protocol使用的身份协议类型,如 openid-connect、saml。
"protocolMappers"ProtocolMappers协议映射器列表,用于将用户属性映射到 Token。
"publicClient"PublicClient是否为公共客户端(不需客户端密钥)。
"redirectUris"RedirectUrisOAuth2 登录完成后允许跳转的 URI 列表。
"registeredNodes"RegisteredNodes已注册的集群节点信息。
"registrationAccessToken"RegistrationAccessToken注册客户端时使用的访问令牌。
"rootUrl"RootUrl根路径 URL,用于构建完整 URL。
"secret"Secret客户端密钥(用于身份验证)。
"serviceAccountsEnabled"ServiceAccountsEnabled是否启用服务账户(自动创建服务用户)。
"standardFlowEnabled"StandardFlowEnabled是否启用标准授权码流程(Authorization Code Flow)。
"surrogateAuthRequired"SurrogateAuthRequired是否需要代理认证(用于外部系统)。
"type"Type客户端类型(如 confidential、bearer-only)。
"useTemplateConfig"UseTemplateConfig是否使用模板配置进行初始化。
"useTemplateMappers"UseTemplateMappers是否使用模板映射器。
"useTemplateScope"UseTemplateScope是否使用模板 scope。
"webOrigins"WebOrigins允许跨域请求的源列表(CORS 设置)。

📌 总结

这些字段共同构成了一个完整的 Keycloak ClientRepresentation 模型,表示一个客户端应用在 Keycloak 中的所有配置信息。通过这个模型,你可以:

  • 获取/更新客户端配置;
  • 动态管理客户端行为;
  • 实现与 Keycloak 的集成(如自动化配置、SSO 等);

适用于开发基于 Keycloak 的统一认证中心、多租户系统、微服务安全网关等场景。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ChaITSimpleLove

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值