gpt4 book ai didi

java - 安全灰熊 HttpServer (HTTPS)

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:12:31 26 4
gpt4 key购买 nike

目前我在使用 HTTPS 运行 Grizzly 服务器时遇到了问题。我将它与 Jersey 结合使用。

Grizzly 版本是:2.3.23

Jersey 版本: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 unexpectedly closed the connection

感谢您的帮助!

最佳答案

还通过添加信任库文件解决了该问题。

代码现在看起来像这样:

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));
}

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

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

关于java - 安全灰熊 HttpServer (HTTPS),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41915302/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com