0

我目前正在从 Spring Security SAML Extension 迁移到 Spring Security SAML2,并且用例需要在 Extensions 元素中发送语言代码。

使用 Spring Security SAML Extension,这是通过以下方式完成的:

  1. 扩展 SAMLEntryPoint 并将语言环境作为 relayState 存储到 SAMLMessageContext,如下所示:
public class CustomSAMLEntryPoint extends SAMLEntryPoint {

    private String relayState;

    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authenticationException) throws IOException, ServletException {
        //read your request parameter
        setRelayState(request.getParameter("locale"));
        super.commence(request, response, authenticationException);
    }

    @Override
    protected WebSSOProfileOptions getProfileOptions(SAMLMessageContext samlMessageContext, AuthenticationException authenticationException) throws MetadataProviderException {
        //set the relayState to your SAML message context
        samlMessageContext.setRelayState(getRelayState());
        return super.getProfileOptions(samlMessageContext, authenticationException);
    }

    private void setRelayState(String relayState) {
        this.relayState = relayState;
    }

    private String getRelayState() {
        return relayState;
    }
}
  1. 扩展 WebSSOProfileImpl 并使用先前设置的 relayState 值生成 Extensions -元素:
public class CustomWebSSOProfileImpl 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(context.getRelayState()));
        return authnRequest;
    }
}

如何使用 Spring Security Core SAML2 完成相同的功能?有没有与使用 SAMLMessageContext 和 relayState 类似的方法?

我可以自定义 AuthenticationEntryPoint 以及身份验证请求创建,但似乎无法在这两者之间移动语言环境。

     public AuthenticationEntryPoint authenticationEntryPoint() {
        final AuthenticationEntryPoint authenticationEntryPoint = new LoginUrlAuthenticationEntryPoint(
                "/saml2/authenticate/sp");
        return (request, response, exception) -> {
            String locale = request.getParameter("locale");
            // Where shoud locale be stored???
            authenticationEntryPoint.commence(request, response, exception);
        };
    }
    @Bean
    public Saml2AuthenticationRequestFactory authenticationRequestFactory() {
        final OpenSamlAuthenticationRequestFactory authenticationRequestFactory = new OpenSamlAuthenticationRequestFactory();
        authenticationRequestFactory.setAuthenticationRequestContextConverter(context -> {
            final AuthnRequest request = new AuthnRequestBuilder().buildObject();
            request.setAssertionConsumerServiceURL(context.getAssertionConsumerServiceUrl());
            request.setDestination(context.getDestination());
            request.setID("A" + UUID.randomUUID());
            request.setIssueInstant(new DateTime());
            final Issuer issuer = new IssuerBuilder().buildObject();
            issuer.setValue(context.getIssuer());
            request.setIssuer(issuer);
            // Where can locale be read from???
            request.setExtensions(buildLanguageExtensions(???);
            return request;
        });
        return authenticationRequestFactory;
    }
4

0 回答 0