使用OpenSaml将SAMLRequest解析为AuthnRequest,Unmarshaller为NULL

时间:2019-08-28 14:54:45

标签: java spring saml opensaml

我想将在HttpServletRequest中获取的SAMLRequest解析为AuthnRequest对象,以便获取其颁发者和其他属性。

使用OpenSaml版本

<dependency>
            <groupId>org.opensaml</groupId>
            <artifactId>opensaml-core</artifactId>
            <version>3.1.0</version>
        </dependency>
        private String receiveResponse(@RequestParam(name = "SAMLRequest") final String samlRequest, final Model model,
        final HttpServletRequest request, final HttpServletResponse response){

        final String encodedSAMLRequest = request.getParameter(EidasConstants.SAML_REQUEST);

        byte[] samlToken = EidasStringUtil.decodeBytesFromBase64(encodedSAMLRequest);
        //equal to base64.decode

        ByteArrayInputStream stream = new ByteArrayInputStream(samlToken);        
        DefaultBootstrap.bootstrap();

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        DocumentBuilder docBuilder = factory.newDocumentBuilder();
        Document samlDocument = docBuilder.parse(stream);
        Element samlElem = samlDocument.getDocumentElement();

        UnmarshallerFactory unmarshallerFactory = Configuration.getUnmarshallerFactory();
        Unmarshaller unmarshaller = unmarshallerFactory.getUnmarshaller(samlElem);
        Object requestXmlObj = unmarshaller.unmarshall(samlElem);
        AuthnRequest authRequest = (AuthnRequest) requestXmlObj;
        }

我拥有的元素就是这个

<samlp:AuthnRequest xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol" xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion" AssertionConsumerServiceURL=
"https://amis-dev.ulisboa.pt/nidp/saml2/spassertion_consumer" Consent="urn:oasis:names:tc:SAML:2.0:consent:unavailable" Destination="http://10.110.58.
100:8080/RequestReceiver" ForceAuthn="false" ID="iddVeu9K-BZRNMsife2aSbJ_SZUt8" IsPassive="false" IssueInstant="2019-08-28T14:33:33Z" ProtocolBinding=
"urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST" Version="2.0">
<saml:Issuer Format="urn:oasis:names:tc:SAML:2.0:nameid-format:entity">ULisboa</saml:Issuer>
<ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
<ds:SignedInfo>
<CanonicalizationMethod xmlns="http://www.w3.org/2000/09/xmldsig#" Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
<ds:SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"/>
<ds:Reference URI="#iddVeu9K-BZRNMsife2aSbJ_SZUt8">
<ds:Transforms>
<ds:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/>
<ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
</ds:Transforms>
<ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
<DigestValue xmlns="http://www.w3.org/2000/09/xmldsig#">DB9eyPJL2El5nj01uowV7AtskOuiqW+482q29Ox9nU4=</DigestValue>
</ds:Reference>
</ds:SignedInfo>
<SignatureValue xmlns="http://www.w3.org/2000/09/xmldsig#">
VgNtmtSJEHSY2dgHKNJbSDKocDZfMyIrxkjGkBLgWAKMRwC601C0OY9t1w0C5ZT+79pjbzVgHJ47
OxNrXqW0ayVuSQiFdw8pPAsNRsCXRkgPWUQf2so5XgvIAUZOd491sehRKzz6LNF9FhJWuNLXuNIl
joKu9Sn5G7OZXaynSFQ93Rph4uMYp/esu/tlO8d1toPpZS0phwlAEtQvoVgWRQBNyXdgzyulrorJ
Ogm3zP5U6F41wYhO9mb29rcEIuTUQUrOaNHNJ+f1BlJuffs4SCB2toKXKsuxwWmiDyQ0nMOr9RvA
WUy9nUmNhljuRwQoB1Wt31AvW4jX9yeZsi0XqA==
</SignatureValue>
<ds:KeyInfo>
<ds:X509Data>
<ds:X509Certificate>
MIIGvTCCBaWgAwIBAgIQDwGHVYp6wgGfBYR9UdgiRDANBgkqhkiG9w0BAQsFADBkMQswCQYDVQQG
EwJOTDEWMBQGA1UECBMNTm9vcmQtSG9sbGFuZDESMBAGA1UEBxMJQW1zdGVyZGF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9aoxwIm3w8LnikWCtv2lBWnXSCV2HaQ5UhjH+pjt5yrBgsDWPOtQF
6dJlIODiWR35BmBaAQOkF7Aapg==
</ds:X509Certificate>
</ds:X509Data>
</ds:KeyInfo>
</ds:Signature>
<samlp:NameIDPolicy Format="urn:oasis:names:tc:SAML:1.1:nameid-format:unspecified"/>
</samlp:AuthnRequest>

用于

Unmarshaller unmarshaller = unmarshallerFactory.getUnmarshaller(samlElem);

创建解组器,但解组器返回NULL。

获得的错误消息是

java.lang.NullPointerException: null
        at pt.ulisboa.ssobroker.controller.RequestReceiverController.receiveResponse(RequestReceiverController.java:100) ~[classes/:na]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_181]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_181]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_181]
        at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_181]
        at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190) ~[spring-web-5.1.8.RELEASE.jar:5.1.
8.RELEASE]
        at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138) ~[spring-web-5.1.8.RELEASE.
jar:5.1.8.RELEASE]
        at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
 ~[spring-webmvc-5.1.8.RELEASE.jar:5.1.8.RELEASE]
        at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:89
2) ~[spring-webmvc-5.1.8.RELEASE.jar:5.1.8.RELEASE]
        at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:797) ~[
spring-webmvc-5.1.8.RELEASE.jar:5.1.8.RELEASE]
        at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.1.8.
RELEASE.jar:5.1.8.RELEASE]
        at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1039) ~[spring-webmvc-5.1.8.RELEASE.jar:5.1.8.RELEASE]
        at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942) ~[spring-webmvc-5.1.8.RELEASE.jar:5.1.8.RELEASE]
        at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005) ~[spring-webmvc-5.1.8.RELEASE.jar:5.1.8.RELEASE
]
        at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:908) ~[spring-webmvc-5.1.8.RELEASE.jar:5.1.8.RELEASE]

所以最后我只想获取AuthnRequest对象,以便可以使用它,但是由于解组器为NULL,所以我无法获取它,我已经测试了所有其他步骤,直到出现正常情况为止,我只是不知道为什么解组器返回为NULL。

我尝试过使用shibboleth中的parsePool的方法,但无济于事。

AuthnRequest authRequest = (AuthnRequest) XMLObjectSupport.unmarshallFromInputStream(parserPool, stream);

但是我无法让shibboleth parserPool正确初始化

1 个答案:

答案 0 :(得分:0)

我认为当前您同时具有OpenSAML2和OpenSAML3依赖项。因此,首先通过使用mvn dependency:tree搜索来消除OpenSAML2依赖性,然后排除OpenSAML2依赖性。之后,除了问题中提到的依赖项之外,您还需要添加:

<dependency>
        <groupId>org.opensaml</groupId>
        <artifactId>opensaml-saml-impl</artifactId>
        <version>3.1.0</version>
</dependency>

然后修改方法如下:

private String receiveResponse(@RequestParam(name = "SAMLRequest") final String samlRequest, final Model model,
    final HttpServletRequest request, final HttpServletResponse response){

    InitializationService.initialize();

    final String encodedSAMLRequest = request.getParameter(EidasConstants.SAML_REQUEST);

    byte[] samlToken = EidasStringUtil.decodeBytesFromBase64(encodedSAMLRequest);
    //equal to base64.decode

    ByteArrayInputStream stream = new ByteArrayInputStream(samlToken);        
    DefaultBootstrap.bootstrap();

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    DocumentBuilder docBuilder = factory.newDocumentBuilder();
    Document samlDocument = docBuilder.parse(stream);
    Element samlElem = samlDocument.getDocumentElement();

    UnmarshallerFactory unmarshallerFactory = XMLObjectProviderRegistrySupport.getUnmarshallerFactory();
    Unmarshaller unmarshaller = unmarshallerFactory.getUnmarshaller(samlElem);
    Object requestXmlObj = unmarshaller.unmarshall(samlElem);
    AuthnRequest authRequest = (AuthnRequest) requestXmlObj;
}

并尝试一下。