【问题标题】:Secure Grizzly HttpServer (HTTPS)安全 Grizzly HttpServer (HTTPS)
【发布时间】:2017-06-14 09:27:28
【问题描述】:

目前我无法让我的 Grizzly 服务器使用 HTTPS 运行。我将它与泽西岛结合使用。

Grizzly 版本是:2.3.23

球衣版本:2.24.1

我是这样启动服务器的:

public class Main {
public static final String BASE_URI = "https://localhost:8443/api/";
private static final String KEYSTORE_LOC = "I:\\rest-api\\keystore";
//private static final String KEYSTORE_LOC = "./server.cert";
private static final String KEYSTORE_PASS= "somepw123";

public static HttpServer startServer() {

    final ResourceConfig rc = new ResourceConfig()
            .register(MultiPartFeature.class)
            .packages("com.restapi");


    SSLContextConfigurator sslCon = new SSLContextConfigurator();

    sslCon.setKeyStoreFile(KEYSTORE_LOC);
    sslCon.setKeyStorePass(KEYSTORE_PASS);

    return GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc, true,new SSLEngineConfigurator(sslCon).setClientMode(false).setNeedClientAuth(false));
}

keystore文件是用keytool生成的

keytool -genkey -keystore ./keystore -alias serverKey -dname

当我尝试打开浏览器告诉我的 URL 时

localhost 意外关闭连接

感谢您的帮助!

【问题讨论】:

    标签: java rest ssl jersey grizzly


    【解决方案1】:

    通过添加 Truststore 文件也解决了该问题。

    代码现在看起来像这样:

    public class Main {
    public static final String BASE_URI = "https://localhost:8443/api/";
    private static final String KEYSTORE_LOC = "./keystore_server";
    private static final String KEYSTORE_PASS= "keystorePass";
    
    private static final String TRUSTSTORE_LOC = "./truststore_server";
    private static final String TRUSTSTORE_PASS = "truststorePass";
    
    private static HttpServer startServer() {
    
        final ResourceConfig rc = new ResourceConfig()
                .register(MultiPartFeature.class)
                .packages("com.restapi");
    
    
        SSLContextConfigurator sslCon = new SSLContextConfigurator();
    
        sslCon.setKeyStoreFile(KEYSTORE_LOC);
        sslCon.setKeyStorePass(KEYSTORE_PASS);
    
        sslCon.setTrustStoreFile(TRUSTSTORE_LOC);
        sslCon.setTrustStorePass(TRUSTSTORE_PASS);
    
        return GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc, true,new SSLEngineConfigurator(sslCon).setClientMode(false).setNeedClientAuth(false));
    }
    

    使用这些命令创建的密钥和信任库文件:

    keytool -genkey -keyalg RSA -keystore ./keystore_client -alias clientKey
    keytool -export -alias clientKey -rfc -keystore ./keystore_client > ./client.cert
    keytool -import -alias clientCert -file ./client.cert -keystore ./truststore_server
    
    keytool -genkey -keyalg RSA -keystore ./keystore_server -alias serverKey
    keytool -export -alias serverKey -rfc -keystore ./keystore_server > ./server.cert
    keytool -import -alias serverCert -file ./server.cert -keystore ./truststore_client
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-24
      • 2015-05-02
      • 1970-01-01
      • 1970-01-01
      • 2018-05-03
      • 1970-01-01
      • 2011-07-05
      相关资源
      最近更新 更多