gpt4 book ai didi

java - 向请求添加自定义 header 时出现 Axis 2 和 Rampart 问题

转载 作者:行者123 更新时间:2023-12-01 04:51:45 24 4
gpt4 key购买 nike

<soapenv:Envelope> 
<soapenv:Header>
<cor:india>test</cor:india>
</soapenv:Header>

<soapenv:Body>
.
.
</soapenv:Body>
</soapenv:Envelope>

OMFactory omFactory =OMAbstractFactory.getOMFactory();
OMNamespace omNamespace = omFactory.createOMNamespace("http://example.com/...", "cor");
OMElement header = omFactory.createOMElement("india", omNamespace);
header.setText("test");
stub._getServiceClient().addHeader(header);

我想将自定义 header 添加到使用 Axis 2 和城墙的肥皂请求中。但下面是我遇到的异常

Caused by: org.apache.rampart.RampartException: Error in extracting message properties
at org.apache.rampart.RampartMessageData.<init>(RampartMessageData.java:379)
at org.apache.rampart.MessageBuilder.build(MessageBuilder.java:61)
at org.apache.rampart.handler.RampartSender.invoke(RampartSender.java:65)
... 10 more
Caused by: org.apache.ws.security.WSSecurityException: Error in converting SOAP Envelope to Document; nested exception is:
java.lang.ClassCastException: org.apache.axiom.om.impl.llom.OMElementImpl cannot be cast to org.apache.axiom.soap.SOAPHeaderBlock
at org.apache.rampart.util.Axis2Util.getDocumentFromSOAPEnvelope(Axis2Util.java:191)
at org.apache.rampart.RampartMessageData.<init>(RampartMessageData.java:270)
... 12 more
Caused by: java.lang.ClassCastException: org.apache.axiom.om.impl.llom.OMElementImpl cannot be cast to org.apache.axiom.soap.SOAPHeaderBlock
at org.apache.rampart.util.Axis2Util.getDocumentFromSOAPEnvelope(Axis2Util.java:141)
... 13 more

最佳答案

如果您尝试使用此处提供的提示添加 header Example Custom Headers当您使用壁垒进行交战时,您可能会面临上述问题。

解决方案是在 META-INF 中添加您自己的 module.xml 并在您要添加自定义 header 的位置添加输出流

<module name="test" class="exampleClass">

<OutFlow>

<handler name="handle" class="exampleClass">
<order phase="Security" />
</handler>
</OutFlow>
</module>

================================================== =====================================

public class exampleClass AbstractHandler implements org.apache.axis2.modules.Module {

public InvocationResponse invoke(MessageContext ctx) throws AxisFault {

SOAPEnvelope env = ctx.getEnvelope();
SOAPHeader hdr = env.getHeader();

SOAPFactory factory = (SOAPFactory) env.getOMFactory();

OMNamespace ns = factory.createOMNamespace("http://google.com", "cor");

//SOAPHeader head = factory.createSOAPHeader(env);

hdr.addHeaderBlock("india", ns).setText("value here");

return InvocationResponse.CONTINUE;
}


public void applyPolicy(Policy arg0, AxisDescription arg1) throws AxisFault {
// TODO Auto-generated method stub

}

public boolean canSupportAssertion(Assertion arg0) {
// TODO Auto-generated method stub
return false;
}

public void engageNotify(AxisDescription arg0) throws AxisFault {
// TODO Auto-generated method stub

}

public void init(ConfigurationContext arg0, AxisModule arg1) throws AxisFault {
// TODO Auto-generated method stub

}

public void shutdown(ConfigurationContext arg0) throws AxisFault {
// TODO Auto-generated method stub

} }

================================================== ===================================

在客户端中,在城墙之前启用您的模块,例如

Options options = serviceClient.getOptions();
serviceClient.engageModule("test");

options.setProperty(RampartMessageData.KEY_RAMPART_POLICY,loadPolicy("policy.xml"));
serviceClient.engageModule("rampart");
..

..

关于java - 向请求添加自定义 header 时出现 Axis 2 和 Rampart 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14852843/

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