【问题标题】:Add request parameter to SAML request using Spring Security SAML使用 Spring Security SAML 将请求参数添加到 SAML 请求
【发布时间】:2015-05-29 01:26:52
【问题描述】:

我需要在 SAML 请求中添加请求参数(例如 locale=en),以便让登录页面显示正确的语言。我该怎么做?

我尝试将属性添加到作为参数发送到开始方法 (SamlEntryPoint) 的 HttpServletRequest 中,但这似乎不起作用。

有什么建议吗?

【问题讨论】:

    标签: spring-saml


    【解决方案1】:

    SAML 提供了一种标准机制来扩展身份验证请求中发送的内容 - Extensions 元素。

    为了使用它,您需要与您的 IDP 就您发送的数据和格式进行协调。在 Spring SAML 中,您可以通过扩展类 WebSSOProfileImpl 来自定义其内容,例如:

    package com.v7security.saml;
    
    import org.opensaml.common.SAMLException;
    import org.opensaml.saml2.common.Extensions;
    import org.opensaml.saml2.common.impl.ExtensionsBuilder;
    import org.opensaml.saml2.core.AuthnRequest;
    import org.opensaml.saml2.metadata.AssertionConsumerService;
    import org.opensaml.saml2.metadata.SingleSignOnService;
    import org.opensaml.saml2.metadata.provider.MetadataProviderException;
    import org.opensaml.xml.schema.XSAny;
    import org.opensaml.xml.schema.impl.XSAnyBuilder;
    import org.springframework.security.saml.context.SAMLMessageContext;
    import org.springframework.security.saml.websso.WebSSOProfileImpl;
    import org.springframework.security.saml.websso.WebSSOProfileOptions;
    
    /**
     * Class adds additional extensions element to the AuthnRequest sent to IDP.
     */
    public class WebSSOProfile extends WebSSOProfileImpl {
    
        @Override
        protected AuthnRequest getAuthnRequest(SAMLMessageContext context, WebSSOProfileOptions options, AssertionConsumerService assertionConsumer, SingleSignOnService bindingService) throws SAMLException, MetadataProviderException {
            AuthnRequest authnRequest = super.getAuthnRequest(context, options, assertionConsumer, bindingService);
            authnRequest.setExtensions(buildExtensions());
            return authnRequest;
        }
    
        protected Extensions buildExtensions() {
    
            XSAny languageClass = new XSAnyBuilder().buildObject("http://www.v7security.com/schema/2015/04/request", "RequestLanguage", "req");
            languageClass.setTextContent("urn:v7security:request:lang:english");
    
            Extensions extensions = new ExtensionsBuilder().buildObject();
            extensions.getUnknownXMLObjects().add(languageClass);
            return extensions;
    
        }
    
    }
    

    另一种选择是在relayState 中发送数据,这是SP 可以发送给IDP 并期望它被反弹回来的一条信息(通常是SP 的状态)。该值应该对 IDP 不透明,但它当然可以按您想要的方式处理它。有关设置继电器状态的详细信息,请参阅chapter on SP initialized SSO in the manual

    HttpRequest 对象上设置请求参数预计不会产生任何结果,Spring SAML 不会以任何方式自动传达这些。

    可以通过扩展类HTTPRedirectDeflateEncoder 和覆盖方法buildRedirectURL 为使用HTTP 重定向绑定发送的请求添加HTTP 参数。然后可以将新类提供给HTTPRedirectDeflateBinding 的构造函数并替换为securityContext.xml 的bean redirectBinding,方式如下:

    <bean id="redirectBinding" class="org.springframework.security.saml.processor.HTTPRedirectDeflateBinding">
        <constructor-arg>
            <bean class="org.opensaml.saml2.binding.decoding.HTTPRedirectDeflateDecoder">
                <constructor-arg name="pool" ref="parserPool"/>
            </bean>
        </constructor-arg>
        <constructor-arg>
            <bean class="com.custom.HTTPRedirectDeflateEncoder"/>
        </constructor-arg>
    </bean>
    

    【讨论】:

    • 感谢您的回答。当我从我们的 IdP 阅读文档时,似乎只需添加请求参数即可发送语言环境。不幸的是,文档是用挪威语编写的,但您应该很容易找到有关语言环境的示例:begrep.difi.no/ID-porten/SAMLAuthnRequest (Eksempel på overføring av locale)
    • 你是对的,这就是他们似乎正在使用的。我已经更新了答案以涵盖自定义,允许您添加此类参数。
    • 谢谢,这似乎运作良好。由于我们需要动态更改语言/区域设置,因此从超类访问 HttpServletRequest 或 Locale 会很棒。
    • 感谢弗拉迪,它成功了。我想知道是否还有一种标准方法来处理 IDP 发送的额外参数(在 IDP 发起的 SSO 中)。
    • @VladimírSchäfer,我在我的项目中创建了 WebSSOProfile 类。但它似乎没有被调用。我已经把调试点。有什么我需要做的设置吗?一些配置或类似的东西。我正在使用 spring-saml2-core-1.0.2-RELEASE
    猜你喜欢
    • 1970-01-01
    • 2015-05-19
    • 2011-12-11
    • 2015-09-27
    • 2015-01-21
    • 1970-01-01
    • 2017-02-11
    • 2018-07-11
    • 1970-01-01
    相关资源
    最近更新 更多