开发CXF客户端通过HTTP Basic Authorization 安全认证的几种办法
开发CXF客户端通过HTTP Basic Authorization 安全认证的几种办法,有很多个,但是网上包括英文stackoverflow网站和官方文档全部是错误的。一般的CXF客户端开发方法是首先通过网络获取WSDL文件,然后再发送验证用户名密码调用服务,然而在客户端声明并创建服务实例的时候,如
HelloWorldService ss = new HelloWorldService(wsdlURL, SERVICE_NAME);
就无法通过Http basic 验证,所以之后的所有用户名和密码设置全部失效。
下面贴出几种CXF客户端通过HTTP Basic Authorization 安全认证的方法代码:
private static final QName SERVICE_NAME = new QName("http://example.org/", "HelloWorldService");
private HelloWorld_HelloWorldPort_Client() {
}
public static void main(String args[]) throws java.lang.Exception {
URL wsdlURL = HelloWorldService.WSDL_LOCATION;
if (args.length > 0 && args[0] != null && !"".equals(args[0])) {
File wsdlFile = new File(args[0]);
try {
if (wsdlFile.exists()) {
wsdlURL = wsdlFile.toURI().toURL();
} else {
wsdlURL = new URL(args[0]);
}
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
System.setProperty( "proxySet", "true" );
System.setProperty( "http.proxyHost", "10.133.96.245" );
System.setProperty( "http.proxyPort", "8080" );
//very important!!!
wsdlURL=new File("D:/hello.xml").toURI().toURL();
HelloWorldService ss = new HelloWorldService(wsdlURL, SERVICE_NAME);//如果wsdlURL是来自HTTP的网络,则此处无法通过Http Basic 验证,已经出错
HelloWorld port = ss.getHelloWorldPort();
//1、使用本地wsdl文件,而不是从网络中获取wsdl,则可以正常工作
// Service service = Service.create(wsdlURL, SERVICE_NAME);
// HelloWorld port = (HelloWorld) service.getPort(HelloWorld.class);
// BindingProvider bp=(BindingProvider)port;
// bp.getRequestContext().put(
// BindingProvider.USERNAME_PROPERTY, "ross");
// bp.getRequestContext().put(
// BindingProvider.PASSWORD_PROPERTY, "ross");
//2、以下可正常工作
// JaxWsProxyFactoryBean clientFactory = new JaxWsProxyFactoryBean();
// clientFactory.setAddress("http://localhost:8081/hello");
// clientFactory.setServiceClass(HelloWorld.class);
// clientFactory.setUsername("ross");
// clientFactory.setPassword("ross");
// HelloWorld port = (HelloWorld)clientFactory.create();
//end
//3、使用本地wsdl文件,而不是从网络中获取wsdl,则可以正常工作
// Client client = ClientProxy.getClient(port);
// HTTPConduit http = (HTTPConduit) client.getConduit();
// AuthorizationPolicy authorizationPolicy = new AuthorizationPolicy();
// authorizationPolicy.setUserName("ross");
// authorizationPolicy.setPassword("ross");
// authorizationPolicy.setAuthorizationType("Basic");
// http.setAuthorization(authorizationPolicy);
//end
//4、使用本地wsdl文件,而不是从网络中获取wsdl,则可以正常工作
// Map<String, List<String>> headers = new HashMap<String, List<String>>();
// headers.put("Authorization", Arrays.asList("Basic cm9zczpyb3Nz"));
// Client client = ClientProxy.getClient(port);
// client.getRequestContext().put(Message.PROTOCOL_HEADERS, headers);
//end
//5、使用本地wsdl文件,而不是从网络中获取wsdl,则可以正常工作
Client client = ClientProxy.getClient(port);
HTTPConduit http =
(HTTPConduit) client.getConduit();
HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();
httpClientPolicy.setAutoRedirect(true);
http.setClient(httpClientPolicy);
http.setAuthSupplier(
new MyBasicAuthSupplier("mule-realm", "ross", "ross"));
//end
// Okay, are you sick of configuration files ?
// This will show you how to configure the http conduit dynamically
// Client client = ClientProxy.getClient(port);
// HTTPConduit http = (HTTPConduit) client.getConduit();
// HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();
// httpClientPolicy.setConnectionTimeout(36000);
// httpClientPolicy.setAllowChunking(false);
// httpClientPolicy.setReceiveTimeout(32000);
// httpClientPolicy.setConnection(ConnectionType.CLOSE);
// http.setClient(httpClientPolicy);
{
System.out.println("Invoking sayHi...");
java.lang.String _sayHi_arg0 = "";
java.lang.String _sayHi__return = port.sayHi(_sayHi_arg0);
System.out.println("sayHi.result=" + _sayHi__return);
}
System.exit(0);
}