我正在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 .
我已经找到了在_857959中配置它的各种示例,但没有使用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来实现这一目标?
1 回答
当我遇到同样的问题时,这对我有用:
从jhipster公开端口80(您可以在
application-prod.yml
中更改它) .当从http重定向到https时,Amazon ELB会添加一些 Headers ,您应该在同一个文件中进行寻址:
server: use-forward-headers: true port: 80
此外,您需要强制执行来自jhipster的https:https://jhipster.github.io/tips/007_tips_enforce_https.html