如何在CXF客户端中动态添加HTTP标头? [英] How to dynamically add HTTP headers in CXF client?
问题描述
我正在使用 Spring
和 CXF
来创建一个soap webservice客户端。
Web服务需要HTTP标头授权= [pass]
进行身份验证。到目前为止,我实现如下:
Map< String,Object> context =((BindingProvider)port).getRequestContext();
Map< String,List< String>> headers = new HashMap<>();
headers.put(Authorization,Arrays.asList(Basic pass)); //到目前为止的固定值
context.put(MessageContext.HTTP_REQUEST_HEADERS,headers);
但是现在我必须根据实际执行客户端的用户动态更改基本传递。 / p>
问题:当我在Spring上使用CXF时,我的CXF客户端是Spring管理和共享的代理。
因此,每当我更改端口
内的HTTP标头时,它都会影响所有实例!
如何在发送Web服务请求期间动态添加HTTP身份验证标头?
我必须完全创建一个http标头:授权= [pass]
我假设您真正想要做的是HTTP BASIC身份验证。 CXF中定义了API以设置BASIC身份验证的用户名/密码。这将做你想要的:
HTTPConduit httpConduit =(HTTPConduit)ClientProxy.getClient(stub).getConduit();
httpConduit.getAuthorization()。setUserName(username);
httpConduit.getAuthorization()。setPassword(password);
还有其他选择,例如将用户名/密码直接放入请求上下文中:
AuthorizationPolicy authzPolicy = new AuthorizationPolicy();
authzPolicy.setUserName(username);
authzPolicy.setPassword(password);
((BindingProvider)stub).getRequestContext()。put(AuthorizationPolicy.class.getName(),authzPolicy);
I'm using Spring
and CXF
to create a soap webservice client.
The webservice requires the HTTP header Authorization=[pass]
for authentication. So far I implemented it as follows:
Map<String, Object> context = ((BindingProvider) port).getRequestContext();
Map<String, List<String>> headers = new HashMap<>();
headers.put("Authorization", Arrays.asList("Basic pass")); //fixed value so far
context.put(MessageContext.HTTP_REQUEST_HEADERS, headers);
But now I have to change the basic pass dynamically based on the user that actually executes the client.
Problem: as I use CXF with Spring, my CXF clients are managed and shared proxies by Spring.
So whenever I change the HTTP header inside the port
, it will affect all the instances!
How could I add the HTTP auth header dynamically just during sending of the webservice requests?
I have to exactly create a http header lik: Authorization=[pass]
I assume what you really want to do is HTTP BASIC Authentication. There are defined APIs in CXF to set username/password for BASIC authentication. This will do what you want:
HTTPConduit httpConduit = (HTTPConduit) ClientProxy.getClient(stub).getConduit();
httpConduit.getAuthorization().setUserName("username");
httpConduit.getAuthorization().setPassword("password");
There are also other alternatives, like putting username/password directly into the request context:
AuthorizationPolicy authzPolicy = new AuthorizationPolicy();
authzPolicy.setUserName("username");
authzPolicy.setPassword("password");
((BindingProvider) stub).getRequestContext().put(AuthorizationPolicy.class.getName(), authzPolicy);
这篇关于如何在CXF客户端中动态添加HTTP标头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!