通过HTTPS使用javax.xml.ws.Endpoint

通过HTTPS使用javax.xml.ws.Endpoint,java,soap,https,Java,Soap,Https,我正在做一个控制建筑物照明和供暖的项目。后端(用Java编写)将在MacMini上运行,并且应该可以通过SOAP访问 我想将这个项目的复杂性降到最低,因为我不希望每个使用它的人都必须设置应用服务器。到目前为止,我一直在使用javax.xml.ws.Endpoint: Endpoint endpoint = Endpoint.create(frontendInterface); String uri = "http://"+config.getHost()+":"+config.getPort

我正在做一个控制建筑物照明和供暖的项目。后端(用Java编写)将在MacMini上运行,并且应该可以通过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的方法

有没有一种方法可以在不使用应用服务器的情况下实现这一点,或者有没有其他方法可以保护此连接

您好, Marek

用于服务器:

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;
   }
});

不验证主机名(使用允许任何内容通过的
HostnameVerifier
)将删除确保通信安全的一个重要步骤。别这样!请注意,HttpsServer.create中的第二个参数是“侦听套接字上允许的最大排队传入连接数”,根据:uri变量需要类似于“/contextPath/或“/”。不幸的是,这对我不起作用。未找到上下文的错误消息处理程序。在jre8update上,我让它工作了。当cxf位于类路径上时,由于JavaSPI的更改,这不起作用