2017-03-11 41 views
1

我在AWS EC2實例上的端口8080上運行Spring引導(Jhipster/Undertow)應用程序。帶有嵌入式Undertow的AWS Boot ELB - HTTPS重定向

我有一個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來實現此目的?

+0

https://twitter.com/ankinson/status/829256167700492288可能涉及類似的問題,你想解決? –

回答

0

這爲我工作,當我有同樣的問題:

揭露從jhipster端口80(你可以在application-prod.yml其更改)。

亞馬遜ELB從HTTP重定向到https的時候增加了一些標題,你應該在同一個文件地址:

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

此外,您還需要從jhipster執行的https: https://jhipster.github.io/tips/007_tips_enforce_https.html