Programming Tips - Java: Simple https server

Date: 2021oct19 Language: Java OS: agnostic, non-Andr*id Keywords: secure server Q. Java: Simple https server A.
import java.io.*; import java.net.InetSocketAddress; import java.lang.*; import java.net.URL; import com.sun.net.httpserver.HttpsServer; import java.security.KeyStore; import javax.net.ssl.KeyManagerFactory; import javax.net.ssl.TrustManagerFactory; import com.sun.net.httpserver.*; import javax.net.ssl.SSLEngine; import javax.net.ssl.SSLParameters; import java.io.InputStreamReader; import java.io.Reader; import java.net.URLConnection; import javax.net.ssl.HostnameVerifier; import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.SSLContext; import javax.net.ssl.SSLSession; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; import java.security.cert.X509Certificate; import java.net.InetAddress; import com.sun.net.httpserver.HttpExchange; import com.sun.net.httpserver.HttpHandler; import com.sun.net.httpserver.HttpServer; import com.sun.net.httpserver.HttpsExchange; public class SecureServer { public static class MyHandler implements HttpHandler { @Override public void handle(HttpExchange t) throws IOException { String response = "This is the response"; HttpsExchange httpsExchange = (HttpsExchange) t; t.getResponseHeaders().add("Access-Control-Allow-Origin", "*"); t.sendResponseHeaders(200, response.getBytes().length); OutputStream os = t.getResponseBody(); os.write(response.getBytes()); os.close(); } } public static void start() { final int port = 8443; try { // Setup the socket address InetSocketAddress address = new InetSocketAddress(port); // Initialize the HTTPS server HttpsServer httpsServer = HttpsServer.create(address, 0); SSLContext sslContext = SSLContext.getInstance("TLS"); // Initialize the keystore char[] password = "helloworld".toCharArray(); KeyStore ks = KeyStore.getInstance("JKS"); FileInputStream fis = new FileInputStream("c:/etc/testkey.jks"); ks.load(fis, password); // Setup the key manager factory KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509"); kmf.init(ks, password); // Setup the trust manager factory TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509"); tmf.init(ks); // Setup the HTTPS context and parameters sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null); httpsServer.setHttpsConfigurator(new HttpsConfigurator(sslContext) { public void configure(HttpsParameters params) { try { // Initialize the SSL context SSLContext context = getSSLContext(); SSLEngine engine = context.createSSLEngine(); params.setNeedClientAuth(false); params.setCipherSuites(engine.getEnabledCipherSuites()); params.setProtocols(engine.getEnabledProtocols()); // Set the SSL parameters SSLParameters sslParameters = context.getSupportedSSLParameters(); params.setSSLParameters(sslParameters); } catch (Exception ex) { System.out.prntln("Failed to create HTTPS port " + ex.toString()); } } }); httpsServer.createContext("/test", new MyHandler()); httpsServer.setExecutor(null); // creates a default executor httpsServer.start(); } catch (Exception ex) { System.out.println("Failed to create HTTPS server on port " + port + " of localhost " + ex.toString()); ex.printStackTrace(); } }
// Usage SecureServer.start();
Based on https://stackoverflow.com/questions/2308479/simple-java-https-server by Krishnakumar Sekar