gpt4 book ai didi

java - 使用 java 的客户端的 cxf 安全 header

转载 作者:行者123 更新时间:2023-12-02 03:31:05 24 4
gpt4 key购买 nike

我的要求是实现一种通过使用传入的用户名、密码生成 ws 安全 header 的方法。

因此,有人可以通过提供用户名和密码从 xslt 调用我的方法,并且我的方法应该能够返回安全 header ,并且他们还可以将此安全 header 附加到肥皂请求中以调用第三方 Web 服务。

我正在寻找可以通过用户名和密码生成soap安全 header 的api。

我发现 WSS4JOutInterceptor 需要端口和服务信息,但就我而言,我只有 2 个参数(用户名、密码)。

请建议除了创建 SoapEnvelop 并向其添加安全元素之外是否还有其他 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>

最佳答案

您可以使用WSS4J生成安全 header

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

结果将与此类似。在您方便的时候参数化 WSSecUsernameTokenWSSecHeader 代码

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

关于java - 使用 java 的客户端的 cxf 安全 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38082271/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com