Java 生成有效SOAP头的OpenSAML实现

Java 生成有效SOAP头的OpenSAML实现,java,saml-2.0,opensaml,Java,Saml 2.0,Opensaml,我打算通过SAML令牌策略创建SOAP头。我写了一个方法如下: void addSamlTokenClientPolicy(SOAPMessageContext context){ SOAPEnvelope envelope = context.getMessage().getSOAPPart().getEnvelope(); SOAPHeader header = envelope.getHeader(); if (header

我打算通过SAML令牌策略创建SOAP头。我写了一个方法如下:

        void addSamlTokenClientPolicy(SOAPMessageContext context){
        SOAPEnvelope envelope = context.getMessage().getSOAPPart().getEnvelope();
        SOAPHeader header = envelope.getHeader();
        if (header == null) {
            header = envelope.addHeader();
        }

        SOAPFactory factory = SOAPFactory.newInstance();
        SOAPElement securityElem = factory.createElement(SoapSecurityHeaderConstants.SecurityEle,
        SoapSecurityHeaderConstants.prefix, SoapSecurityHeaderConstants.uri);

        String samlMetadataFile = "/samlMetadata.xml";

        // Initialize the library
        DefaultBootstrap.bootstrap();

        // Get Parser Pool Manager
        BasicParserPool ppMgr = new BasicParserPool();
        ppMgr.setNamespaceAware(true);

        // Parse Metadata file
        InputStream in = CZSoapSecurityHandler.class.getResourceAsStream(samlMetadataFile);
        Document inDoc = ppMgr.parse(in);
        Element metadataRoot = inDoc.getDocumentElement();

        // Get appropriate unmarshaller
        UnmarshallerFactory unmarshallerFactory = Configuration.getUnmarshallerFactory();
        Unmarshaller unmarshaller = unmarshallerFactory.getUnmarshaller(metadataRoot);

        XMLObject xmlObj = unmarshaller.unmarshall(metadataRoot);

        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        DOMSource source = new DOMSource(xmlObj.getDOM());
        StreamResult result = new StreamResult(new StringWriter());
        transformer.transform(source, result);
        String strObject = result.getWriter().toString();

        securityElem.setTextContent(strObject);
        header.addChildElement(securityElem);
}

文件
samlMetadata.xml
的内容如下:

        <saml:Assertion Version="2.0"
            ID="SAML-8z1f2F9fQ1EjegMIuH11Wg22" IssueInstant="2015-12-17T11:15:50Z"
            xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion">
            <saml:Issuer>xxx</saml:Issuer>
            <saml:Subject>
                <saml:NameID
                    Format="urn:oasis:names:tc:SAML:1.1:nameid-format:unspecified">username</saml:NameID>
                <saml:SubjectConfirmation
                    Method="urn:oasis:names:tc:SAML:2.0:cm:sender-vouches" />
            </saml:Subject>
            <saml:AuthnStatement
                AuthnInstant="2015-12-17T11:15:50Z">
                <saml:AuthnContext>
                    <saml:AuthnContextClassRef>urn:oasis:names:tc:SAML:2.0:ac:classes:Password
                    </saml:AuthnContextClassRef>
                </saml:AuthnContext>
            </saml:AuthnStatement>
        </saml:Assertion>

xxx
用户名
urn:oasis:name:tc:SAML:2.0:ac:classes:Password
这里的问题是,它抛出以下错误:

客户端从服务器接收到SOAP错误:InvalidSecurity:处理WS-Security标头时出错


我很确定这个错误是由于代码
securityElem.setTextContent(strObject)
造成的,我正在尝试一种解决方法,设置从XML解析的文本,而不是添加子元素。请帮忙。

我想我找到了答案。 代码中唯一缺少的是封送员。在代码中添加以下行以将xmlObject填充到SOAPEnvelope中:

        XMLObject xmlObj = unmarshaller.unmarshall(metadataRoot);
        MarshallerFactory ms = Configuration.getMarshallerFactory();
        Marshaller mshaller = ms.getMarshaller(xmlObj);
        mshaller.marshall(xmlObj, securityElem);
        header.addChildElement(securityElem);