将 javax.xml.ws.Endpoint 与 HTTPS 结合使用

发布于 2024-10-10 17:53:33 字数 503 浏览 2 评论 0原文

我正在开展一个控制建筑物内灯光和供暖的项目。后端(用 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。

有没有一种方法可以在不使用应用程序服务器的情况下执行此操作,或者是否有其他方法来保护此连接?

问候, 马雷克

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.

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

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?

Greetings,
Marek

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

姜生凉生 2024-10-17 17:53:33

对于服务器:

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

For server:

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;
   }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文