如何修改CXF中的JAX-WS响应的HTTP头? [英] How do i modify HTTP headers for a JAX-WS response in CXF?

查看:476
本文介绍了如何修改CXF中的JAX-WS响应的HTTP头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在开发CXF Web服务,这些服务位于一个安全代理之后,该代理在调用服务之前请求HTTP基本认证。这些服务彼此之间进行通信,并要求对请求和响应进行身份验证。



到目前为止,我已经能够通过HTTPConduit为请求设置HTTP基本身份验证,如下所示:

 客户端客户端= ClientProxy.getClient(port); 
HTTPConduit conduit =(HTTPConduit)client.getConduit();
AuthorizationPolicy authorizationPolicy = new AuthorizationPolicy();
authorizationPolicy.setUserName(username);
authorizationPolicy.setPassword(password);
authorizationPolicy.setAuthorizationType(Basic);
conduit.setAuthorization(authorizationPolicy);

上面的方法在每个服务方法调用上被调用,并且我在表单中获得正确的入站消息

  INFO:入站消息
----------------- -----------
ID:1
地址:http:// someURL / someService?wsdl
编码:UTF-8
Http方法: POST
Content-Type:text / xml; charset = UTF-8
Headers:{Accept = [* / *],Authorization = [Basic {TOKEN}],
cache-control = [no-cache],connection = [keep-alive] ,
Content-Length = [735],content-type = [text / xml; charset = UTF-8],
pragma = [no-cache],...}
有效负载:< soap:Envelope> ...< / soap:Envelope>
--------------------------------------

但是,该响应不包含所需的标题

  INFO:出站消息
---------------------------
ID:2
编码:UTF-8
Content-Type:text / xml
标题:{}
Payload:< soap:Envelope /> ...< / soap:信封>
--------------------------------------

如何修改响应HTTP头文件?我已经试过了

<$ p $($ BindingProvider)port).getResponseContext()。put(
MessageContext.HTTP_RESPONSE_HEADERS,
Collections.singletonMap(Authentication,
Collections.singletonList() Basic+ token)));

没有得到想要的结果。

解决方案

一种方法是创建一个 CXF拦截器

  public class BasicAuthOutInterceptor extends AbstractPhaseInterceptor< Message> {

public BasicAuthOutInterceptor(){
super(Phase.PRE_STREAM);
}

@Override
public void handleMessage(Message message)throws Fault {
String token =basic auth token;

@SuppressWarnings(unchecked)
Map< String,List< String>> headers =(Map< String,List< String>)消息
.get(Message.PROTOCOL_HEADERS);
if(headers == null){
headers = new TreeMap< String,List< String>>(
String.CASE_INSENSITIVE_ORDER);
message.put(Message.PROTOCOL_HEADERS,headers);
}

headers.put(Authentication,Arrays.asList(Basic+ token));
}

}

并将其注册为输出outFault拦截器。

 < bean id =basicAuthOutInterceptor class =BasicAuthOutInterceptor/> 

< cxf:bus>
< ref bean =basicAuthOutInterceptor/>
< / cxf:outInterceptors>
< cxf: outFaultInterceptors>
< ref>
<


I have been working on developing CXF web services that sit behind a security proxy which asks for HTTP basic authentication prior service invocation. These services communicate between each other and require authentication for both request and response.

So far i have been able to set HTTP basic authentication via the HTTPConduit for the request like so:

    Client client = ClientProxy.getClient(port);
    HTTPConduit conduit = (HTTPConduit) client.getConduit();
    AuthorizationPolicy authorizationPolicy = new AuthorizationPolicy();
    authorizationPolicy.setUserName(username);
    authorizationPolicy.setPassword(password);
    authorizationPolicy.setAuthorizationType("Basic");
    conduit.setAuthorization(authorizationPolicy);

The method above is called on every service method invocation and i'm getting correct inbound messages in the form of

INFO: Inbound Message
----------------------------
ID: 1
Address: http://someURL/someService?wsdl
Encoding: UTF-8
Http-Method: POST
Content-Type: text/xml; charset=UTF-8
Headers: {Accept=[*/*], Authorization=[Basic {TOKEN}], 
                cache-control=[no-cache], connection=[keep-alive], 
                Content-Length=[735], content-type=[text/xml; charset=UTF-8], 
                pragma=[no-cache], ...}
Payload: <soap:Envelope>...</soap:Envelope>
--------------------------------------

The response, however, doesn't contain the required headers

INFO: Outbound Message
---------------------------
ID: 2
Encoding: UTF-8
Content-Type: text/xml
Headers: {}
Payload: <soap:Envelope/">...</soap:Envelope>
--------------------------------------

How can i modify response HTTP headers? I've tried

((BindingProvider)port).getResponseContext().put(
           MessageContext.HTTP_RESPONSE_HEADERS, 
           Collections.singletonMap("Authentication", 
           Collections.singletonList("Basic "+token)));

without getting the desired result.

解决方案

One approach would be creating a CXF interceptor.

public class BasicAuthOutInterceptor extends AbstractPhaseInterceptor<Message> {

    public BasicAuthOutInterceptor() {
        super(Phase.PRE_STREAM);
    }

    @Override
    public void handleMessage(Message message) throws Fault {
        String token = "basic auth token";

        @SuppressWarnings("unchecked")
        Map<String, List<String>> headers = (Map<String, List<String>>) message
                .get(Message.PROTOCOL_HEADERS);
        if (headers == null) {
            headers = new TreeMap<String, List<String>>(
                    String.CASE_INSENSITIVE_ORDER);
            message.put(Message.PROTOCOL_HEADERS, headers);
        }

        headers.put("Authentication", Arrays.asList("Basic "+ token));
    }

}

and registering it as an out and outFault interceptor.

    <bean id="basicAuthOutInterceptor class="BasicAuthOutInterceptor" />

    <cxf:bus>
        <cxf:outInterceptors>
            <ref bean="basicAuthOutInterceptor"/>
        </cxf:outInterceptors>
        <cxf:outFaultInterceptors>
            <ref bean="basicAuthOutInterceptor"/>
        </cxf:outFaultInterceptors>        
    </cxf:bus> 

这篇关于如何修改CXF中的JAX-WS响应的HTTP头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆