是否可以从客户端向所有传出的cxf连接添加标头。
使用Spring 3.0和CXF 2.6.0
答案 0 :(得分:6)
我想在这里给我两分钱。我在我的帖子中解决了同样的案例 -
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();
}
}
答案 1 :(得分:2)
我已经知道两种方法。一种是创建SOAP Handler并在Spring配置中将其注册为JAX-WS
处理程序。
检查my answer here如何创建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);
}
此方法需要在客户请求后立即调用。
答案 2 :(得分:1)
这就是我的做法,
Spring.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);
}