如何将获取请求中的凭据(基本身份验证)传递给另一个Web服务的新请求?
我没有找到任何可以在单个请求中在拦截器之间共享数据的属性包。
澄清:
答案 0 :(得分:2)
希望通过这个解决方案,我不会在秒中运行。麻烦?
我做了什么:
添加一个inInterceptor读出的凭证和远程ip
AuthorizationPolicy policy = message.get(AuthorizationPolicy.class);
if (policy == null) {
sendErrorResponse(message, HttpURLConnection.HTTP_UNAUTHORIZED);
return;
}
message.put("request_usr", policy.getUserName());
message.put("request_pwd", policy.getPassword());
操纵CXF生成的WebServiceClient以更改构造函数返回值,如
/**
*
* @return returns WebServiceClass
*/
@WebEndpoint(name = "WebServiceClassSoap")
public WebServiceClassSoap getWebServiceClassSoap() {
return dynamicAuthorisation(super.getPort(WebServiceClassSoap,
WebServiceClassSoap.class));
}
private WebServiceClassSoap dynamicAuthorisation (WebServiceClassSoap service) {
return dynamicAuthorisation(service,
PhaseInterceptorChain.getCurrentMessage().get("request_usr").toString(),
PhaseInterceptorChain.getCurrentMessage().get("request_pwd").toString());
}
private WebServiceClassSoap dynamicAuthorisation (WebServiceClassSoap service, String username, String password) {
Client client = ClientProxy.getClient(service);
HTTPConduit http = (HTTPConduit) client.getConduit();
AuthorizationPolicy auth = http.getAuthorization();
auth.setUserName(username);
auth.setPassword(password);
http.setAuthorization(auth);
return service;
}
在beans.xml中保留http-conf:conduit
<http-conf:conduit name="{http://schemas.foobar.com/websvc/WebServiceClass/}WebServiceClassSoap.http-conduit">
<http-conf:authorization>
<!--
<sec:UserName>${webservices.username}@${webservices.domain}</sec:UserName>
<sec:Password>${webservices.password}</sec:Password>
-->
<sec:AuthorizationType>Basic</sec:AuthorizationType>
</http-conf:authorization>
<http-conf:client AllowChunking="false" ConnectionTimeout="30000" />
</http-conf:conduit>
感谢Apache CXF: Forwarding an information from an interceptor to the actual webservice implementation =)