Apache CXF - 设置 HTTP 标头 [英] Apache CXF - Set HTTP header

查看:83
本文介绍了Apache CXF - 设置 HTTP 标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须在 Apache CXF 客户端中设置一些 http 标头字段:

I have to set some http header fields in a Apache CXF client:

我通过 Interceptor 尝试过:

I tried it via Interceptor:

    public class HttpHeaderInterceptor extends AbstractPhaseInterceptor<Message> {

    private String userId;
    private String xAuthorizeRoles;
    private String host;


    public HttpHeaderInterceptor() {
        super(Phase.POST_PROTOCOL);
    }

    @Override
    public void handleMessage(Message message) throws Fault {
        Map<String, List> headers = (Map<String, List>) message.get(Message.PROTOCOL_HEADERS);
        try {
            System.out.println("HttpHeaderInterceptor Host: " + host + " UserId: " + userId + " X-AUTHORIZE-roles: " + xAuthorizeRoles);
            headers.put("Host", Collections.singletonList(host));
            headers.put("UserId", Collections.singletonList(userId));
            headers.put("X-AUTHORIZE-roles", Collections.singletonList(xAuthorizeRoles));
        } catch (Exception ce) {
            throw new Fault(ce);
        }
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public void setxAuthorizeRoles(String xAuthorizeRoles) {
        this.xAuthorizeRoles = xAuthorizeRoles;
    }

    public void setHost(String host) {
        this.host = host;
    }
}

在我的动态客户端类中,方法:

in my dynamic client class the methode:

public void setHttHeaderInterceptor(String userId, String xAuthorizeRoles){
    Client cxfClient = ClientProxy.getClient(this.abgWebServicePort);
    HttpHeaderInterceptor httpHeaderInterceptor = new HttpHeaderInterceptor();
    httpHeaderInterceptor.setHost("example.org");
    httpHeaderInterceptor.setUserId(userId);
    httpHeaderInterceptor.setxAuthorizeRoles(xAuthorizeRoles);
    cxfClient.getOutInterceptors().add(httpHeaderInterceptor);
}

在我调用远程服务之前被调用:

is called before I invoke the remote service:

对于每个调用,userId 和 xAuthorizeRoles 应该有所不同,但是当我通过 tcpdump 通过调用进行检查时,所有调用在标头字段中都有相同的值.

For each call the userId and the xAuthorizeRoles should vary but when I inspect by calls via tcpdump all calls have the same values in the header fields.

有什么想法吗?

推荐答案

我的问题已经解决了:

通过xml配置添加拦截器:

adding the interceptor via xml configuration:

<jaxws:client id="clientBean" serviceClass="org.example.service.ServicePortType"
              address="example.org/src/service/ServicePort">
    <jaxws:outInterceptors>
        <bean class="org.example.interceptor.HttpHeaderInterceptor"/>
    </jaxws:outInterceptors>
    <jaxws:properties>
        <entry key="mtom-enabled" value="true"/>
    </jaxws:properties>
</jaxws:client>

在客户端类中,我将 setHttpHeaderInterceptor 更改为

in the client class I altered setHttpHeaderInterceptor to

public void setHttpHeaderInterceptor(String userId, String xAuthorizeRoles){
    Client cxfClient = ClientProxy.getClient(this.servicePort);
    cxfClient.getRequestContext().put("HTTP_HEADER_HOST", "example.org");
    cxfClient.getRequestContext().put("HTTP_HEADER_USER_ID", userId);
    cxfClient.getRequestContext().put("HTTP_HEADER_X_AUTHORIZE-ROLES", xAuthorizeRoles);
}

拦截器类

@Override
    public void handleMessage(Message message) throws Fault {
        Map<String, List> headers = (Map<String, List>) message.get(Message.PROTOCOL_HEADERS);
        try {
            headers.put("Host", Collections.singletonList(message.get("HTTP_HEADER_HOST")));
            headers.put("KD_NR", Collections.singletonList(message.get("HTTP_HEADER_KD_NR")));
            headers.put("X-AUTHORIZE-roles", Collections.singletonList(message.get("HTTP_HEADER_X_AUTHORIZE-ROLES")));
        } catch (Exception ce) {
            throw new Fault(ce);
        }
    }

现在可以了.

通过这种方法,我可以在运行时设置 HTTP-Header 字段.

With this approach I can set HTTP-Header fields at runtime.

这篇关于Apache CXF - 设置 HTTP 标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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