使用java的客户端的cxf安全标头

使用java的客户端的cxf安全标头,java,web-services,cxf,ws-security,usernametoken,Java,Web Services,Cxf,Ws Security,Usernametoken,我的要求是实现一种方法,通过使用传入的用户名、密码生成ws-security头 因此,有些人可以通过提供用户名和密码从xslt调用我的方法,我的方法应该能够返回安全头,而且他们可以在soap请求中附加此安全头以调用第三方web服务。 我正在寻找api,它可以生成soap安全头采取用户名和密码 我找到了WSS4JOutInterceptor,它需要端口和服务信息,但在我的例子中,我只有2个参数(用户名、密码) 请建议除了创建SOAPEnvelope并向其添加安全元素之外,是否还有其他api/方法

我的要求是实现一种方法,通过使用传入的用户名、密码生成ws-security头

因此,有些人可以通过提供用户名和密码从xslt调用我的方法,我的方法应该能够返回安全头,而且他们可以在soap请求中附加此安全头以调用第三方web服务。

我正在寻找api,它可以生成soap安全头采取用户名和密码

我找到了WSS4JOutInterceptor,它需要端口和服务信息,但在我的例子中,我只有2个参数(用户名、密码)

请建议除了创建SOAPEnvelope并向其添加安全元素之外,是否还有其他api/方法

<oas:Security xmlns:oas="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">     <oas:UsernameToken xmlns:oas1="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" oas1:Id="UsernameToken-1">      <oas:Username> lakshmi </oas:Username><oas:Nonce EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">MTQ2NzA5NTg3MjM5Mw==</oas:Nonce>       <oas:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest">uSlFkVhDynZoCXFojlM1w4UrJYY=</oas:Password><oas1:Created>2016-06-28T06:37:52.425Z</oas1:Created></oas:UsernameToken></oas:Security>
lakshmi MTQ2NzA5NTg3MjM5Mw==uSlFkVhDynZoCXFojlM1w4UrJYY=2016-06-28T06:37:52.425Z
您可以使用生成安全标头

 public Node buildSecurityHeader(String username, String password) 
        throws WSSecurityException, ParserConfigurationException, SAXException, IOException{

    //XML Document builder with a root node
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    DocumentBuilder builder = factory.newDocumentBuilder();
    InputSource inStream = new InputSource();
    inStream.setCharacterStream(new StringReader("<root></root>"));
    Document document = builder.parse(inStream);

    //<wsse:UsernameToken>
    WSSecUsernameToken usernametoken = new WSSecUsernameToken();
    usernametoken.setPasswordType(WSConstants.PASSWORD_DIGEST);
    usernametoken.setUserInfo(username, password);

    //<wsse:Security>
    WSSecHeader secHeader = new WSSecHeader(document);
    secHeader.insertSecurityHeader();

    //Generates the Document with <root><Header><wsse:Security>...
    usernametoken.build(document, secHeader);

    //Extract the desired node
    Node securityNode = document.getElementsByTagName("wsse:Security").item(0);

    return securityNode;

}
并以这种方式使用它

 String securityHeader = nodeToString(buildSecurityHeader(username,password));
结果将与此类似。在方便的时候对
WSSecUsernameToken
WSSecHeader
代码进行参数化

<wsse:Security xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" soapenv:mustUnderstand="1">
    <wsse:UsernameToken wsu:Id="UsernameToken-39dba965-c4a8-4b2d-826e-ade8c0931f3f">
       <wsse:Username>username</wsse:Username>
       <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest">BxJH0G5PzPfBFbBGimF0bq3vjsY=</wsse:Password>
       <wsse:Nonce EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">iaO1xilL6qfuN2apbSdfPQ==</wsse:Nonce>
       <wsu:Created>2016-06-30T07:17:26.552Z</wsu:Created>
    </wsse:UsernameToken>
</wsse:Security>

用户名
BxJH0G5PzPfBFbBGimF0bq3vjsY=
iaO1xilL6qfuN2apbSdfPQ==
2016-06-30T07:17:26.552Z
您可以使用生成安全标头

 public Node buildSecurityHeader(String username, String password) 
        throws WSSecurityException, ParserConfigurationException, SAXException, IOException{

    //XML Document builder with a root node
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    DocumentBuilder builder = factory.newDocumentBuilder();
    InputSource inStream = new InputSource();
    inStream.setCharacterStream(new StringReader("<root></root>"));
    Document document = builder.parse(inStream);

    //<wsse:UsernameToken>
    WSSecUsernameToken usernametoken = new WSSecUsernameToken();
    usernametoken.setPasswordType(WSConstants.PASSWORD_DIGEST);
    usernametoken.setUserInfo(username, password);

    //<wsse:Security>
    WSSecHeader secHeader = new WSSecHeader(document);
    secHeader.insertSecurityHeader();

    //Generates the Document with <root><Header><wsse:Security>...
    usernametoken.build(document, secHeader);

    //Extract the desired node
    Node securityNode = document.getElementsByTagName("wsse:Security").item(0);

    return securityNode;

}
并以这种方式使用它

 String securityHeader = nodeToString(buildSecurityHeader(username,password));
结果将与此类似。在方便的时候对
WSSecUsernameToken
WSSecHeader
代码进行参数化

<wsse:Security xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" soapenv:mustUnderstand="1">
    <wsse:UsernameToken wsu:Id="UsernameToken-39dba965-c4a8-4b2d-826e-ade8c0931f3f">
       <wsse:Username>username</wsse:Username>
       <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest">BxJH0G5PzPfBFbBGimF0bq3vjsY=</wsse:Password>
       <wsse:Nonce EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">iaO1xilL6qfuN2apbSdfPQ==</wsse:Nonce>
       <wsu:Created>2016-06-30T07:17:26.552Z</wsu:Created>
    </wsse:UsernameToken>
</wsse:Security>

用户名
BxJH0G5PzPfBFbBGimF0bq3vjsY=
iaO1xilL6qfuN2apbSdfPQ==
2016-06-30T07:17:26.552Z

使用cxf时使用xslt生成请求的原因是什么?您的方法的输出应该是什么?XML字符串?请添加您真正需要的更多信息。@Frank您是wright。我的方法的输出应该是xmlString。在我们的项目中,我们仅通过xslt将传入的请求(XML)转换为soap格式。可能我在文章中传达了错误,我们没有使用任何CXF将传入的XML转换为soap格式。当前的要求是通过获取用户名和密码来创建安全标头。所以您需要为出站连接生成soap标头,而不必担心正文是如何生成的:xslt等等,不是吗?可能您不需要CXF。如果您知道,请添加所需标题格式的示例,或者至少添加您的管理员设置的二进制安全令牌限制的示例server@pedrofb这正是我要找的。Soap主体和其他头部分由XSLT负责。我编辑了问题广告,添加了预期的标题格式。请看一下。(我不知道如何链接文件,所以我添加了问题)。目前,我们硬编码了所有的头,并使用Base64编码格式创建密码摘要、Nonce等,然后将其附加到安全头并返回该字符串。我想用一些api创建这些东西。您好@lkredy1231,您检查过答案了吗?使用cxf时使用xslt生成请求的原因是什么?您的方法的输出应该是什么?XML字符串?请添加您真正需要的更多信息。@Frank您是wright。我的方法的输出应该是xmlString。在我们的项目中,我们仅通过xslt将传入的请求(XML)转换为soap格式。可能我在文章中传达了错误,我们没有使用任何CXF将传入的XML转换为soap格式。当前的要求是通过获取用户名和密码来创建安全标头。所以您需要为出站连接生成soap标头,而不必担心正文是如何生成的:xslt等等,不是吗?可能您不需要CXF。如果您知道,请添加所需标题格式的示例,或者至少添加您的管理员设置的二进制安全令牌限制的示例server@pedrofb这正是我要找的。Soap主体和其他头部分由XSLT负责。我编辑了问题广告,添加了预期的标题格式。请看一下。(我不知道如何链接文件,所以我添加了问题)。目前,我们硬编码了所有的头,并使用Base64编码格式创建密码摘要、Nonce等,然后将其附加到安全头并返回该字符串。我想用一些api创建这些东西。嗨@lkredy1231,你检查过答案了吗?