【问题标题】:Jetty Redirect HTTP to HTTPS using RewriteJetty 使用 Rewrite 将 HTTP 重定向到 HTTPS
【发布时间】:2016-03-17 21:18:30
【问题描述】:

我有一个嵌入式 Jetty 服务器,我想将请求从 http://localhost:8080/ 直接发送到 https://localhost:4433/ 我该怎么做?我试过这个:

    // Set up rewriting (HTTP -> HTTPS)
    RewriteHandler rewrite = new RewriteHandler();

    RedirectPatternRule redirect = new RedirectPatternRule();
    redirect.setPattern("http://localhost:8080/*");
    redirect.setLocation("https://localhost:4433/");  
    rewrite.addRule(redirect);

并在我的处理程序中添加了它,但它不起作用。我知道像

这样的规则
    RewriteHandler rewrite = new RewriteHandler();

    RedirectPatternRule redirect = new RedirectPatternRule();
    redirect.setPattern("/redirect/*");
    redirect.setLocation("/redirected");  
    rewrite.addRule(redirect);

工作 100%,但有没有办法匹配完整的 URL?

【问题讨论】:

    标签: java jsp url-rewriting jetty


    【解决方案1】:

    RewriteHandler 仅适用于路径。

    不是方案、主机或端口。

    您可以使用自 Jetty 9.2 起内置于 jetty-server 工件中的 SecuredRedirectHandler。 (确保您的两个连接器都具有有效的HttpConfiguration 设置)

    .. 或 ..

    您可以使用 Servlet 约束将其强制为机密。

    请参阅buildConstraintSecurityHandler() 回复how to programatically enforce security-constraint in web.xml

    【讨论】:

      【解决方案2】:

      不特定于 Jetty 的更通用的方法是返回带有新 URL 的 301:

      GET /path/location.html HTTP/1.1
      Host: example.com
      

      变成了

      HTTP/1.1 301 Moved Permanently
      Location: https://example.com/path/location.html
      

      这可以在 Jetty 和大多数服务器框架中完成,如果您不希望通过纯文本 http 访问资源,只需设置响应状态并在过滤器中添加位置标头。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-02-13
        • 1970-01-01
        • 2018-07-25
        • 2017-11-19
        • 1970-01-01
        • 2016-09-03
        • 2019-03-18
        • 2016-03-27
        相关资源
        最近更新 更多