1.客戶端調用webservice工具類 java
package cn.net.sunge.oas_dep.utils; import java.io.File; import java.io.FileInputStream; import java.security.KeyStore; import java.util.Map; import javax.activation.DataHandler; import javax.activation.DataSource; import javax.activation.FileDataSource; import javax.net.ssl.KeyManager; import javax.net.ssl.KeyManagerFactory; import javax.net.ssl.TrustManager; import javax.net.ssl.TrustManagerFactory; import org.apache.cxf.configuration.jsse.TLSClientParameters; import org.apache.cxf.endpoint.Client; import org.apache.cxf.frontend.ClientProxy; import org.apache.cxf.jaxws.JaxWsProxyFactoryBean; import org.apache.cxf.transport.http.HTTPConduit; import cn.net.sunge.oas_dep.modules.webservice.model.FileEntity; public class WsClientUtil { public static <T> T getInterface(Class<T> clazz, String address) { return getInterface(clazz, address, null); } @SuppressWarnings("unchecked") public static <T> T getInterface(Class<T> clazz, String address, Map<String, Object> properties) { JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean(); factory.setAddress(address); factory.setServiceClass(clazz); if (null != properties) { factory.setProperties(properties); } return (T) factory.create(); } public static <T> T getHttpsInterface(Class<T> clazz, String address, String jksPath, String jksPwd) { return getHttpsInterface(clazz, address, jksPath, jksPwd, null); } @SuppressWarnings("unchecked") public static <T> T getHttpsInterface(Class<T> clazz, String address, String jksPath, String jksPwd, Map<String, Object> properties) { JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean(); factory.setAddress(address); factory.setServiceClass(clazz); if (null != properties) { factory.setProperties(properties); } T t = (T) factory.create(); configureSSLOnTheClient(t, jksPath, jksPwd); return t; } private static void configureSSLOnTheClient(Object obj, String jksPath, String jksPwd) { File file = new File(jksPath); Client client = ClientProxy.getClient(obj); HTTPConduit httpConduit = (HTTPConduit) client.getConduit(); try { TLSClientParameters tlsParams = new TLSClientParameters(); tlsParams.setDisableCNCheck(true); KeyStore keyStore = KeyStore.getInstance("JKS"); String password = jksPwd; String storePassword = jksPwd; keyStore.load(new FileInputStream(file), storePassword.toCharArray()); TrustManagerFactory trustFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); trustFactory.init(keyStore); TrustManager[] trustManagers = trustFactory.getTrustManagers(); tlsParams.setTrustManagers(trustManagers); keyStore.load(new FileInputStream(file), storePassword.toCharArray()); KeyManagerFactory keyFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); keyFactory.init(keyStore, password.toCharArray()); KeyManager[] keyManagers = keyFactory.getKeyManagers(); tlsParams.setKeyManagers(keyManagers); // FiltersType filtersTypes = new FiltersType(); // filtersTypes.getInclude().add(".*_EXPORT_.*"); // filtersTypes.getInclude().add(".*_EXPORT1024_.*"); // filtersTypes.getInclude().add(".*_WITH_DES_.*"); // filtersTypes.getInclude().add(".*_WITH_NULL_.*"); // filtersTypes.getExclude().add(".*_DH_anon_.*"); // tlsParams.setCipherSuitesFilter(filtersTypes); tlsParams.setDisableCNCheck(true); httpConduit.setTlsClientParameters(tlsParams); } catch (Exception e) { e.printStackTrace(); } } /** * Construct FileEntity. * * @param the * real file path. * @return FileEntity. */ public static FileEntity constructFileEntity(String filePath) { // construct FileEntity FileEntity fileEntity = new FileEntity(); File file = new File(filePath); fileEntity.setFileName(file.getName().substring(0, (file.getName().lastIndexOf(".")))); fileEntity.setFileType(filePath.substring(filePath.lastIndexOf(".") + 1)); DataSource source = new FileDataSource(file); DataHandler handler = new DataHandler(source); fileEntity.setFile(handler); return fileEntity; } }
2.調用Util web
Map<String, Object> properties = Maps.newHashMap(); properties.put("mtom-enabled", true); IntranetFileWebserviceI intranetFileWebservice = WsClientUtil.getIntranetInterface(IntranetFileWebserviceI.class, IntranetFileWebserviceI.address, properties);
參考資料: apache