使用带有HTTPS的javax.xml.ws.Endpoint

时间:2011-01-07 00:02:25

标签: java soap https

我正在开展一个控制建筑物光照和供暖的项目。后端(用Java编写)将在Mac Mini上运行,并且应该可以通过SOAP访问。

我希望将此项目的复杂性保持在最低限度,因为我不希望每个人都使用它来设置应用程序服务器。到目前为止,我一直使用javax.xml.ws.Endpoint:

 Endpoint endpoint = Endpoint.create(frontendInterface);
 String uri = "http://"+config.getHost()+":"+config.getPort()+config.getPath();

 endpoint.publish(uri);

这个效果非常好(嘿,你们上次在Java中看到的东西只用了3行代码吗?),但现在我正在寻找一种使用HTTPS而不是HTTP的方法。

有没有办法在不使用应用程序服务器的情况下执行此操作,还是有其他方法来保护此连接?

问候, 马立克

1 个答案:

答案 0 :(得分:16)

对于服务器:

SSLContext ssl = SSLContext.getInstance("TLS");

KeyManagerFactory keyFactory = KeyManagerFactory                    .getInstance(KeyManagerFactory.getDefaultAlgorithm());
KeyStore store = KeyStore.getInstance("JKS");

store.load(new FileInputStream(keystoreFile),keyPass.toCharArray());

keyFactory.init(store, keyPass.toCharArray());


TrustManagerFactory trustFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());

trustFactory.init(store);

ssl.init(keyFactory.getKeyManagers(),
trustFactory.getTrustManagers(), new SecureRandom());

HttpsConfigurator configurator = new HttpsConfigurator(ssl);

HttpsServer httpsServer = HttpsServer.create(new InetSocketAddress(hostname, port), port);

httpsServer.setHttpsConfigurator(configurator);

HttpContext httpContext = httpsServer.createContext(uri);

httpsServer.start();

endpoint.publish(httpContext);

对于客户,请务必执行此操作:

System.setProperty("javax.net.ssl.trustStore", "path");
System.setProperty("javax.net.ssl.keyStore", "password");
System.setProperty("javax.net.ssl.keyStorePassword", "password");
System.setProperty("javax.net.ssl.keyStoreType", "JKS");
//done to prevent CN verification in client keystore
HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
   @Override
   public boolean verify(String hostname, SSLSession session) {
     return true;
   }
});