gpt4 book ai didi

Apache CXF - 设置 HTTP header

转载 作者:行者123 更新时间:2023-12-02 19:51:59 25 4
gpt4 key购买 nike

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

我通过拦截器尝试过:

    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 检查调用时,所有调用在 header 字段中具有相同的值。

有什么想法吗?

最佳答案

我已经解决了我的问题:

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

关于Apache CXF - 设置 HTTP header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10479396/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com