`
liugang594
  • 浏览: 977482 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

[CXF] Server与Client实现方式五:HTTPS

 
阅读更多

【参考:http://cxf.apache.org/docs/client-http-transport-including-ssl-support.html 】

【参考:http://cxf.apache.org/docs/configuration.html 】

前几节讲了http和jms的传输协议的实现,这节介绍如何使用https来实现通信。

一、生成密钥

要使用https通信,首先我们需要生成一个用于双方通信的密钥文件,可以使用java自带的keytool工具来生成,例如:

keytool -genkey -alias gliu -keyalg RSA -storepass liu123 -keypass liu123 -keystore gliu.jks -dname "CN=localhost"

如果不指定 -keystore 参数,则会在用户目录下生成一个.keystore 文件。另外这里-dname里的CN参数需要指定为你的website的名字,例如对于本地测试,则使用localhost。

 

二、接口定义

和前几节一样,接口定义很简单:

@WebService
public interface OrderProcess {

	public String processOrder(Order order);
}

 

三、创建Service

要使得service支持https的传输协议,就需要用上面生成的密钥文件去配置服务引擎,首先在创建service时取得引擎对象:

		JaxWsServerFactoryBean bean = new JaxWsServerFactoryBean();
		Bus bus = bean.getBus();
		JettyHTTPServerEngineFactory serverEngineFactory = bus
				.getExtension(JettyHTTPServerEngineFactory.class);

 然后就是配置引擎对象:

		File file = new File("key/gliu.jks");
		TLSServerParameters tlsParams = new TLSServerParameters();
		KeyStore keyStore = KeyStore.getInstance("JKS");
		String password = "liu123";
		String storePassword = "liu123";

		FileInputStream is = new FileInputStream(file);
		keyStore.load(is, storePassword.toCharArray());
		is.close();
		
		KeyManagerFactory keyFactory = KeyManagerFactory
				.getInstance(KeyManagerFactory.getDefaultAlgorithm());
		keyFactory.init(keyStore, password.toCharArray());
		KeyManager[] keyManagers = keyFactory.getKeyManagers();
		tlsParams.setKeyManagers(keyManagers);

		TrustManagerFactory trustFactory = TrustManagerFactory
				.getInstance(TrustManagerFactory.getDefaultAlgorithm());
		trustFactory.init(keyStore);
		TrustManager[] trustManagers = trustFactory.getTrustManagers();
		tlsParams.setTrustManagers(trustManagers);
		serverEngineFactory.setTLSServerParametersForPort(443, tlsParams);

 最后创建服务即可:

		bean.setAddress("https://localhost/security/order");
		bean.setServiceBean(new OrderProcessImpl());
		bean.setServiceClass(OrderProcess.class);
		bean.create();

 启动服务后就可以通过以下路径访问生成的wsdl了:

https://localhost/security/order?wsdl

 

也可以使用Spring配置,如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:sec="http://cxf.apache.org/configuration/security"
	xmlns:jetty="http://cxf.apache.org/transports/http-jetty/configuration"
	xmlns:jaxws="http://cxf.apache.org/jaxws"
	xsi:schemaLocation="
            http://cxf.apache.org/jaxws 
            http://cxf.apache.org/schemas/jaxws.xsd
            http://cxf.apache.org/configuration/security              
            http://cxf.apache.org/schemas/configuration/security.xsd
            http://cxf.apache.org/transports/http-jetty/configuration
            http://cxf.apache.org/schemas/configuration/http-jetty.xsd
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd">

	<import resource="classpath:META-INF/cxf/cxf.xml" />
	<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
	<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />

	<jetty:engine-factory bus="cxf">
		<jetty:engine port="443">
			<jetty:tlsServerParameters>
				<sec:keyManagers keyPassword="liu123">
					<sec:keyStore type="JKS" password="liu123" file="key/gliu.jks" />
				</sec:keyManagers>
			</jetty:tlsServerParameters>
		</jetty:engine>
	</jetty:engine-factory>
	
	<jaxws:endpoint id="orderProcess"
		implementorClass="com.liulutu.liugang.https.OrderProcess"
		implementor="com.liulutu.liugang.https.OrderProcessImpl" address="https://localhost:443/security/order">
	</jaxws:endpoint>
</beans>

 

四、Client设置

一个简单的调用HTTPS服务的方式就是设置系统的ssl属性,例如调用上述service:

		System.setProperty("javax.net.ssl.trustStore", "key/gliu.jks");

		JaxWsDynamicClientFactory factory = JaxWsDynamicClientFactory
				.newInstance();
		Client client = factory
				.createClient("https://localhost/security/order?wsdl");
		Order order = new Order();
		order.setId("helloId");
		Object[] invokeWrapped = client.invoke("processOrder", order);
		System.out.println(invokeWrapped[0]);

 也可以通过配置HTTPConduit对象来调用,例如:

		//create and configure client bean
		JaxWsClientFactoryBean jwcfb = new JaxWsClientFactoryBean();
		jwcfb.setServiceClass(OrderProcess.class);
		jwcfb.setAddress("https://localhost/security/order");
		Client client = jwcfb.create();
		
		//configure conduit
		HTTPConduit conduit = (HTTPConduit) client.getConduit();
		configureSecurityInfoFor(conduit);
		
		//invoke
		Order order = new Order();
		order.setId("helloId");
		Object[] invokeWrapped = client.invoke("processOrder", order);
		System.out.println(invokeWrapped[0]);

 

其中configureSecurityInfoFor(conduit)方法实现如如下:
		TLSClientParameters tlsClientParameters = new TLSClientParameters();
		tlsClientParameters.setSecureSocketProtocol("SSL");

		FileInputStream is = new FileInputStream("key/gliu.jks");
		KeyStore keyStore = KeyStore.getInstance("JKS");
		keyStore.load(is, "liu123".toCharArray());
		is.close();

		//set trust keystore
		TrustManagerFactory trustManagerFactory = TrustManagerFactory
				.getInstance(TrustManagerFactory.getDefaultAlgorithm());
		trustManagerFactory.init(keyStore);
		tlsClientParameters.setTrustManagers(trustManagerFactory
				.getTrustManagers());

		//set keystore
		KeyManagerFactory keyFactory = KeyManagerFactory
				.getInstance(KeyManagerFactory.getDefaultAlgorithm());
		keyFactory.init(keyStore, "liu123".toCharArray());
		tlsClientParameters.setKeyManagers(keyFactory.getKeyManagers());

		conduit.setTlsClientParameters(tlsClientParameters);
 也可以使用Spring配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:http-conf="http://cxf.apache.org/transports/http/configuration"
	xmlns:sec="http://cxf.apache.org/configuration/security"
	xsi:schemaLocation="
	   http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
	   http://cxf.apache.org/transports/http/configuration http://cxf.apache.org/schemas/configuration/http-conf.xsd
	   http://cxf.apache.org/configuration/security http://cxf.apache.org/schemas/configuration/security.xsd 
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

	<http-conf:conduit name="*.http-conduit">
		<http-conf:tlsClientParameters
			secureSocketProtocol="SSL">
			<sec:keyManagers keyPassword="liu123">
				<sec:keyStore type="JKS" password="liu123" file="key/gliu.jks" />
			</sec:keyManagers>
			<sec:trustManagers>
				<sec:keyStore type="JKS" password="liu123" file="key/gliu.jks" />
			</sec:trustManagers>
		</http-conf:tlsClientParameters>
	</http-conf:conduit>

	<jaxws:client id="orderProcessClient"
		serviceClass="com.liulutu.liugang.https.OrderProcess" address="https://localhost:443/security/order">
	</jaxws:client>
</beans>
 然后在java代码里:
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("/com/liulutu/liugang/https/client.xml");
		OrderProcess bean = context.getBean("orderProcessClient", OrderProcess.class);
		Order order = new Order();
		order.setId("hello");
		String processOrder = bean.processOrder(order);
		System.out.println(processOrder);
 
分享到:
评论
1 楼 运乃强谦 2018-12-06  
老哥,你确定这个wsdl  地址 可以访问?

相关推荐

    cxf spring server client demo.rar

    cxf spring server client demo.rar soap webservice

    java webservice server and client

    使用webservice的cxf做server与client例子

    spring集成cxf,server发布webservice,client调用webservice

    使用spring集成cxf,在两个web ...server端使用spring+springmvc+mybatis+cxf,client端使用struts2+spring+hibernate+cxf 两个工程均为myeclipse project,包含所有除myeclipse自带以外所有jar 内附 使用文档,数据库脚本

    springboot整合CXF发布webservice和客户端调用

    springboot整合CXF发布webservice和客户端调用 项目中每个类都有详细的注释,保证都能看懂,不失为一个学习springboot整合cxf来学习webservice发布调用的好例子

    apache-cxf-2.7.18.rar

    cxf自动生成webservice客户端,apache-cxf-2.7.18最稳定的版本 apache cxf 框架wsdl2java命令的使用。 -encoding是指定编码类型; -p 指定包名 -d 指定生成目录 -all生成服务端和客户端代码 -...

    cxf 2.1.3 client

    测试 cxf 2.1.3客服端 调用 xfire 1.2.6 的服务端 的对象传递,与对象返回,服务端见xfire 1.2.6 server

    apache-cxf-3.3.4.rar

    apache-cxf-3.3.4.zip wsdl webservice 通过wsdl2java的命令生成客户端代码 进入dos窗口,进入apache-cxf-2.3.1\bin所在的目录,输入...原文链接:https://blog.csdn.net/qq_36970062/article/details/79731681

    webservice-CXF-spring+maven

    webservice-CXF-spring 实现server+client

    CXF 2.4 WebService 发布和调用的身份验证和获取示例代码

    实现: server.TrialServiceImpl 使用 @Resource private WebServiceContext wsctx;得到wsctx,从而得到调用者身份. 配置: trial-cxf-server.xml 其中配置了对传入请求的拦截器用以验证调用者身份 验证程序: ...

    cxf ws-Security的实现

    cxf ws-Security的实现 WS-SecurityPolicy 安全配置指定在客户机和服务之间交换的消息所需的安全处理。在大多数情况下,Web 服务堆栈还需要更多信息,才能对消息交换应用安全措施。 里面有2个project,分别server ...

    cxf简单例子,包含lib 下载即可运行

    cxf简单例子,包括 server 端跟 client 端 两个项目 以及lib 下载即可运行

    cxf-ssl:JBossWS-CXF SSL 客户端示例

    JBoss EAP 6 中的 #CXF SSL 客户端 这是一个展示如何设置 JBossWS CXF SSL 客户端的项目。 除此之外,它还展示了如何在 EAP 6 中自动安装 HTTPS ... 客户端日志在client.log ,服务器日志在server.log 您必须正确设置J

    cxf应用demo 以及与spring整合

    cxf应用demo,代码取例,一共有四个工程,Client,Server,ClientSpring,ServerSpering 且包含所有jar包,直接可以运行

    xfire 1.2.6 server

    xfire server 支持对象的传递和对象的返回,对应的cxf client cxf 1.2.6 client

    cxf-xjc-utils:Apache CXF的镜像

    cxf-xjc-utils:Apache CXF的镜像

    cxf-dosgi:Apache CXF的镜像

    因此,这是在OSGi中将CXF与声明式服务一起使用的推荐方法。 查看示例以了解使用的简单程度。 模组 。 意向 意图允许服务利用CXF扩展(例如功能)。 服务可以列出其所需的命名意图。 然后,只有在所有意图都可用时,...

    wsdl2java源码-springboot-cxf-demo:基于ApacheCXF框架,并以“Contract-First”模式进行服务客

    CXF后,并配置完成相关环境变量,通过wsdl2java生成对应的Java代码,实现方式有很多,这里推荐两种常用方式。 在拥有WSDL和XSD的目录,执行wsdl2java命令以后,将得到的Java代码拷贝至代码工程源代码路径 本例为...

    wsdl2java源码-spring-cxf:弹簧-CXF

    wsdl2java源码弹簧-CXF 演示使用 Spring Boot 和 Apache CXF 以零 XML 配置托管 ...ws-server:bootRun 或者,可以通过将demo.spring.service.Application.java作为普通...ws-client:bootRun 在服务器运行时,执行包含main

    cxf:Apache CXF

    欢迎使用Apache CXF! Apache CXF是一个开放源代码服务框架。 CXF帮助您使用前端编程API(例如JAX-WS和JAX-RS)来构建和开发服务。 这些服务可以说各种协议,例如SOAP,XML / HTTP,RESTful HTTP或CORBA,并且可以...

    Apache-CXF使用说明

    wsdl2java -p com.founder.rhin.ws.degreeinfo -d D:\0n.Workspace\Project\server_src -client http://www.walkfish.com/Project/services/Hello?wsdl

Global site tag (gtag.js) - Google Analytics