是否可以从客户端向所有传出的 cxf 连接添加标头。
使用 Spring 3.0 和 CXF 2.6.0
我想在这里给我两分钱。我在我的帖子中解决了同样的情况 -
http://saurzcode.in/2014/05/08/adding-header-to-soap-request-using-cxf-2/
弹簧配置:-
<jaxws:client id="mywebServiceClient"
serviceClass="com.saurzcode.TestService"
address="http://saurzcode.com:8088/mockTestService">
<jaxws:binding>
<soap:soapBinding version="1.2" mtomEnabled="true" />
</jaxws:binding>
</jaxws:client>
<cxf:bus>
<cxf:outInterceptors>
<bean class="com.saurzcode.ws.caller.SoapHeaderInterceptor" />
</cxf:outInterceptors>
</cxf:bus>
CXF 拦截器 -
public class SoapHeaderInterceptor extends AbstractSoapInterceptor {
public SoapHeaderInterceptor() {
super(Phase.POST_LOGICAL);
}
@Override
public void handleMessage(SoapMessage message) throws Fault {
List<Header> headers = message.getHeaders();
TestHeader testHeader = new TestHeader();
JAXBElement<TestHeader> testHeaders = new ObjectFactory()
.createTestHeader(testHeader);
try {
Header header = new Header(testHeaders.getName(), testHeader,
new JAXBDataBinding(TestHeader.class));
headers.add(header);
message.put(Header.HEADER_LIST, headers);
} catch (JAXBException e) {
e.printStackTrace();
}
}
我已经知道两种方法。一种是创建 SOAP 处理程序并将其注册为JAX-WS
Spring 配置中的处理程序。
在此处查看我的答案如何创建 SOAP 处理程序。当您希望标头出现在响应(传出请求)中时,请不要忘记您需要检查消息是否出站,这样的事情会做:
Boolean outbound = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
if (outbound) {
//Modify your header.
}
另一种可能更简单的方法。将标头直接放入 CXF 响应上下文中。请注意,此示例仅是概念证明,我不知道您在响应中需要凭据的实际情况。它将显示如何将用户凭据对象添加到标题中,您必须根据需要对其进行修改。
private void modifyResponse(String username, String password) {
UserCredentials authHeader = new UserCredentials();
authHeader.setUsername(username);
authHeader.setPassword(password);
ArrayList<Header> headers = new ArrayList<Header>(1);
try {
Header soapHeader = new Header(
new QName("http://yournamespaceuri.com/something", "UserCredentials"),
authHeader,
new JAXBDataBinding(UserCredentials.class));
headers.add(soapHeader);
} catch (JAXBException ex) {
LOGGER.error("Exception trying to serialize header: {}", ex);
}
((BindingProvider) proxy).getResponseContext().put(Header.HEADER_LIST, headers);
}
此方法需要在您的客户端请求之后立即调用。
我就是这样做的
春天.xml
<import resource="classpath:META-INF/cxf/cxf.xml" />
<bean id="cxf" class="org.apache.cxf.bus.spring.SpringBus">
<property name="outInterceptors">
<list>
<ref bean="headerInterceptor"/>
</list>
</property>
<property name="inInterceptors">
<list>
<ref bean="headerInterceptor"/>
</list>
</property>
</bean>
<bean id="headerInterceptor" class="logging.Interceptor"/>
拦截器:
public class UUIDHeaderInterceptor extends AbstractPhaseInterceptor {
private static final Logger logger = LoggerFactory.getLogger(UUIDHeaderInterceptor.class);
public UUIDHeaderInterceptor() {
super(Phase.RECEIVE);
}
@Override
public void handleMessage(Message message) throws Fault {
Map<String, List<String>> headers = (Map<String, List<String>>) message.get(Message.PROTOCOL_HEADERS);
headers.put(REQUEST_ID_ATTRIBUTE_NAME, Arrays.asList(new String[]{"TEST"}));
}
}
@Override
public void handleFault(Message message) {
handleMessage(message);
}