在AWS ELB之后使用嵌入式Tomcat进行Spring Boot-HTTPS重定向

问题描述:

在EC2实例上运行Spring引导应用程序端口8080。

Running Spring boot application port 8080 on EC2 instance.

配置为重定向的AWS ELB

AWS ELB configured to redirect

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

应用程序使用Spring Security,并且如果用户到达 http://example.com 它将重定向到。我想登录页面以使用SSL。

Application uses Spring Security and if you user arrives to http://example.com it will redirect to . I would like to login page to use SSL.

春季安全性片段:

 http.requiresChannel().antMatchers("/login", "/logout").requiresSecure();

我们正在进入重定向循环,这很有意义。

We are running into redirect loop which makes sense.

对于Spring Boot应用程序,看起来所有请求都是向非安全端口8080发出的,它重定向到 https://example.com ,通过ELB并再次收到有关8080的请求

To Spring Boot application it looks like all requests are made to non-secured port 8080, it redirects to https://example.com, goes through ELB and again gets request on 8080

有关如何在AWS上运行它的任何想法ELB ???

Any ideas on how to run this with AWS ELB ???

看起来像这样的把戏:

@Component
public class TomcatCustomizer implements EmbeddedServletContainerCustomizer {

@Override
public void customize(ConfigurableEmbeddedServletContainer container) {
    TomcatEmbeddedServletContainerFactory tomcat = (TomcatEmbeddedServletContainerFactory) container;
    tomcat.addConnectorCustomizers(new TomcatConnectorCustomizer() {
        @Override
        public void customize(Connector connector) {
            connector.setSecure(true);  
        }
    });

}

}