18

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

我通过拦截器尝试过:

    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;
    }
}

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

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);
}

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

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

有任何想法吗?

4

4 回答 4

18

我已经解决了我的问题:

通过xml配置添加拦截器:

<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 更改为

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 字段。

于 2012-05-10T06:49:46.437 回答
1

您应该使用 :Phase.POST_LOGICAL而不是Phase.POST. 这对我有用

于 2013-10-30T21:00:21.293 回答
1

这是一个代码片段,用于在单个 CXF 输出拦截器的响应中复制自定义 HTTP 标头(来自请求)。

public void handleMessage(SoapMessage message) throws Fault {
    // Get request HTTP headers
    Map<String, List<String>> inHeaders = (Map<String, List<String>>) message.getExchange().getInMessage().get(Message.PROTOCOL_HEADERS);
    // Get response HTTP headers
    Map<String, List<String>> outHeaders = (Map<String, List<String>>) message.get(Message.PROTOCOL_HEADERS);
    if (outHeaders == null) {
        outHeaders = new HashMap<>();
        message.put(Message.PROTOCOL_HEADERS, outHeaders);
    }
    // Copy Custom HTTP header on the response
    outHeaders.put("myCustomHTTPHeader", inHeaders.get("myCustomHTTPHeader"));
}
于 2015-08-20T14:06:57.957 回答
1

如果需要设置标准 HTTP 标头,那么也可以使用 http 管道来完成。

<http-conf:conduit
        name="*.http-conduit">
<http-conf:client AllowChunking="false" AcceptEncoding="gzip,deflate" Connection="Keep-Alive"
 Host="myhost.com"/>
</http-conf:conduit>
于 2016-08-10T05:53:14.550 回答