2011-01-07 35 views
7

Binalarda ışık ve ısınmayı denetlemek için bir proje üzerinde çalışıyorum. Arka uç (Java'da yazılmış) bir Mac Mini'de çalışır ve SOAP üzerinden erişilebilir olmalıdır.javax.xml.ws.Endpoint'i HTTPS ile kullanma

Bu projenin karmaşıklığını en aza indirmek istiyorum çünkü herkesin bir uygulama sunucusu kurmak zorunda kalmasını istemiyorum. Yani şu ana kadar ben javax.xml.ws.Endpoint ile çalıştı:

Endpoint endpoint = Endpoint.create(frontendInterface); 
String uri = "http://"+config.getHost()+":"+config.getPort()+config.getPath(); 

endpoint.publish(uri); 

Bu (? Hey, ne zaman son kod sadece 3 satır ile çalışan Java şey mi gördün) şaşırtıcı derecede iyi çalışıyor, ama şimdi HTTP yerine HTTPS kullanmanın bir yolunu arıyorum.

Bir uygulama sunucusu kullanmadan bunu yapmanın bir yolu var mı yoksa bu bağlantıyı güvenli hale getirmenin başka bir yolu var mı?

Selamlar, sunucusu için Marek

cevap

15

: istemci için

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

, bunu emin olun:

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

Vay - teşekkürler, bu gerçekten işe yarıyor! Neredeyse umudunu yitirmiştim ... – marekventur

+3

Ana bilgisayar adını doğrulamıyor (bu, herşeyi sağlayan bir "HostnameVerifier" ile iletişimi güvenceye almak için önemli bir adımı atıyor). Yapma! – Bruno

+1

Lütfen HttpsServer.create'daki ikinci argümanın "dinleme soketine izin vermek için sıraya konan gelen bağlantıların maksimum sayısı" olduğunu unutmayın: http://docs.oracle.com/javase/6/docs/jre/api/ net/HTTPServer/spec/com/güneş/net/HTTPServer/HttpsServer.html – zpon