使用带有HTTPS的javax.xml.ws.Endpoint [英] Using javax.xml.ws.Endpoint with HTTPS

查看:173
本文介绍了使用带有HTTPS的javax.xml.ws.Endpoint的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

问候,
Marek

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

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

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

这篇关于使用带有HTTPS的javax.xml.ws.Endpoint的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆