我正在尝试运行一个简单的https嵌入式服务器。这是一个测试原型,所以真正的安全性在这一点上并不重要,假的或省略的证书是好的,但我的情况需要https vs常规http。
此代码似乎调用正确的API,它运行,但客户端浏览器获取“SSL连接错误”:
public class SimpleHttps {
public static final String SERVER_NAME = "https://localhost:8090";
public static final URI BASE_URI = URI.create(SERVER_NAME);
private static class TrustAllCerts implements X509TrustManager {
@Override public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException { }
@Override public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException { }
@Override public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[0]; }
}
private final static TrustManager[] trustAllCerts = new TrustManager[] { new TrustAllCerts() };
public static SSLEngineConfigurator getSslEngineConfig() throws KeyManagementException, NoSuchAlgorithmException {
SSLContext sc = SSLContext.getInstance("TLS");
sc.init(null, trustAllCerts, new SecureRandom());
SSLEngineConfigurator sslEngineConfigurator = new SSLEngineConfigurator(sc, false, false, false);
return sslEngineConfigurator;
}
public static void main(String[] args) throws Exception {
final ResourceConfig rc = new ResourceConfig().register(MyResource.class);
HttpServer httpServer = GrizzlyHttpServerFactory.createHttpServer(BASE_URI, rc, true, getSslEngineConfig());
System.in.read();
httpServer.stop();
}
}