2016-06-28 59 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>