【问题标题】:Spring Boot with Embedded Undertow behind AWS ELB - HTTP to HTTPS redirect在 AWS ELB 后面带有嵌入式 Undertow 的 Spring Boot - HTTP 到 HTTPS 重定向
【发布时间】:2017-08-02 03:28:17
【问题描述】:

我正在 AWS EC2 实例的 8080 端口上运行 Spring Boot (Jhipster/Undertow) 应用程序。

我有一个配置为重定向的 AWS ELB

 80 -> 8080
 443 (SSL termination happens here) -> 8080

应用程序使用 Spring Security,如果您的用户到达 http://example.com,我希望它重定向到 https://example.com,以使用 SSL。

我在Tomcat 中找到了各种配置示例,但没有使用 Undertow。

我已经尝试过,使用第二个端口 8089,它会根据需要重定向,但这会导致端口 8080 也重定向我不想要的。

80 -> 8089
443 (SSL termination happens here) -> 8080
@Bean
public EmbeddedServletContainerFactory undertow() {

    UndertowEmbeddedServletContainerFactory undertow = new UndertowEmbeddedServletContainerFactory();
    undertow.addBuilderCustomizers(builder -> builder.addHttpListener(8089, "0.0.0.0"));
    undertow.addDeploymentInfoCustomizers(deploymentInfo -> {
        deploymentInfo.addSecurityConstraint(new SecurityConstraint()
                .addWebResourceCollection(new WebResourceCollection()
                        .addUrlPattern("/*"))
                .setTransportGuaranteeType(TransportGuaranteeType.CONFIDENTIAL)
                .setEmptyRoleSemantic(SecurityInfo.EmptyRoleSemantic.PERMIT))
                .setConfidentialPortManager(exchange -> 443);
    });
    return undertow;
}

如何配置 Undertow 来实现这一点?

【问题讨论】:

标签: spring-boot jhipster undertow


【解决方案1】:

当我遇到同样的问题时,这对我有用:

从 jhipster 暴露 80 端口(可以在application-prod.yml 中更改)。

Amazon ELB 在从 http 重定向到 https 时会添加一些标头,您应该在同一个文件中处理这些标头:

server: use-forward-headers: true port: 80

另外,您需要从 jhipster 强制执行 https: https://jhipster.github.io/tips/007_tips_enforce_https.html

【讨论】:

    【解决方案2】:

    以防万一有人想要在 Spring Boot 1.5.19 中使用 HTTP/2 将所有 http 请求重定向到 https 的工作解决方案,以下是application.properties文件中的设置:

    server.ssl.protocol=TLSv1.2
    server.ssl.key-store-type=PKCS12
    server.ssl.key-store=keystore.p12
    server.ssl.key-store-password=xxxxxxx
    server.port=443
    server.use-forward-headers=true
    

    以及以下Java配置:

    import io.undertow.UndertowOptions;
    import io.undertow.servlet.api.SecurityConstraint;
    import io.undertow.servlet.api.SecurityInfo;
    import io.undertow.servlet.api.TransportGuaranteeType;
    import io.undertow.servlet.api.WebResourceCollection;
    import org.springframework.boot.context.embedded.undertow.UndertowBuilderCustomizer;
    import org.springframework.boot.context.embedded.undertow.UndertowEmbeddedServletContainerFactory;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.PropertySource;
    
    
    @Configuration
    public class ConnectorConfig {
    
        @Bean
        public UndertowEmbeddedServletContainerFactory embeddedServletContainerFactory() {
    
            UndertowEmbeddedServletContainerFactory factory = new UndertowEmbeddedServletContainerFactory();
    
            factory.addBuilderCustomizers((UndertowBuilderCustomizer) builder -> {
                builder.setServerOption(UndertowOptions.ENABLE_HTTP2, true);
                builder.addHttpListener(80, "0.0.0.0");
            });
    
            factory.addDeploymentInfoCustomizers(deploymentInfo -> {
                deploymentInfo.addSecurityConstraint(
                        new SecurityConstraint()
                                .addWebResourceCollection(new WebResourceCollection().addUrlPattern("/*"))
                                .setTransportGuaranteeType(TransportGuaranteeType.CONFIDENTIAL)
                                .setEmptyRoleSemantic(SecurityInfo.EmptyRoleSemantic.PERMIT))
                        .setConfidentialPortManager(exchange -> 443);
            });
    
            return factory;
        }
    }
    

    一切都会完美运行。

    【讨论】:

      猜你喜欢
      • 2015-07-25
      • 2015-08-19
      • 2019-04-21
      • 2014-12-13
      • 2018-09-26
      • 2018-01-12
      • 2017-12-25
      • 2018-04-15
      • 1970-01-01
      相关资源
      最近更新 更多