在Undertow中将HTTP重定向到HTTPS

时间:2015-01-13 14:05:58

标签: https rewrite undertow

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

基本上,我正在寻找的是与Apache的mod_rewrite配置相对应的东西:

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

谢谢!

1 个答案:

答案 0 :(得分:3)

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