Redirect HTTP to HTTPS in Undertow

半城伤御伤魂 提交于 2019-12-19 11:02:13

问题


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.

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}

Thanks!


回答1:


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


来源:https://stackoverflow.com/questions/27923989/redirect-http-to-https-in-undertow

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!