【问题标题】:Redirect HTTP to HTTPS in Undertow在 Undertow 中将 HTTP 重定向到 HTTPS
【发布时间】:2015-03-11 12:25:52
【问题描述】:

如何在 Undertow 中配置 HTTP->HTTPS 重定向?我查看了 Undertow 的代码库,有些类似乎是相关的(例如 RedirectHandler)。此外,Undertow 文档 (Predicates Attributes and Handlers) 似乎正是针对这个问题。但我不知道从哪里开始。

基本上,我正在寻找的是 Apache 的 mod_rewrite 配置的对应物:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

谢谢!

【问题讨论】:

    标签: https rewrite undertow


    【解决方案1】:

    This answer 指向了正确的方向。以编程方式,必须将SecurityConstraint 添加到Builder 并设置ConfidentialPortManager

    DeploymentInfo servletBuilder = Servlets.deployment();
    servletBuilder.addSecurityConstraint(new SecurityConstraint()
      .addWebResourceCollection(new WebResourceCollection()
        .addUrlPattern("/*"))
      .setTransportGuaranteeType(TransportGuaranteeType.CONFIDENTIAL)
      .setEmptyRoleSemantic(EmptyRoleSemantic.PERMIT))
      .setConfidentialPortManager(new ConfidentialPortManager() {
        @Override
        public int getConfidentialPort(HttpServerExchange exchange) {
          return 443;
        }
      }
    );
    

    【讨论】:

    • 这种方法可以使用301永久重定向代替默认的302重定向吗?
    • @Thermometer - 不确定,从未尝试使用DeploymentInfo 进行重定向。
    • 如果您想要完整的实现,请参考以下stackoverflow.com/a/54908727/7538821
    猜你喜欢
    • 2016-10-13
    • 2015-08-19
    • 2019-02-28
    • 2014-10-26
    • 2017-11-13
    • 1970-01-01
    • 1970-01-01
    • 2019-07-12
    • 2012-06-29
    相关资源
    最近更新 更多