2016-06-28 63 views
0

我的要求是实现一种通过使用传入的用户名和密码来生成ws安全头文件的方法。使用java的客户端的cxf安全头文件

所以有人可以通过提供用户名和密码从xslt调用我的方法,我的方法应该能够返回安全标头,并进一步他们可以在肥皂请求中附加这个安全标头来调用第三方Web服务。

我正在寻找可以通过输入用户名和密码生成肥皂安全标头的api。

我发现WSS4JOutInterceptor需要端口和服务信息,但在我的情况下,我只有2个参数(用户名,PassWord)。

请建议是否有其他的API /方法比创建SoapEnvelop和添加安全元素?

<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> 
+0

什么IST使用CXF时建立与XSLT您的请求的原因参数化的WSSecUsernameTokenWSSecHeader代码?你的方法的输出应该是什么?一个xmlstring?请添加更多信息,您确切需要。 – Frank

+0

@弗兰克你是赖特。我的方法的输出应该是xmlString。 在我们的项目中,我们只是通过xslt将传入请求(XML)转换为soap格式。可能是我在帖子中传递错误,我们没有使用任何CXF将传入的XML转换为SOAP格式。目前的要求是通过输入用户名和密码来创建安全头。 – lkreddy1231

+0

因此,您需要为出站连接生成肥皂头,而不用担心生成身体的方式:xslt等不是吗?可能你不需要CXF。如果您知道,请添加所需头文件格式的示例,或者至少您的服务器 – pedrofb

回答

1

您可以使用WSS4J生成安全头

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; 

} 

要打印的节点作为字符串使用此

public String nodeToString(Node node) throws TransformerFactoryConfigurationError, TransformerException { 
    StringWriter sw = new StringWriter(); 

    Transformer t = TransformerFactory.newInstance().newTransformer(); 
    t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); 
    t.setOutputProperty(OutputKeys.INDENT, "yes"); 
    t.transform(new DOMSource(node), new StreamResult(sw)); 
    return sw.toString(); 
} 

并以这种方式

String securityHeader = nodeToString(buildSecurityHeader(username,password)); 
使用

T他的结果将与此类似。在您方便的

<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>