在 Undertow 中将 HTTP 重定向到 HTTPS [英] Redirect HTTP to HTTPS in Undertow

查看:35
本文介绍了在 Undertow 中将 HTTP 重定向到 HTTPS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 Undertow 中配置 HTTP->HTTPS 重定向?我查看了 Undertow 的代码库,有些类似乎是相关的(例如 RedirectHandler).此外,Undertow 文档(谓词属性和处理程序)似乎正是针对此问题等等.但我不知道从哪里开始.

How can one configure HTTP->HTTPS redirection in Undertow? I've looked through Undertow's codebase, there are some classes that seem to be relevant (e.g. RedirectHandler). Also, Undertow documentation (Predicates Attributes and Handlers) seems to target exactly this problem among others. But I'm not sure where to start.

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

Basically, what I'm looking for is some counterpart to Apache's mod_rewrite configuration:

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

谢谢!

推荐答案

这个答案 指出了正确的方向.以编程方式,必须将 SecurityConstraint 添加到 Builder 并设置 ConfidentialPortManager:

This answer pointed in the right direction. Programmatically, one has to add a SecurityConstraint to Builder and set 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;
    }
  }
);

这篇关于在 Undertow 中将 HTTP 重定向到 HTTPS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆