如何在SAMLEntryPoint中设置RelayState

时间:2017-03-14 05:59:54

标签: spring spring-mvc single-sign-on spring-saml

我的EntryPoint类如下所示,但是在任何一个例子中,在relaystate中设置的确切值是什么。我想在验证之前获取所请求的URL参数值。

import org.opensaml.saml2.metadata.provider.MetadataProviderException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.saml.SAMLEntryPoint;
import org.springframework.security.saml.context.SAMLMessageContext;
import org.springframework.security.saml.websso.WebSSOProfileOptions;

public class SamlLoginEntryPoint extends SAMLEntryPoint{

    protected WebSSOProfileOptions getProfileOptions(SAMLMessageContext context, AuthenticationException exception) throws MetadataProviderException {

        System.out.println("inside entrypoint");
        WebSSOProfileOptions ssoProfileOptions;
        if (defaultOptions != null) {
            System.out.println("in if");
            ssoProfileOptions = defaultOptions.clone();
            ssoProfileOptions.setRelayState("");
            System.out.println("relaystate:"+ssoProfileOptions.getRelayState());
        } else {
            System.out.println("in else");
            ssoProfileOptions = new WebSSOProfileOptions();
            ssoProfileOptions.setRelayState("");
        }

        return ssoProfileOptions;

    }

2 个答案:

答案 0 :(得分:1)

@Dalu:我不确定这对比赛条件是否安全。 SAMLEntryPoint通常是单例bean。

请考虑以下情况:User A要使用URL X登录,但同时User BURL Y登录(在A称为commence()之后,但在他打电话给getProfileOptions之前。

解决方案是不将private String relayState;定义为全局变量,而是使用基于会话的存储。

答案 1 :(得分:-1)

重写SAMLEntryPoint类的begin()方法并从请求对象获取请求参数。

所以我的实现看起来像这样:

public class CustomMFASamlEntryPoint 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("request-param-for-relaystate"));
       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;
   }
}