spring 向所有传出 CXF 请求添加标头
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18527863/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add Header To all outgoing CXF request
提问by Trind
Is it possible to add a Header to all outgoing cxf connections, from the client side.
是否可以从客户端向所有传出 cxf 连接添加 Header。
Using Spring 3.0 and CXF 2.6.0
使用 Spring 3.0 和 CXF 2.6.0
采纳答案by Trind
this is how i did,
我就是这样做的
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"/>
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);
}
回答by saurzcode
I would like to give my two cents here . I am solving the same case here in my post -
我想在这里给我两分钱。我在我的帖子中解决了同样的案例 -
http://saurzcode.in/2014/05/08/adding-header-to-soap-request-using-cxf-2/
http://saurzcode.in/2014/05/08/adding-header-to-soap-request-using-cxf-2/
Spring Configuration :-
弹簧配置:-
<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 Interceptor -
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();
}
}
回答by Paulius Matulionis
I already know two ways of doing this. One is to create your SOAP Handler and register it as JAX-WShandler in your Spring config.
我已经知道两种方法可以做到这一点。一种是创建您的 SOAP 处理JAX-WS程序并将其注册为您的 Spring 配置中的处理程序。
Check my answer herehow to create a SOAP handler. As you want header to appear in the response (outgoing request) don't forget that you then need to check if the message is outbound, something like this would do:
检查我的答案在这里如何创建一个SOAP处理程序。由于您希望标头出现在响应(传出请求)中,请不要忘记您需要检查消息是否出站,如下所示:
Boolean outbound = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
if (outbound) {
//Modify your header.
}
The other, maybe more easier way. Put the header directly into CXF response context. Please note that this example is only proof of concepts, I don't know the situation in reality where you need credentials in the response. It will display how to add user credentials object into the header, you have to modify it depending on your needs.
另一种可能更简单的方法。将标头直接放入 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);
}
This method needs to be called just after the request your client.
此方法需要在您的客户端请求之后立即调用。

