我必须在 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 调用进行检查时,所有调用在标头字段中都具有相同的值。
有任何想法吗?