gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:51:45 24 4
gpt4 key购买 nike

我正在从事一个控制建筑物中的光和暖气的项目。后端(用 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 的方法。

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

您好,马立克

最佳答案

对于服务器:

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

关于java - 将 javax.xml.ws.Endpoint 与 HTTPS 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4621313/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com