@Override public HttpServer createHttpServerInstance(final int port) throws IOException { try { final HttpsServer server = HttpsServer.create(new InetSocketAddress(port), 5); server.setHttpsConfigurator(new HttpsConfigurator(sslContext) { @Override public void configure(final HttpsParameters params) { final SSLContext c = getSSLContext(); // get the default parameters final SSLParameters sslparams = c.getDefaultSSLParameters(); params.setSSLParameters(sslparams); // statement above could throw IAE if any params invalid. // eg. if app has a UI and parameters supplied by a user. } }); s_logger.info("create HTTPS server instance on port: " + port); return server; } catch (final Exception ioe) { s_logger.error(ioe.toString(), ioe); } return null; }
protected HttpsServer startHttpsServer(SSLContext ctx) throws Exception { InetSocketAddress localhost = new InetSocketAddress("127.0.0.59", 59995); HttpsServer server = HttpsServer.create(localhost, 0); server.setHttpsConfigurator(new HttpsConfigurator(ctx)); server.createContext("/", (t) -> { byte[] data = "success".getBytes(); t.sendResponseHeaders(HttpURLConnection.HTTP_OK, data.length); OutputStream o = t.getResponseBody(); o.write(data); o.close(); }); server.setExecutor(null); server.start(); return server; }
/** * Internal method which creates and starts the server. * * @param httpsMode True if the server to be started is HTTPS, false otherwise. * @return Started server. */ private static GridEmbeddedHttpServer createAndStart(boolean httpsMode) throws Exception { HttpServer httpSrv; InetSocketAddress addrToBind = new InetSocketAddress(HOSTNAME_TO_BIND_SRV, getAvailablePort()); if (httpsMode) { HttpsServer httpsSrv = HttpsServer.create(addrToBind, 0); httpsSrv.setHttpsConfigurator(new HttpsConfigurator(GridTestUtils.sslContext())); httpSrv = httpsSrv; } else httpSrv = HttpServer.create(addrToBind, 0); GridEmbeddedHttpServer embeddedHttpSrv = new GridEmbeddedHttpServer(); embeddedHttpSrv.proto = httpsMode ? "https" : "http"; embeddedHttpSrv.httpSrv = httpSrv; embeddedHttpSrv.httpSrv.start(); return embeddedHttpSrv; }
private static HttpServer initHttpsServer(InputStream keystoreInputSteam, char[] password) throws IOException { try { KeyStore ks = KeyStore.getInstance("JKS"); ks.load(keystoreInputSteam, password); KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509"); kmf.init(ks, password); TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509"); tmf.init(ks); SSLContext sslContext = SSLContext.getInstance("TLS"); sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null); HttpsServer httpsServer = HttpsServer.create(); httpsServer.setHttpsConfigurator(new HttpsConfigurator(sslContext)); return httpsServer; } catch (NoSuchAlgorithmException | CertificateException | KeyStoreException | UnrecoverableKeyException | KeyManagementException e) { throw new RuntimeException(e); } }
/** * Https Server */ public void startHttpsServer() throws IOException, NoSuchAlgorithmException { httpsServer = com.sun.net.httpserver.HttpsServer.create(new InetSocketAddress(0), 0); httpsServer.createContext("/", new MyHandler()); httpsServer.setHttpsConfigurator(new HttpsConfigurator(SSLContext.getDefault())); httpsServer.start(); }
/** * Https Server */ public void startHttpsServer() throws IOException, NoSuchAlgorithmException { httpsServer = com.sun.net.httpserver.HttpsServer.create(new InetSocketAddress(0), 0); httpsServer.createContext("/test6614957/", new MyHandler()); httpsServer.setHttpsConfigurator(new HttpsConfigurator(SSLContext.getDefault())); httpsServer.start(); }
public static void initServer() throws IOException { Logger logger = Logger.getLogger("com.sun.net.httpserver"); ConsoleHandler ch = new ConsoleHandler(); logger.setLevel(Level.ALL); ch.setLevel(Level.ALL); logger.addHandler(ch); String root = System.getProperty("test.src") + "/docs"; InetSocketAddress addr = new InetSocketAddress(0); httpServer = HttpServer.create(addr, 0); if (httpServer instanceof HttpsServer) { throw new RuntimeException("should not be httpsserver"); } httpsServer = HttpsServer.create(addr, 0); HttpHandler h = new FileServerHandler(root); HttpContext c1 = httpServer.createContext("/files", h); HttpContext c2 = httpsServer.createContext("/files", h); HttpContext c3 = httpServer.createContext("/echo", new EchoHandler()); redirectHandler = new RedirectHandler("/redirect"); redirectHandlerSecure = new RedirectHandler("/redirect"); HttpContext c4 = httpServer.createContext("/redirect", redirectHandler); HttpContext c41 = httpsServer.createContext("/redirect", redirectHandlerSecure); HttpContext c5 = httpsServer.createContext("/echo", new EchoHandler()); HttpContext c6 = httpServer.createContext("/keepalive", new KeepAliveHandler()); redirectErrorHandler = new RedirectErrorHandler("/redirecterror"); redirectErrorHandlerSecure = new RedirectErrorHandler("/redirecterror"); HttpContext c7 = httpServer.createContext("/redirecterror", redirectErrorHandler); HttpContext c71 = httpsServer.createContext("/redirecterror", redirectErrorHandlerSecure); delayHandler = new DelayHandler(); HttpContext c8 = httpServer.createContext("/delay", delayHandler); HttpContext c81 = httpsServer.createContext("/delay", delayHandler); executor = Executors.newCachedThreadPool(); httpServer.setExecutor(executor); httpsServer.setExecutor(executor); ctx = new SimpleSSLContext().get(); httpsServer.setHttpsConfigurator(new HttpsConfigurator(ctx)); httpServer.start(); httpsServer.start(); port = httpServer.getAddress().getPort(); System.out.println("HTTP server port = " + port); httpsport = httpsServer.getAddress().getPort(); System.out.println("HTTPS server port = " + httpsport); httproot = "http://127.0.0.1:" + port + "/"; httpsroot = "https://127.0.0.1:" + httpsport + "/"; proxy = new ProxyServer(0, false); proxyPort = proxy.getPort(); System.out.println("Proxy port = " + proxyPort); }
private static HttpsServer createHttpsServer(int port) throws Exception { generateCertificate(); HttpsServer httpsServer = HttpsServer.create(new InetSocketAddress(port), 0); SSLContext sslContext = getSslContext(); httpsServer.setHttpsConfigurator(new HttpsConfigurator(sslContext)); return httpsServer; }
public static void startHttpsServer() throws CertificateException, IOException, KeyManagementException, KeyStoreException, NoSuchAlgorithmException, UnrecoverableKeyException { HttpsServer httpsServer = HttpsServer.create(new InetSocketAddress(443), 0); char[] keystorePassword = "password".toCharArray(); SSLContext sslContext = SSLContext.getInstance("TLS"); KeyStore keyStore = KeyStore.getInstance("JKS"); keyStore.load(new FileInputStream("keystore.jks"), keystorePassword); KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance("SunX509"); keyManagerFactory.init(keyStore, keystorePassword); sslContext.init(keyManagerFactory.getKeyManagers(), null, null); HttpsConfigurator configurator = new HttpsConfigurator(sslContext); httpsServer.createContext("/example", new ExampleHandler()); httpsServer.setHttpsConfigurator(configurator); httpsServer.setExecutor(null); httpsServer.start(); }
protected void startHttpsServer() throws Exception { final InetSocketAddress addr = new InetSocketAddress(httpsServerPort); httpsServer = HttpsServer.create(addr, 0); httpsServer.setExecutor(Executors.newCachedThreadPool()); attachHttpHandlers(httpsServer); final char[] passphrase = KEYSTORE_PASSWORD.toCharArray(); final KeyStore ks = KeyStore.getInstance("JKS"); ks.load(getKeyStore(), passphrase); final KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); kmf.init(ks, passphrase); final TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); tmf.init(ks); final SSLContext ssl = SSLContext.getInstance("TLS"); ssl.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null); httpsServer.setHttpsConfigurator(new HttpsConfigurator(ssl) { @Override public void configure(final HttpsParameters params) { final SSLContext c = getSSLContext(); final SSLParameters sslparams = c.getDefaultSSLParameters(); params.setSSLParameters(sslparams); } }); httpsServer.start(); }
@Override public HttpServer createHttpServerInstance(int port) throws IOException { try { HttpsServer server = HttpsServer.create(new InetSocketAddress(port), 5); server.setHttpsConfigurator(new HttpsConfigurator(sslContext) { @Override public void configure(HttpsParameters params) { // get the remote address if needed InetSocketAddress remote = params.getClientAddress(); SSLContext c = getSSLContext(); // get the default parameters SSLParameters sslparams = c.getDefaultSSLParameters(); params.setSSLParameters(sslparams); params.setProtocols(SSLUtils.getRecommendedProtocols()); params.setCipherSuites(SSLUtils.getRecommendedCiphers()); // statement above could throw IAE if any params invalid. // eg. if app has a UI and parameters supplied by a user. } }); s_logger.info("create HTTPS server instance on port: " + port); return server; } catch (Exception ioe) { s_logger.error(ioe.toString(), ioe); } return null; }
@Test public void shouldSetHttpsParametersOnEveryRequest() throws Exception { // setup final SSLParameters defaultSslParameters = mock(SSLParameters.class); final String[] cipherSuites = new String[0]; final String[] protocols = new String[0]; SSLEngine sslEngine = mock(SSLEngine.class); when(sslEngine.getEnabledCipherSuites()).thenReturn(cipherSuites); when(sslEngine.getEnabledProtocols()).thenReturn(protocols); when(sslContext.getDefaultSSLParameters()).thenReturn(defaultSslParameters); when(sslContext.createSSLEngine()).thenReturn(sslEngine); doAnswer(new Answer<Object>() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { HttpsParameters httpsParameters = mock(HttpsParameters.class); HttpsConfigurator httpsConfigurator = (HttpsConfigurator) invocation.getArguments()[0]; httpsConfigurator.configure(httpsParameters); verify(httpsParameters).setSSLParameters(defaultSslParameters); verify(httpsParameters).setNeedClientAuth(false); verify(httpsParameters).setWantClientAuth(false); verify(httpsParameters).setCipherSuites(cipherSuites); verify(httpsParameters).setProtocols(protocols); return null; } }).when(httpsServer).setHttpsConfigurator(isA(HttpsConfigurator.class)); // act HttpServer result = simpleHttpsServerFactoryBean.getInitializedServer(inetSocketAddress); // assert verify(httpsServer).setHttpsConfigurator(isA(HttpsConfigurator.class)); assertEquals(httpsServer, result); }
private HttpsConfigurator createHttpsConfigurator() throws Exception { KeyStore keyStore = loadKeyStore(); SSLContext sslContext = SSLContext.getInstance("TLS"); sslContext.init(createKeyManagerFactory(keyStore).getKeyManagers(), createTrustManagerFactory(keyStore) .getTrustManagers(), null); return new HttpsConfigurator(sslContext); }
private HttpsConfigurator getConfigurator(SSLContext sslContext) { return new HttpsConfigurator(sslContext) { @Override public void configure (HttpsParameters params) { SSLContext context = getSSLContext(); SSLParameters sslParams = context.getDefaultSSLParameters(); params.setNeedClientAuth(false); params.setSSLParameters(sslParams); } }; }