希望這個解決方案,我不會在秒運行。麻煩?
我做了什麼:
添加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此類改變contructors返回值一樣
/**
*
* @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.xml
<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 =)