使用 Authelia OpenID Connect 1.0 为 Jenkins 配置单点登录(SSO):完整集成指南

使用 Authelia OpenID Connect 1.0 为 Jenkins 配置单点登录(SSO):完整集成指南 使用 Authelia OpenID Connect 1.0 为 Jenkins 配置单点登录SSO完整集成指南【免费下载链接】autheliaThe Single Sign-On Multi-Factor portal for web apps. OpenID Certified™ and Post-Quantum Cryptography Ready.项目地址: https://gitcode.com/GitHub_Trending/au/authelia本文以 Authelia 的 OpenID Connect 1.0 Provider 为身份源详细讲解如何将 Jenkins 作为 Relying Party信赖方接入 Authelia实现统一登录。指南完整覆盖oic-auth插件的安装、Authelia 侧客户端注册配置含 client_secret 摘要、PKCE、scope、authorization_policy 等关键参数、Jenkins 侧 Configuration as CodeJCasC与 Web GUI 两种配置方式并深入剖析配置项背后的源码实现与 claim 映射原理帮助读者一次性打通 Jenkins 与 Authelia 的 SSO 链路。测试版本与前提假设本集成方案在以下版本组合上经过官方验证见 Jenkins 集成文档Autheliav4.39.13Jenkinsv2.516.3官方示例基于以下环境假设实际部署时请替换为你的真实域名配置项示例值Application Root URLJenkins 根地址https://jenkins.example.com/Authelia Root URLAuthelia 根地址https://auth.example.com/Client IDjenkinsClient Secretinsecure_secret其中auth.example.com同时也是 Authelia OpenID Connect 1.0 的 Issuer签发者Jenkins 将通过https://auth.example.com/.well-known/openid-configuration发现端点来自动获取授权、令牌、UserInfo 等全部端点信息。文档中的部分域名变量如example.com、auth会被官方文档系统自动替换动手部署时请相应替换。前置Authelia 的 OpenID Connect 1.0 Provider 概览Authelia 可以充当 OpenID Connect 1.0 ProviderOP并已通过 OpenID 基金会的 [OpenID Certified™] 认证覆盖 Basic OP / Implicit OP / Hybrid OP / Form Post OP / Config OP 配置档。它实现了 Core、Discovery 与 Form Post Response Mode并完整支持 PKCERFC 7636、Pushed Authorization RequestsRFC 9126、Token IntrospectionRFC 7662、Token RevocationRFC 7009等机制详见 OpenID Connect 1.0 集成总览。在本集成场景中Jenkins 扮演Relying PartyRP角色通过发现端点读取 Authelia 的 Provider 元数据从而得知以下关键端点路径路径均追加在 Authelia 根 URL 之后发现端点/.well-known/openid-configurationJWKS/jwks.json授权端点/api/oidc/authorization令牌端点/api/oidc/tokenUserInfo 端点/api/oidc/userinfo客户端在 Authelia 中的注册与授权策略、scope 权限、PKCE 要求等均由 Authelia 的identity_providers.oidc.clients配置决定详见 OpenID Connect 1.0 Clients 配置指南。第一步在 Jenkins 中安装 OpenId Connect Authentication PluginJenkins 侧通过社区插件OpenId Connect Authentication Pluginoic-auth实现 OpenID Connect 依赖方功能。该插件可通过两种方式安装方式一Web GUI 安装访问Manage Jenkins系统管理。进入Plugins插件管理。选择Available Plugins可选插件。搜索oic-auth。点击安装。重启 Jenkins 使插件生效。方式二CLI 安装使用 Jenkins 自带的插件安装工具jenkins-plugin-cli --plugins oic-auth第二步在 Authelia 中注册 Jenkins 客户端回到 Authelia 配置文件configuration.yml在identity_providers.oidc.clients下新增一个jenkins客户端。以下是官方示例的完整配置identity_providers: oidc: ## The other portions of the mandatory OpenID Connect 1.0 configuration go here. ## See: https://www.authelia.com/c/oidc clients: - client_id: jenkins client_name: Jenkins client_secret: $pbkdf2-sha512$310000$c8p78n7pUMln0jzvd4aK4Q$JNRBzwAo0ek5qKn50cFzzvE9RXV88h1wJn5KGiHrD0YKtZaR/nCb2CJPOsKaPK0hjf.9yHxzQGZziziccp6Yng # The digest of insecure_secret. public: false authorization_policy: two_factor require_pkce: true pkce_challenge_method: S256 redirect_uris: - https://jenkins.example.com/accounts/authelia/login/callback scopes: - openid - profile - email - groups response_types: - code grant_types: - authorization_code access_token_signed_response_alg: none userinfo_signed_response_alg: none token_endpoint_auth_method: client_secret_basic客户端参数逐项解析以下参数均来自 Authelia 的 Clients 配置参考并结合源码 RegisteredClient 结构体 说明其底层作用client_id必填客户端的唯一标识必须与 Jenkins 侧配置的 Client ID 完全一致。官方建议使用半长的随机字母数字字符串字符必须属于 RFC 3986 非保留字符长度不超过 100。client_name可选默认同 client_id显示在 Authelia 同意consent界面上的友好名称。client_secret按需必填Authelia 与 Jenkins 之间的共享密钥。注意示例中存储的是insecure_secret明文经过 PBKDF2-SHA512 哈希后的摘要而非明文本身明文对应的摘要为$pbkdf2-sha512$310000$c8p78n7pUMln0jzvd4aK4Q$JNRBzwAo0ek5qKn50cFzzvE9RXV88h1wJn5KGiHrD0YKtZaR/nCb2CJPOsKaPK0hjf.9yHxzQGZziziccp6Yng。生产环境务必使用高强度随机密钥并妥善保管。public布尔默认false设为true表示公开客户端类型如 SPA、CLI此时 client_secret 必须为空字符串。Jenkins 作为可安全持有密钥的服务端应用应保持false机密客户端。authorization_policy默认two_factor该客户端发起授权请求时要求的最低认证级别可选one_factor、two_factor或全局定义的命名策略。该策略仅作用于 OIDC 授权请求与访问控制规则Access Control Rules是两套独立机制。redirect_uris必填合法的回调 URI 列表大小写敏感且必须带http或https协议。本场景中oic-auth插件的默认回调路径为/accounts/authelia/login/callback。凡是不在该列表中的回调地址都会被 Authelia 拒绝。scopes默认openid,groups,profile,email允许该客户端请求的 scope 白名单。本示例显式列出了 Jenkins 需要的四个 scope其中openid是启用 OpenID Connect 语义返回 ID Token的必要 scopeprofile、email提供用户身份信息groups提供用户组信息Jenkins 据此做基于组的授权。各 scope 对应的 claim 明细可参考 OpenID Connect 1.0 Claims 指南。response_types默认code授权码流程。官方安全建议是仅使用code响应类型因为其它类型implicit/hybrid安全性较弱。grant_types默认authorization_code允许该客户端使用的授权类型。若需要刷新令牌可额外加入refresh_token并配合offline_accessscope。require_pkce布尔默认false强制该客户端必须使用 PKCE。oic-auth插件支持 PKCE官方示例将其开启以增强安全性。pkce_challenge_method默认空指定 PKCE challenge 方法合法值为空、plain或S256。S256是强烈推荐的值——它要求依赖方在令牌请求时证明自己掌握与授权码绑定的code_verifier可有效缓解授权码拦截攻击。设置该值会同时等效启用require_pkce。access_token_signed_response_alg / userinfo_signed_response_alg默认none访问令牌与 UserInfo 响应的签名算法。设为none表示不签名UserInfo 以纯 JSON 返回application/json; charsetutf-8这是绝大多数客户端兼容的做法。token_endpoint_auth_method默认client_secret_basic客户端在令牌端点的认证方式即通过 HTTP Basic 认证发送 client_id/client_secret。第三步配置 Jenkins 侧Jenkins 侧提供两种配置方式Configuration as CodeJCasC与Web GUI二者等价按需选用其一即可。方式一Configuration as Code推荐可版本化将以下 YAML 写入 Jenkins 的 JCasC 配置jenkins: systemMessage: This Jenkins instance was configured using the Authelia example Configuration as Code, thanks Authelia! securityRealm: oic: clientId: jenkins clientSecret: insecure_secret disableSslVerification: false emailFieldName: email fullNameFieldName: name groupIdStrategy: caseSensitive groupsFieldName: groups logoutFromOpenidProvider: false properties: - pkce - escapeHatch: group: admin-users secret: escapeHatch username: escapeHatch sendScopesInTokenRequest: true serverConfiguration: wellKnown: scopesOverride: openid profile email groups wellKnownOpenIDConfigurationUrl: https://auth.example.com/.well-known/openid-configuration userIdStrategy: caseSensitive userNameField: preferred_username关键字段说明clientId / clientSecret与 Authelia 侧client_id、client_secret明文一一对应。wellKnownOpenIDConfigurationUrlAuthelia 的 OIDC 发现端点Jenkins 据此自动拉取 Provider 元数据无需手工填写各端点。scopesOverride覆盖默认 scope 列表必须与 Authelia 侧授权的 scope 一致openid profile email groups。userNameField登录用户名取自 ID Token / UserInfo 中的preferred_usernameclaim。fullNameFieldName / emailFieldName / groupsFieldName分别映射到name、email、groupsclaim用于填充 Jenkins 用户的显示名、邮箱与所属组。groupIdStrategy / userIdStrategy设为caseSensitive表示组名/用户名大小写敏感匹配。sendScopesInTokenRequest在令牌请求中携带 scope 参数。properties启用附加属性——pkce开启 PKCE与 Authelia 侧require_pkce/pkce_challenge_method: S256呼应escapeHatch提供应急逃生通道当 OpenID Provider 不可用时允许使用本地配置的username/secret凭据以指定group登录。disableSslVerification默认false保持 TLS 证书校验开启切勿在生产环境关闭。logoutFromOpenidProvider默认false表示 Jenkins 登出时不联动注销 Authelia 会话。方式二Web GUI访问Manage Jenkins。进入Security安全。在 Security Realm 中选择Login with Openid Connect。按如下配置Client idjenkinsClient secretinsecure_secretConfiguration modeDiscovery via well-known endpoint通过 well-known 端点自动发现Well-known configuration endpointhttps://auth.example.com/.well-known/openid-configuration在Advanced高级下Override scopes覆盖 scopeopenid profile email groups在Advanced configuration高级配置下在User fields用户字段下User name field namepreferred_usernameFull name field namenameEmail field nameemailGroups field namegroups添加以下属性propertiesEnable Proof Key for Code Exchange启用 PKCEEnabled开启Configure Escape Hatch for when the OpenID Provider is unavailableOpenID Provider 不可用时的逃生通道建议启用Web GUI 中的字段与 JCasC 配置一一对应二者选一即可。登录流程与 claim 映射原理完成上述两端配置后完整的登录流程如下用户访问 Jenkins 未认证页面被重定向到 Authelia 的授权端点/api/oidc/authorization。Authelia 依据authorization_policy: two_factor要求用户完成双因素认证密码 TOTP/WebAuthn 等。认证通过后若配置要求用户需在同意consent界面确认授予 Jenkins 的 scope。Authelia 将授权码通过回调 URIhttps://jenkins.example.com/accounts/authelia/login/callback返回给 Jenkins。Jenkins 携带code与 PKCEcode_verifier以client_secret_basic方式向令牌端点/api/oidc/token换取 ID Token 与 Access Token。Jenkins 从 ID Token 中读取preferred_username建立本地用户并通过 UserInfo 端点/api/oidc/userinfo获取email、name、groups等补充 claim。需要留意的是由于 Authelia 默认将大多数身份 claim 放在 UserInfo 端点而非 ID Token 中出于隐私与最小化设计依赖方应通过 Access Token 调用 UserInfo 端点获取这些 claim——这正是oic-auth插件会执行的标准流程。若个别客户端无法请求 UserInfo 端点可在 Authelia 侧通过 claims policy 将所需 claim 注入 ID Token但这属于 break-glass 的非常规手段详见 Claims 指南中的相关章节。用户与组映射建议用户标识Authelia 的subclaim 是基于 RFC 4122 的 UUID v4 不透明标识isssub组合是跨登录唯一且可靠地关联用户的方式preferred_username与email仅建议用于首次配置账号不应作为长期主键。组映射Jenkins 的groupsFieldName: groups直接消费 Authelia 返回的groupsclaim。确保 Authelia 的身份后端LDAP 或文件正确配置了组信息并在 Authelia 授权配置中合理设置组的访问权限Jenkins 端即可按组进行授权与角色管理。大小写策略groupIdStrategy与userIdStrategy均设为caseSensitive时组名/用户名按大小写敏感匹配避免出现同名不同组的隐性问题。验证与常见问题排查配置完成后可通过以下方式验证集成是否生效访问https://jenkins.example.com应被重定向至 Authelia 登录页。使用 Authelia 用户凭据含第二因素完成认证并同意授权。回到 Jenkins确认右上角显示的用户名、邮箱与所属组正确。若配置了escapeHatch可在 Authelia 服务不可用时使用escapeHatch/escapeHatch凭据登录生产环境请务必更换默认值。常见问题定位思路回调被拒redirect_uri 不匹配检查 Authelia 侧redirect_uris是否与 Jenkins 实际回调地址完全一致含协议、域名、大小写。scope 不足确保 Jenkins 的scopesOverride与 Authelia 侧scopes列表一致openidscope 必须存在。PKCE 报错确认两端均启用了 PKCE 且 challenge 方法为S256oic-auth插件在properties中勾选pkce即会启用。发现端点访问失败确认wellKnownOpenIDConfigurationUrl可被 Jenkins 服务器访问且 Authelia 的 OIDC Provider 已正确启用identity_providers.oidc配置完整。参考链接Jenkins 集成官方文档OpenID Connect 1.0 集成总览OpenID Connect 1.0 Clients 配置指南OpenID Connect 1.0 Claims 指南OpenID Connect 1.0 常见问题【免费下载链接】autheliaThe Single Sign-On Multi-Factor portal for web apps. OpenID Certified™ and Post-Quantum Cryptography Ready.项目地址: https://gitcode.com/GitHub_Trending/au/authelia创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考