带有嵌入式Undertow的Spring Boot在AWS ELB背后 - HTTP到HTTPS重定向

问题描述 投票:2回答:2

我正在AWS EC2实例上的端口8080上运行Spring启动(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
2个回答
1
投票

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

从jhipster公开端口80(您可以在application-prod.yml中更改它)。

当从http重定向到https时,Amazon ELB会添加一些标题,您应该在同一个文件中寻址:

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

此外,您需要强制执行来自jhipster的https:https://jhipster.github.io/tips/007_tips_enforce_https.html


0
投票

如果有人想在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;
    }
}

一切都会完美运作。

© www.soinside.com 2019 - 2024. All rights reserved.