Search in sources :

Example 1 with HttpsParameters

use of com.sun.net.httpserver.HttpsParameters in project cosmic by MissionCriticalCloud.

the class ConsoleProxySecureServerFactoryImpl method createHttpServerInstance.

@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;
}
Also used : HttpsConfigurator(com.sun.net.httpserver.HttpsConfigurator) SSLParameters(javax.net.ssl.SSLParameters) InetSocketAddress(java.net.InetSocketAddress) HttpsParameters(com.sun.net.httpserver.HttpsParameters) SSLContext(javax.net.ssl.SSLContext) HttpsServer(com.sun.net.httpserver.HttpsServer) IOException(java.io.IOException)

Example 2 with HttpsParameters

use of com.sun.net.httpserver.HttpsParameters in project ribbon by Netflix.

the class MockHttpServer method before.

public void before(final Description description) throws Exception {
    this.service = Executors.newFixedThreadPool(threadCount, new ThreadFactoryBuilder().setDaemon(true).setNameFormat("TestHttpServer-%d").build());
    InetSocketAddress inetSocketAddress = new InetSocketAddress("localhost", 0);
    if (hasSsl) {
        byte[] sampleTruststore1 = Base64.decode(TEST_TS1);
        byte[] sampleKeystore1 = Base64.decode(TEST_KS1);
        keystore = File.createTempFile("SecureAcceptAllGetTest", ".keystore");
        truststore = File.createTempFile("SecureAcceptAllGetTest", ".truststore");
        FileOutputStream keystoreFileOut = new FileOutputStream(keystore);
        try {
            keystoreFileOut.write(sampleKeystore1);
        } finally {
            keystoreFileOut.close();
        }
        FileOutputStream truststoreFileOut = new FileOutputStream(truststore);
        try {
            truststoreFileOut.write(sampleTruststore1);
        } finally {
            truststoreFileOut.close();
        }
        KeyStore ks = KeyStore.getInstance("JKS");
        ks.load(new FileInputStream(keystore), PASSWORD.toCharArray());
        KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
        kmf.init(ks, PASSWORD.toCharArray());
        KeyStore ts = KeyStore.getInstance("JKS");
        ts.load(new FileInputStream(truststore), PASSWORD.toCharArray());
        TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
        tmf.init(ts);
        SSLContext sc = SSLContext.getInstance("TLS");
        sc.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
        HttpsServer secureServer = HttpsServer.create(inetSocketAddress, 0);
        secureServer.setHttpsConfigurator(new HttpsConfigurator(sc) {

            public void configure(HttpsParameters params) {
                SSLContext c = getSSLContext();
                SSLParameters sslparams = c.getDefaultSSLParameters();
                params.setSSLParameters(sslparams);
            }
        });
        server = secureServer;
    } else {
        server = HttpServer.create(inetSocketAddress, 0);
    }
    server.setExecutor(service);
    for (Entry<String, HttpHandler> handler : handlers.entrySet()) {
        server.createContext(handler.getKey(), handler.getValue());
    }
    server.start();
    localHttpServerPort = server.getAddress().getPort();
    System.out.println(description.getClassName() + " TestServer is started: " + getServerUrl());
}
Also used : HttpHandler(com.sun.net.httpserver.HttpHandler) HttpsConfigurator(com.sun.net.httpserver.HttpsConfigurator) InetSocketAddress(java.net.InetSocketAddress) HttpsParameters(com.sun.net.httpserver.HttpsParameters) SSLContext(javax.net.ssl.SSLContext) KeyStore(java.security.KeyStore) FileInputStream(java.io.FileInputStream) KeyManagerFactory(javax.net.ssl.KeyManagerFactory) SSLParameters(javax.net.ssl.SSLParameters) FileOutputStream(java.io.FileOutputStream) TrustManagerFactory(javax.net.ssl.TrustManagerFactory) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) HttpsServer(com.sun.net.httpserver.HttpsServer)

Example 3 with HttpsParameters

use of com.sun.net.httpserver.HttpsParameters in project gradle by gradle.

the class BlockingHttpsServer method configure.

/**
 * @param testKeyStore The key store to configure this server from.
 * @param tlsProtocolFilter Used to prune the supported set of TLS versions
 */
public void configure(TestKeyStore testKeyStore, Predicate<String> tlsProtocolFilter) {
    HttpsServer httpsServer = (HttpsServer) this.server;
    SSLContext context = testKeyStore.asSSLContext();
    httpsServer.setHttpsConfigurator(new HttpsConfigurator(context) {

        @Override
        public void configure(HttpsParameters params) {
            SSLContext c = getSSLContext();
            SSLEngine engine = c.createSSLEngine();
            params.setNeedClientAuth(false);
            params.setCipherSuites(engine.getEnabledCipherSuites());
            // TLS protocols need to be filtered off both the HttpsParameters & SSLParameters
            params.setProtocols(stripFilteredProtocols(engine.getEnabledProtocols()));
            SSLParameters parameters = c.getDefaultSSLParameters();
            parameters.setProtocols(stripFilteredProtocols(parameters.getProtocols()));
            params.setSSLParameters(parameters);
        }

        private String[] stripFilteredProtocols(String[] allProtocols) {
            return Arrays.stream(allProtocols).filter(tlsProtocolFilter).toArray(String[]::new);
        }
    });
}
Also used : HttpsConfigurator(com.sun.net.httpserver.HttpsConfigurator) SSLParameters(javax.net.ssl.SSLParameters) SSLEngine(javax.net.ssl.SSLEngine) HttpsParameters(com.sun.net.httpserver.HttpsParameters) SSLContext(javax.net.ssl.SSLContext) HttpsServer(com.sun.net.httpserver.HttpsServer)

Example 4 with HttpsParameters

use of com.sun.net.httpserver.HttpsParameters in project languagetool by languagetool-org.

the class HTTPSServer method getConfigurator.

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);
        }
    };
}
Also used : HttpsConfigurator(com.sun.net.httpserver.HttpsConfigurator) SSLParameters(javax.net.ssl.SSLParameters) HttpsParameters(com.sun.net.httpserver.HttpsParameters) SSLContext(javax.net.ssl.SSLContext)

Example 5 with HttpsParameters

use of com.sun.net.httpserver.HttpsParameters in project GNS by MobilityFirst.

the class GNSHttpsServer method tryPort.

/**
   * Try to start the http server at the port.
   *
   * @param port
   * @return true if it was started
   */
@Override
public boolean tryPort(int port) {
    try {
        InetSocketAddress addr = new InetSocketAddress(port);
        httpsServer = HttpsServer.create(addr, 0);
        SSLContext sslContext = createSSLContext();
        httpsServer.setHttpsConfigurator(new HttpsConfigurator(sslContext) {

            @Override
            public void configure(HttpsParameters parameters) {
                // initialise the SSL context
                SSLContext context = getSSLContext();
                SSLEngine engine = context.createSSLEngine();
                //parameters.setNeedClientAuth(false);
                parameters.setCipherSuites(engine.getEnabledCipherSuites());
                parameters.setProtocols(engine.getEnabledProtocols());
                // get the default parameters
                SSLParameters sslParameters = context.getDefaultSSLParameters();
                sslParameters.setNeedClientAuth(true);
                parameters.setNeedClientAuth(true);
                parameters.setSSLParameters(sslParameters);
            }
        });
        httpsServer.createContext("/", new EchoHttpHandler());
        httpsServer.createContext("/" + GNS_PATH, new DefaultHttpHandler());
        httpsServer.setExecutor(Executors.newCachedThreadPool());
        httpsServer.start();
        // Need to do this for the places where we expose the secure http service to the user
        requestHandler.setHttpsServerPort(port);
        LOG.log(Level.INFO, "HTTPS server is listening on port {0}", port);
        return true;
    } catch (BindException e) {
        LOG.log(Level.FINE, "HTTPS server failed to start on port {0} due to {1}", new Object[] { port, e.getMessage() });
        return false;
    } catch (IOException | NoSuchAlgorithmException | KeyStoreException | CertificateException | UnrecoverableKeyException | KeyManagementException e) {
        LOG.log(Level.FINE, "HTTPS server failed to start on port {0} due to {1}", new Object[] { port, e.getMessage() });
        e.printStackTrace();
        return false;
    }
}
Also used : HttpsConfigurator(com.sun.net.httpserver.HttpsConfigurator) InetSocketAddress(java.net.InetSocketAddress) SSLEngine(javax.net.ssl.SSLEngine) HttpsParameters(com.sun.net.httpserver.HttpsParameters) BindException(java.net.BindException) CertificateException(java.security.cert.CertificateException) SSLContext(javax.net.ssl.SSLContext) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyStoreException(java.security.KeyStoreException) KeyManagementException(java.security.KeyManagementException) SSLParameters(javax.net.ssl.SSLParameters) UnrecoverableKeyException(java.security.UnrecoverableKeyException)

Aggregations

HttpsConfigurator (com.sun.net.httpserver.HttpsConfigurator)7 HttpsParameters (com.sun.net.httpserver.HttpsParameters)7 SSLContext (javax.net.ssl.SSLContext)7 SSLParameters (javax.net.ssl.SSLParameters)7 HttpsServer (com.sun.net.httpserver.HttpsServer)5 InetSocketAddress (java.net.InetSocketAddress)5 IOException (java.io.IOException)3 SSLEngine (javax.net.ssl.SSLEngine)3 ThreadFactoryBuilder (com.google.common.util.concurrent.ThreadFactoryBuilder)1 HttpHandler (com.sun.net.httpserver.HttpHandler)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 BindException (java.net.BindException)1 KeyManagementException (java.security.KeyManagementException)1 KeyStore (java.security.KeyStore)1 KeyStoreException (java.security.KeyStoreException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 UnrecoverableKeyException (java.security.UnrecoverableKeyException)1 CertificateException (java.security.cert.CertificateException)1 KeyManagerFactory (javax.net.ssl.KeyManagerFactory)1