ARTICLE DETAIL

资讯详情

深耕网站建设与运营推广的一线实战洞察。

Apereo CAS 中配置 SAML2 认证上下文类(AuthnContext):服务级覆盖、Groovy 脚本化与 MFA 映射

Apereo CAS 中配置 SAML2 认证上下文类(AuthnContext):服务级覆盖、Groovy 脚本化与 MFA 映射 后端认证鉴权单点登录【免费下载链接】casApereo CAS - Identity Single Sign On for all earthlings and beyond.项目地址https://gitcode.com/gh_mirrors/ca/cas点击查看免费下载在 Apereo CAS 作为 SAML2 IdP 的场景中每个服务提供者SP都可以在注册服务定义中指定一个必需的认证上下文类Authentication Context Class它会在最终回传给 SP 的 SAML2 断言中覆写AuthnContext元素本文围绕 Configuring-SAML2-AuthnContextClass.md 展开讲解静态值、外部/内嵌 Groovy 脚本、自定义 Builder 四种配置方式并结合仓库源码剖析默认匹配流程与多因子认证MFA触发机制。读完本文你可以掌握cas.authn.saml-idp.core.context相关配置项的完整取值逻辑、requiredAuthenticationContextClass服务字段的解析链路以及如何通过签名验证将 SAML2 认证请求映射到 MFA 配置文件。1. 核心概念SAML2 断言中的 AuthnContextSAML2 断言中的AuthnContext元素用于向 SP 声明本次认证所达到的强度级别由AuthnContextClassRef一个 URI 引用和AuthenticatingAuthority组成。CAS 通过 SamlProfileAuthnContextClassRefBuilder 构建该元素在build方法中若解析出的类值非空则创建AuthnContextClassRef并设置 URI随后调用buildDefaultAuthenticatingAuthority将 CAS IdP 元数据中解析出的 EntityID 作为AuthenticatingAuthority加入除非注册服务设置了skipGeneratingAuthenticatingAuthority见 SamlProfileAuthnContextClassRefBuilder.java#L56-L82。原文档给出的全局配置入口是cas.authn.saml-idp.core.context.*对应的配置模型类为 SamlIdPAuthenticationContextProperties包含两个属性配置属性类型默认值说明cas.authn.saml-idp.core.context.default-authentication-context-classStringurn:oasis:names:tc:SAML:2.0:ac:classes:PasswordProtectedTransport服务未指定时使用、且请求上下文匹配失败时的兜底值cas.authn.saml-idp.core.context.authentication-context-class-mappingsListString空认证上下文类到 MFA 提供者或委托认证目标上下文的映射形式为key-value其中映射项的 Javadoc 给出了两个典型示例面向 MFA 的映射如urn:oasis:names:tc:SAML:2.0:ac:classes:SomeClassName-mfa-duo面向委托认证delegated authenticationCAS 代理/委托到外部 SAML2 IdP的映射如https://refeds.org/profile/mfa-http://schemas.microsoft.com/claims/multipleauthn用于把 SP 提交的 RefEDS MFA 上下文转写为 AAD 的 multipleauthn 上下文找不到映射时原始上下文原样透传。2. 服务级覆盖requiredAuthenticationContextClass每个 SAML2 注册服务都可以携带requiredAuthenticationContextClass字段它定义在 SamlRegisteredService 中并标注了ExpressionLanguageCapableSamlRegisteredService.java#L56-L57因此字段值不仅可以是字面量还可以包含 Spring 表达式语言片段。原文档给出四种取值方式以下完整保留其示例。2.1 静态值Static最终响应中永远使用指定的认证上下文类{ class: org.apereo.cas.support.saml.services.SamlRegisteredService, serviceId: https://spring.io/security/saml-sp, name: SAML, id: 1, metadataLocation: /path/to/sp-metadata.xml, requiredAuthenticationContextClass: https://refeds.org/profile/mfa, }源码印证在 buildAuthnContextClassRefValue 中只要服务字段非空且不是可执行脚本就会经SpringExpressionLanguageValueResolver.resolve处理后直接返回该值跳过对AuthnRequest中RequestedAuthnContext的一切协商——这正是覆写 SP 请求语义的实现位置。2.2 外部 Groovy 脚本External Groovy把字段值设为一个脚本文件引用file:///...即可动态计算上下文类{ class: org.apereo.cas.support.saml.services.SamlRegisteredService, serviceId: https://spring.io/security/saml-sp, name: SAML, id: 1, metadataLocation: /path/to/sp-metadata.xml, requiredAuthenticationContextClass: file:///path/to/GroovyScript.groovy }脚本本身的写法脚本接收context与logger两个参数import org.apereo.cas.support.saml.web.idp.profile.builders.* def run(final Object... args) { def (samlContext,logger) args logger.info(Building context for entity {}, samlContext.adaptor.entityId) /** This is where you calculate the final context class... */ return https://refeds.org/profile/mfa }源码印证buildAuthnContextClassRefValue通过ExecutableCompiledScriptFactory判断字段值是否为可执行脚本对file://这类外部脚本会先用 Spring 表达式解析器解引用用于支持${...}/SpEL 占位再交给buildScriptedAuthnContextClassRef执行——后者从ScriptResourceCacheManager取出编译缓存的脚本以(context, SamlProfileBuilderContext, logger, LOGGER)作为绑定参数执行并把String结果作为最终的上下文类 URISamlProfileAuthnContextClassRefBuilder.java#L119-L137。脚本执行失败时会抛出Unable to locate script cache manager or execute groovy script异常。要让 CAS 支持并集成 Apache Groovy请参考 Apache-Groovy-Scripting。2.3 内嵌 Groovy 脚本Embedded Groovy与外部脚本一致只是脚本直接内嵌在服务定义里{ class: org.apereo.cas.support.saml.services.SamlRegisteredService, serviceId: https://spring.io/security/saml-sp, name: SAML, id: 1, metadataLocation: /path/to/sp-metadata.xml, requiredAuthenticationContextClass: groovy { return https://refeds.org/profile/mfa } }同样需要先完成 Apache-Groovy-Scripting 所述的集成准备。从源码看内嵌脚本走isExternalScript false分支直接以字段原文作为脚本内容执行SamlProfileAuthnContextClassRefBuilder.java#L90-L95。2.4 自定义 BuilderCustom也可以在自己的AutoConfiguration配置类中注册一个实现覆盖默认的上下文类构建逻辑Bean public SamlProfileAuthnContextClassRefBuilder defaultAuthnContextClassRefBuilder() { return new MyBuilder(); }自定义配置类需要注册进 CAS具体做法参见 Configuration-Management-Extensions。由于SamlProfileAuthnContextClassRefBuilder的匹配、脚本执行、MFA 属性比较等方法均为protected子类可以在不重写整个构建流程的前提下仅替换其中某一环例如替换buildDefaultAuthenticationContextClass的映射逻辑。3. 未指定服务字段时的默认匹配流程当服务没有配置requiredAuthenticationContextClass时buildAuthnContextClassRefValue进入请求协商 映射分支SamlProfileAuthnContextClassRefBuilder.java#L99-L116可以归纳为三步取出默认值getDefaultAuthenticationContextClass返回cas.authn.saml-idp.core.context.default-authentication-context-class为空时回落到 OpenSAML 的AuthnContext.PPT_AUTHN_CTX即PasswordProtectedTransport解析 SP 请求若当前 SAML 消息是AuthnRequest且携带非空的RequestedAuthnContext/AuthnContextClassRef列表则进入映射匹配否则直接返回默认值映射匹配getAuthenticationContextByAssertion按请求中第一个出现在authentication-context-class-mappings键集中的AuthnContextClassRef取出其映射值逗号分隔的多个候选 MFA 上下文名然后与断言属性中cas.authn.mfa.core.authentication-context-attribute默认值为authnContextClass见 MultifactorAuthenticationCoreProperties.java#L31记录的用户实际达到的认证级别逐一比较匹配成功则返回请求上下文对应的 URI作为断言中的AuthnContextClassRef匹配失败则返回空串最终由StringUtils.defaultIfBlank回落到默认值。此外buildDefaultAuthenticationContextClassSamlProfileAuthnContextClassRefBuilder.java#L139-L151会在未请求特定上下文时执行同样的属性反查用断言中 MFA 上下文属性值去匹配映射的value侧命中的key即作为输出上下文。单元测试 SamlProfileAuthnContextClassRefBuilderTests 覆盖了这些分支端到端层面delegated-login-saml2-sp-saml2-idp-contextclass-redirect 场景 中的示例服务定义也使用了该字段可用于观察委托登录下上下文类的流转。4. 多因子认证MFA从请求上下文到 MFA Profile原文档最后一段指出CAS 可以按配置属性把 SAML2 认证请求中的认证上下文类映射到 MFA 配置文件且SAML2 认证请求必须正确签名MFA 触发器才会识别相应 Profile。这一约束在 SamlIdPMultifactorAuthenticationTrigger 中有明确的源码对应isMulifactorAuthenticationActivatedForAuthnRequestL97-L124先从SamlIdPSessionManager取回会话中缓存的AuthnRequest仅当注册服务是SamlRegisteredService且通过isAuthnRequestSigned校验后才把请求中的AuthnContextClassRef与authentication-context-class-mappings的键集求交命中后把映射值如mfa-duo交给MultifactorAuthenticationUtils.resolveProvider解析为对应的 MFA 提供者 Bean签名校验isAuthnRequestSignedL126-L144接受三种已签名证据之一请求对象自身authnRequest.isSigned()或绑定层消息签名SAMLBindingSupport.isMessageSigned、对等实体已通过认证SAMLPeerEntityContext.isAuthenticated、或经SamlObjectSignatureValidator.verifySamlProfileRequest依据 SP 元数据完成 XML 签名验证。任何一项都不成立MFA 触发直接不生效——这就是文档强调请求必须签名的原因防止未签名请求通过伪造上下文类名诱导 CAS 走 MFA 或跳过 MFA。除请求级触发外isMulifactorAuthenticationActivatedForMetadataL54-L78提供了一条元数据兜底路径当 SP 请求未显式要求上下文时检查 SP 元数据中的shibboleth:DefaultAuthenticationMethods实体属性是否命中映射键集命中则按元数据声明的默认认证方法解析 MFA 提供者。配置侧只需保证映射属性非空例如按 Javadoc 示例cas.authn.saml-idp.core.context.authentication-context-class-mappingsurn:oasis:names:tc:SAML:2.0:ac:classes:Password-mfa-duo触发器的supports方法同样依赖该映射非空、且当前 SAML 会话中存在携带RequestedAuthnContext的AuthnRequest或 SP 元数据命中shibboleth:DefaultAuthenticationMethods才声明支持见 SamlIdPMultifactorAuthenticationTrigger.java#L146-L175。5. 小结与参考服务字段requiredAuthenticationContextClass拥有最高优先级字面量/SpEL 直接覆写响应脚本形式file://外部脚本或groovy {...}内嵌脚本动态计算未配置服务字段时CAS 依次尝试SP 请求上下文 × 映射表 × 断言 MFA 属性三方匹配失败则落到default-authentication-context-class默认PasswordProtectedTransportMFA 触发以映射表非空为前提且仅接受签名合法的 SAML2 认证请求委托认证场景下同一张映射表可充当上下文类翻译器。关键源码与文档索引内容路径认证上下文构建器SamlProfileAuthnContextClassRefBuilder.javaMFA 触发器签名校验SamlIdPMultifactorAuthenticationTrigger.java配置模型映射/默认值SamlIdPAuthenticationContextProperties.java服务字段定义SamlRegisteredService.javaMFA 属性名默认值MultifactorAuthenticationCoreProperties.java原始文档Configuring-SAML2-AuthnContextClass.md构建器单元测试SamlProfileAuthnContextClassRefBuilderTests.java赞分享后端认证鉴权单点登录【免费下载链接】casApereo CAS - Identity Single Sign On for all earthlings and beyond.项目地址https://gitcode.com/gh_mirrors/ca/cas点击查看免费下载相关推荐Apereo CAS 基于 Groovy 脚本的认证策略Authentication Policy配置指南Apereo CAS 基于 Groovy 脚本的认证策略Authentication Policy配置指南 导读 本文围绕 Apereo CAS 的 cas后端认证鉴权单点登录Apereo CAS 基于 Groovy 脚本的灵活认证Groovy Authentication实战指南Apereo CAS 基于 Groovy 脚本的灵活认证Groovy Authentication实战指南 导读 本文介绍 Apereo CAS 中一种高度后端认证鉴权单点登录Apereo CAS中SAML2 NameID配置详解Apereo CAS中SAML2 NameID配置详解 什么是NameID 在SAML协议中NameID是一个核心概念它代表了认证主体用户的唯一标识符。后端认证鉴权单点登录上一篇卸载Spicetify Bloom主题的正确姿势完全指南与数据清理下一篇XO配置模块化创建可复用配置模块的最佳实践创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表