繁体   English   中英

在与 Jetty 相同的端口上将 HTTP 重定向到 HTTPS — 识别 http 与 Z5E0561Z1050A1C4BADEA77

[英]Redirect HTTP to HTTPS on the same port with Jetty — identifying http vs. https from the Request

从 Jetty 9.4 开始。? 可以在同一端口上运行 HTTP 和 HTTPs。 它的要点是

HttpConnectionFactory http = new HttpConnectionFactory();
SslConnectionFactory https = new SslConnectionFactory(sslCtxFactory, http.getProtocol());
DetectorConnectionFactory conFactory = new DetectorConnectionFactory(https);
ServerConnector connector = new ServerConnector(server, conFactory, http);

由于我只想为 HTTPS 提供服务,因此我想将每个http://host:port/stuff重定向到https://host:port/stuff 我知道如何使用RedirectRule的子类或仅使用称为 early 的处理程序进行重定向。

我正在努力解决的问题是:我如何从请求中确定连接是 HTTP 而不是 HTTPS?

当我在调试器中Request时,我没有发现任何提示,一切看起来都好像是 http即使连接是 httpsRequest.isSecure()是假的,scheme 是http等等。 我能想到的最好的事情是:

if (Request.getHttpChannel().getEndPoint() instanceof SslConnection.DecryptedEndPoint())

这是一个带注释和剪辑的堆栈跟踪,显示了我的处理程序是如何相互包装的:

at server.HttpToHttpsRedirectRule.matchAndApply(HttpToHttpsRedirectRule.java:34)
         "^^ Here I do the matchAndApply myself and then use a jetty RedirectRule.apply"
         "The redirect works OK, but figuring whether it is HTTPS does not work"
at org.eclipse.jetty.rewrite.handler.RuleContainer.apply(RuleContainer.java:166)
at org.eclipse.jetty.rewrite.handler.RuleContainer.matchAndApply(RuleContainer.java:145)
at org.eclipse.jetty.rewrite.handler.RewriteHandler.handle(RewriteHandler.java:317)
at org.eclipse.jetty.server.handler.gzip.GzipHandler.handle(GzipHandler.java:766)
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:127)
at server.LogHandler.handle(LogHandler.java:33)
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:127)
                                   "^^^ the outermost handler"
[clip]
at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:380)
                           "^^^ we decoded the ssl and feed the lower levels plain HTTP"
[clip]
at org.eclipse.jetty.io.ssl.SslConnection$1.run(SslConnection.java:146)
                           "^^^ we are in SSL"

它在我的HttpToHttpsRedirectRule中,我想知道它是哪个连接。 有比上面提到的instanceof更明智的解决方案吗?

为了确保保留原始方案(可能还有更多)信息,有必要将SecureRequestCustomizer添加到 http 配置中,如下所示:

HttpConfiguration httpConf = new HttpConfiguration();
httpConf.addCustomizer(new SecureRequestCustomizer());
HttpConnectionFactory http = new HttpConnectionFactory(httpConf);
SslConnectionFactory https = new SslConnectionFactory(sslCtxFactory, http.getProtocol());
DetectorConnectionFactory conFactory = new DetectorConnectionFactory(https);
ServerConnector connector = new ServerConnector(server, conFactory, http);

在码头邮件列表中找到了关键提示。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM