用于测试的Java HttpsServer

时间:2016-07-21 23:05:44

标签: java ssl

我已经更改了在我们的服务中配置SSL,我想用本地Http(s)服务器测试它。

private static HttpsServer httpsServer;

@BeforeClass
public static void server1() throws Exception {
    httpsServer = HttpsServer.create(new InetSocketAddress(8181), 0);

    KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());

    Path keyStorePath = Paths.get(System.getProperty(JAVA_HOME), KEY_STORE_PATH);
    keyStore.load(Files.newInputStream(keyStorePath),
            KEY_STORE_PASSWORD.toCharArray());

    KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance (KeyManagerFactory.getDefaultAlgorithm());
    keyManagerFactory.init ( keyStore, "changeit".toCharArray());

    TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
    trustManagerFactory.init(keyStore);

    SSLContext sslContext = SSLContext.getInstance("TLS");
    sslContext.init(keyManagerFactory.getKeyManagers(), trustManagerFactory.getTrustManagers(), null);

    httpsServer.setHttpsConfigurator ( new HttpsConfigurator( sslContext )
    {
        public void configure ( HttpsParameters params )
        {
            try
            {
                // initialise the SSL context
                SSLContext c = SSLContext.getDefault ();
                SSLEngine engine = c.createSSLEngine ();
                params.setNeedClientAuth ( false );
                params.setCipherSuites ( engine.getEnabledCipherSuites () );
                params.setProtocols ( engine.getEnabledProtocols () );

                // get the default parameters
                SSLParameters defaultSSLParameters = c.getDefaultSSLParameters ();
                params.setSSLParameters ( defaultSSLParameters );
            }
            catch ( Exception ex )
            {
                System.out.println( "Failed to create HTTPS port" );
            }
        }
    } );

    //Create a default executor
    httpsServer.setExecutor(Executors.newCachedThreadPool());
    httpsServer.start();
}

@AfterClass
public static void shutdown(){
    httpsServer.stop(0);
}

@Test
public void testNotLetsEncryptButSignedByCAs() throws Exception {
    URLConnection connection = new URL("https://localhost:8181").openConnection();
    connection.connect();
    System.out.println(connection.getHeaderFields());
}

但我一直在

   javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:980)
    at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1363)
    at 

com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)         在com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)         at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)         at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)         at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)         在java.lang.reflect.Method.invoke(Method.java:497)         在com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)     引起:java.io.EOFException:SSL对等关闭不正确         at sun.security.ssl.InputRecord.read(InputRecord.java:505)         at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:961)         ......还有41个

我在这里做错了什么?

0 个答案:

没有答案