加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

关于CXF大文件的传输问题

发布时间:2020-12-16 22:30:49 所属栏目:安全 来源:网络整理
导读:1.客户端调用webservice工具类 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;im

1.客户端调用webservice工具类


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 jksPath,String jksPwd) {
		return getHttpsInterface(clazz,jksPath,jksPwd,null);
	}

	@SuppressWarnings("unchecked")
	public static <T> T getHttpsInterface(Class<T> clazz,String jksPwd,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,jksPwd);
		return t;
	}

	private static void configureSSLOnTheClient(Object obj,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

Map<String,Object> properties = Maps.newHashMap();
properties.put("mtom-enabled",true);
IntranetFileWebserviceI intranetFileWebservice = WsClientUtil.getIntranetInterface(IntranetFileWebserviceI.class,IntranetFileWebserviceI.address,properties);



?参考资料:

http://yanghui-java.iteye.com/blog/1747643

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读