Java 访问sharepoint webservice(NTLM & SSL)

引用
遇到需要使用java访问微软的sharepoint的webservice, 公司服
务器配置的是NTLM+SSL验证
解决办法: 
jdk6支持ntlm和SSL, 同时使用jdk6 webservice做客户端


1 sharepoint wsdl
https://server/_vti_bin/lists.asmx?WSDL


2 使用如下命令生成webservice客户端代码,将会有很多java类生成
wsimport  -d  ./bin  -s  ./src -p test.jws.client.ref ./wsdl/citi.sharepoint.wsdl

Java 访问sharepoint webservice(NTLM & SSL)


3 子类化一个Authenticator,用于NTLM验证
public class DefaultAuthenticator extends Authenticator {
	private static String username = "...";
	private static String domain = "...";
	private static String password = ".....!";
	
	public PasswordAuthentication getPasswordAuthentication() {
		String usernamewithdomain = domain + "/ " + username;
		return (new PasswordAuthentication(usernamewithdomain, password.toCharArray()));
	}
}


4 main方法内有信任所有证书的代码,解决ssl证书,同时有访问ws的客户端调用
public static void main(String[] args) throws NoSuchAlgorithmException, KeyManagementException {
		//NTLM Auth
		Authenticator.setDefault(new DefaultAuthenticator());

		//trust all the SSL  cert --start
		TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
			public java.security.cert.X509Certificate[] getAcceptedIssuers() {
				return null;
			}

			public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) {
			}

			public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) {
			}
		} };
		HostnameVerifier dummyHostnameVerifier = new HostnameVerifier() {
			public boolean verify(String hostname, SSLSession session) {
				return true;
			}
		};
		SSLContext sc = SSLContext.getInstance("SSL");
		sc.init(null, trustAllCerts, new SecureRandom());
		HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
		HttpsURLConnection.setDefaultHostnameVerifier(dummyHostnameVerifier);
		//trust all the SSL cert -- end
		
		// ws client call
		ListsSoap list = new Lists().getListsSoap();
		System.out.println(list.getListCollection().getContent().size());
	}

你可能感兴趣的:(webservice)