`
liugang594
  • 浏览: 981211 次
  • 性别: 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发布调用的好例子

    cxf+spring使用经验

    2. **创建服务提供者**:创建一个新的项目,例如 `cxfserver`,作为 Web 服务的服务端。确保所有 CXF 相关的库已经添加到项目中。 3. **定义服务接口**:使用 `@WebService` 注解声明 Web 服务接口,例如 `...

    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客户端调用以及发布服务

    在这里,我们使用了`@Service`注解来标记服务实现类,并通过`@SOAPBinding`来指定SOAP消息的样式和使用方式。 #### 3.2 部署与测试 完成上述步骤后,将项目打包成WAR文件,并部署到应用服务器。一旦服务启动,其他...

    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开发RESTful风格WebService

    实现UserClient接口,提供了对User的操作实现: ```java public class UserClientImpl implements UserClient { @Override public User getUser(String name) { // 实现获取User的逻辑 } @Override public ...

    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

    springBoot整合CXF并实现用户名密码校验的方法

    首先,我们需要创建两个SpringBoot项目,分别是webservice_server和webservice_client。这两个项目都需要添加CXF的依赖项。 在pom.xml文件中添加以下依赖项: ```xml &lt;groupId&gt;org.apache.cxf &lt;artifactId&gt;cxf-...

    CXF应用整理(包括大文件上传MTOM、安全机制)

    System.out.println(client.sayHi("cxf")); } } ``` ### 2. 复杂对象的传递 在 CXF 中,你可以传递包含列表、映射等复杂数据类型的对象。首先,定义一个实体类: ```java public class Demo { private String ...

    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的镜像

Global site tag (gtag.js) - Google Analytics