将 javax.xml.ws.Endpoint 与 HTTPS 结合使用 [英] Using javax.xml.ws.Endpoint with HTTPS
问题描述
我正在开展一个项目来控制建筑物的照明和供暖.后端(用 Java 编写)将在 Mac Mini 上运行,应该可以通过 SOAP 访问.
I'm working on a project to control light and heating in buildings. The backend (written in Java) will run on a Mac Mini and should be accessible via SOAP.
我想将这个项目的复杂性降到最低,因为我不希望每个使用它的人都必须设置一个应用程序服务器.所以到目前为止,我一直在使用 javax.xml.ws.Endpoint:
I want to keep the complexity of this project to a minimum because I don't want everyone using it having to set up an application server. So up till now I worked with 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 的方法.
This works surprisingly well (hey, when did you last see something in Java working with just 3 lines of code?), but now I'm looking for a way to use HTTPS instead of HTTP.
有没有办法在不使用应用服务器的情况下做到这一点,或者有没有其他方法来保护这个连接?
Is there a way to do this without using an application server or is there another way to secure this connection?
您好,马雷克
推荐答案
对于服务器:
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);
对于客户,请确保您这样做:
For client, be sure you do this:
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;
}
});
这篇关于将 javax.xml.ws.Endpoint 与 HTTPS 结合使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!