设为首页 收藏本站
查看: 583|回复: 0

[经验分享] Apache CXF实战之九 发布使用SSL的Web Service

[复制链接]

尚未签到

发表于 2017-1-12 10:45:36 | 显示全部楼层 |阅读模式
  本文链接:http://blog.csdn.net/kongxx/article/details/7738717
  Apache CXF实战之一 Hello World Web Service
  Apache CXF实战之二 集成Sping与Web容器
  Apache CXF实战之三 传输Java对象
  Apache CXF实战之四 构建RESTful Web Service
  Apache CXF实战之五 压缩Web Service数据
  Apache CXF实战之六 创建安全的Web Service
  Apache CXF实战之七 使用Web Service传输文件
  Apache CXF实战之八 Map类型绑定
  在使用Web Service的时候,在很多情况下会要求我们发布ssl的web service,此时如果web service是作为一个war包部署在tomcat之类的web容器中的时候,我们可以通过修改tomcat的配置来比较容易的部署发布成ssl的web service的,当对于独立运行的程序来书,此时发布web service是需要一些操作的,下面看看在CXF中怎样发布并调用SSL的Web Service。

  1. 首先是一个pojo的实体类


package com.googlecode.garbagecan.cxfstudy.ssl;
public class User {
private String id;
private String name;
private String password;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}2. 下面是Web Service的接口和实现类,这两个类和前面文章中介绍的没什么区别
package com.googlecode.garbagecan.cxfstudy.ssl;
import java.util.List;
import javax.jws.WebMethod;
import javax.jws.WebResult;
import javax.jws.WebService;
@WebService
public interface UserService {
@WebMethod
@WebResult List<User> list();
}
package com.googlecode.garbagecan.cxfstudy.ssl;
import java.util.ArrayList;
import java.util.List;
public class UserServiceImpl implements UserService {
public List<User> list() {
List<User> users = new ArrayList<User>();
for (int i = 0; i < 10; i++) {
User user = new User();
user.setId("" + i);
user.setName("user_" + i);
user.setPassword("password_" + i);
users.add(user);
}
return users;
}
}
3. 下面看看Server端代码
package com.googlecode.garbagecan.cxfstudy.ssl;
import java.io.File;
import java.io.FileInputStream;
import java.security.KeyStore;
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.TLSServerParameters;
import org.apache.cxf.configuration.security.ClientAuthentication;
import org.apache.cxf.configuration.security.FiltersType;
import org.apache.cxf.endpoint.Server;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
import org.apache.cxf.transport.http_jetty.JettyHTTPServerEngineFactory;
public class MyServer {
private static final int port = 12345;
private static final String address = "https://0.0.0.0:"+port+"/ws/ssl/userService";
public static void main(String[] args) throws Exception {
System.out.println("Starting Server");
configureSSLOnTheServer();
JaxWsServerFactoryBean factoryBean = new JaxWsServerFactoryBean();
factoryBean.setServiceClass(UserServiceImpl.class);
factoryBean.setAddress(address);
Server server = factoryBean.create();
String endpoint = server.getEndpoint().getEndpointInfo().getAddress();
System.out.println("Server started at " + endpoint);
}
public static void configureSSLOnTheServer() {
File file = new File(MyServer.class.getResource("/com/googlecode/garbagecan/cxfstudy/ssl/test.jks").getFile());
try {
TLSServerParameters tlsParams = new TLSServerParameters();
KeyStore keyStore = KeyStore.getInstance("JKS");
String password = "mypassword";
String storePassword = "mypassword";
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);
keyStore.load(new FileInputStream(file), storePassword.toCharArray());
TrustManagerFactory trustFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
trustFactory.init(keyStore);
TrustManager[] trustManagers = trustFactory.getTrustManagers();
tlsParams.setTrustManagers(trustManagers);
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);
ClientAuthentication ca = new ClientAuthentication();
ca.setRequired(true);
ca.setWant(true);
tlsParams.setClientAuthentication(ca);
JettyHTTPServerEngineFactory factory = new JettyHTTPServerEngineFactory();
factory.setTLSServerParametersForPort(port, tlsParams);
} catch (Exception e) {
e.printStackTrace();
}
}
}
4. 下面看看Client端代码
package com.googlecode.garbagecan.cxfstudy.ssl;
import java.io.File;
import java.io.FileInputStream;
import java.security.KeyStore;
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.configuration.security.FiltersType;
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;
public class MyClient {
private static final String address = "https://localhost:12345/ws/ssl/userService";
public static void main(String[] args) throws Exception {
JaxWsProxyFactoryBean factoryBean = new JaxWsProxyFactoryBean();
factoryBean.setAddress(address);
factoryBean.setServiceClass(UserService.class);
Object obj = factoryBean.create();
UserService userService = (UserService) obj;
configureSSLOnTheClient(userService);
System.out.println(userService.list());
}
private static void configureSSLOnTheClient(Object obj) {
File file = new File(MyServer.class.getResource("/com/googlecode/garbagecan/cxfstudy/ssl/test.jks").getFile());
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 = "mypassword";
String storePassword = "mypassword";
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);
httpConduit.setTlsClientParameters(tlsParams);
} catch (Exception e) {
e.printStackTrace();
}
}
}
5. 我们需要手动生成jks文件,并将其放在maven工程resources的/com/googlecode/garbagecan/cxfstudy/ssl/目录下,下面是手动生成时使用的命令
keytool -genkey -alias test -keyalg RSA -keypass mypassword -storepass mypassword -dname "CN=, OU=, O=, L=, ST=, C=" -validity 3650 -keystore test.jks
6. 最后我们可以通过启动MyServer和MyClient来验证我们的测试。




  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-327473-1-1.html 上篇帖子: 服务器优化Tip-Apache的KeepAlive On是否要开启长连接 下篇帖子: Could not resolve archetype org.apache.maven.archetypes:maven-archetype-webapp-1
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表