Java 如何使用SSL使Jetty正常工作
很抱歉这个糟糕的标题,我真的不确定这里发生了什么。我正在尝试让以前使用http的jetty服务器现在使用https。在过去的几天里,我关注了几篇文章,一直在玩弄它,但我不知道我做错了什么 这是我设置和启动服务器的代码:Java 如何使用SSL使Jetty正常工作,java,rest,ssl,jetty,Java,Rest,Ssl,Jetty,很抱歉这个糟糕的标题,我真的不确定这里发生了什么。我正在尝试让以前使用http的jetty服务器现在使用https。在过去的几天里,我关注了几篇文章,一直在玩弄它,但我不知道我做错了什么 这是我设置和启动服务器的代码: // Create jetty server this.server = new Server(); HttpConfiguration http_config = new HttpConfiguration(); http_config.set
// Create jetty server
this.server = new Server();
HttpConfiguration http_config = new HttpConfiguration();
http_config.setSecureScheme("https");
http_config.setSecurePort(3972);
// Configure Connector for http
ServerConnector http = new ServerConnector(server, new HttpConnectionFactory(http_config));
http.setPort(3971);
http.setIdleTimeout(300000);
// HTTPS Configuration
HttpConfiguration https_config = new HttpConfiguration(http_config);
https_config.addCustomizer(new SecureRequestCustomizer());
SslContextFactory sslContextFactory = new SslContextFactory();
sslContextFactory.setKeyStorePath("data/derkle.jks");
sslContextFactory.setKeyStorePassword("XXXXXXXXXX");
ServerConnector sslConnector = new ServerConnector(server, new SslConnectionFactory(sslContextFactory, "http/1.1"), new HttpConnectionFactory(https_config));
sslConnector.setPort(portSecureNo);
server.setConnectors(new Connector[] { http, sslConnector });
this.server.setHandler(vaultServer);
this.server.start();
this.server.join();
这运行正常,线程在server.join()上成功连接。我得到以下输出:
2016-01-22 10:52:30.587:INFO::main: Logging initialized @269ms
2016-01-22 10:52:42.124:INFO:oejs.Server:main: jetty-9.2.14.v20151106
2016-01-22 10:52:42.227:INFO:oejs.ServerConnector:main: Started ServerConnector@5674fb70{HTTP/1.1}{0.0.0.0:3971}
2016-01-22 10:52:43.044:INFO:oejs.ServerConnector:main: Started ServerConnector@d9f8120{SSL-http/1.1}{0.0.0.0:3972}
2016-01-22 10:52:43.045:INFO:oejs.Server:main: Started @12729ms
所有这些看起来都不错,但当我尝试向它发送请求时,我似乎没有得到任何传递到AbstractHandler实现的信息。它的句柄函数如下所示:
@Override
public void handle(
String target,
Request baseRequest,
HttpServletRequest request,
HttpServletResponse response) throws IOException, ServletException {
Validate.isTrue(this.endPointManager != null, "This has not been initialised.");
target = target.replace('\\', '/');
if (target.startsWith("/"))
target = target.substring(1);
if (target.endsWith("/"))
target = target.substring(0, target.length() - 1);
String[] targets = stelar.extx.StringUtil.stringSplit(target, '/');
if ((targets.length == 1) && (targets[0].trim().equalsIgnoreCase(STATUS_URI))) {
response.setStatus(HttpServletResponse.SC_OK);
baseRequest.setHandled(true);
return;
}
else {
// Display help is the help uri is passed, or not uri is passed
if ((target.isEmpty()) || (targets[0].equalsIgnoreCase(IURLHandler.HELP_URI))) {
displayHelp(this.rootHandler, baseRequest, response);
return;
}
}
parseURI(targets, 0, baseRequest, request, response, this.rootHandler);
}
我正在从“高级REST客户端”向服务器发送请求,该客户端是一个chrome扩展。我也尝试过Curl,但似乎无法使用它
我已尝试拨打以下地址:
https://localhost:3971/
https://localhost:3971
https://localhost:3972/
https://localhost:3972
编辑:
所以我决定恢复使用http作为一个健全的检查,我注意到它也停止了工作。自那以后,我唯一改变的是我使用的jetty版本。最初我使用了一个名为jetty all的库,但它似乎不包含对SSL的支持,所以我切换到jetty-Distribution-9.2.14.v20151106,它被拆分为多个jar文件。我想知道是否需要包含这些附加库中的一些(尽管我没有遇到任何编译问题,所以看起来情况并非如此)。您是否考虑过使用ApacheHTTPD作为反向代理?IMHO是添加HTTPS和其他功能的最快方式,而无需接触应用程序服务器。(Apache只是一个选项,您也可以查看nginx和其他)您是否考虑过使用Apache HTTPD作为反向代理?IMHO是添加HTTPS和其他功能的最快方式,而无需接触应用程序服务器。(Apache只是一个选项,您也可以查看nginx和其他)