gpt4 book ai didi

java - 将 BinarySecurityToken 添加到 cxf header

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:07:28 35 4
gpt4 key购买 nike

我一直在为这个问题绞尽脑汁,但就是无法让它发挥作用。我有一个我调用的 Web 服务,它生成一个安全 token ,然后需要将其传递给 SOAP header 内的后续服务调用。我让那部分工作得很好,但标题部分让我感到困惑(我使用 cxf wsdl2java 生成了客户端)。这是应该添加的部分:

<wsse:BinarySecurityToken ValueType="XXXX" EncodingType="wsse:Base64Binary" wsu:Id="SecurityToken">
My token
</wsse:BinarySecurityToken>

我试过像这样使用 WSS4JOutInterceptor:

Endpoint endpoint = client.getEndpoint();
Map<String, Object> outProps = new HashMap<String, Object>();
outProps.put("SecurityToken", MY-TOKEN);
endpoint.getOutInterceptors().add(new WSS4JOutInterceptor(outProps));

但这没有用。我尝试像这样直接将它添加到标题中(根据 this question ):

List<Header> headers = new ArrayList<Header>();
SOAPFactory sf = SOAPFactory.newInstance();
SOAPElement authElement = sf.createElement(new QName(null, "wsse:BinarySecurityToken"));
authElement.setAttribute("ValueType", "XXXX");
authElement.setAttribute("EncodingType", "wsse:Base64Binary");
authElement.setAttribute("wsu:Id", "SecurityToken");
authElement.addTextNode(MY-TOKEN);
SoapHeader tokenHeader = new SoapHeader(
new QName(null, "wsse:BinarySecurityToken"), authElement);
headers.add(tokenHeader);
((BindingProvider) service).getRequestContext().put(Header.HEADER_LIST, headers);

看起来还不错

<soap:Header><BinarySecurityToken EncodingType="wsse:Base64Binary" ValueType="XXXX" wsu:Id="SecurityToken">MY-TOKEN</BinarySecurityToken></soap:Header>

虽然 BinarySecurityToken 部分缺少 wsse: 前缀,但调用失败。

有没有人得到类似工作的东西——还是我做的完全错了?

最佳答案

@zengr 是的,我终于弄明白了,它缺少命名空间,所以我所做的是:

private static final String XMLNS_WSU = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd";
private static final String XSD_WSSE = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd";

final List<Header> headers = new ArrayList<Header>();
final SOAPFactory sf = SOAPFactory.newInstance();
final SOAPElement securityElement = sf.createElement("Security", "wsse", XSD_WSSE);
final SOAPElement authElement = sf.createElement("BinarySecurityToken", "wsse", XSD_WSSE);
authElement.setAttribute("ValueType", "WASP");
authElement.setAttribute("EncodingType", "wsse:Base64Binary");
authElement.setAttribute("wsu:Id", "SecurityToken");
authElement.addAttribute(new QName("xmlns:wsu"), XMLNS_WSU);
authElement.addTextNode(StringUtils.replace(SessionToken.getEncodedSessionToken(), "\n", ""));
securityElement.addChildElement(authElement);
final SoapHeader securityHeader = new SoapHeader(
new QName(null, "Security"), securityElement);
headers.add(securityHeader);
((BindingProvider) interactiveService).getRequestContext().put(Header.HEADER_LIST, headers);

这就成功了

关于java - 将 BinarySecurityToken 添加到 cxf header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5679547/

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