Amazon web services AWS ELB后面嵌入Tomcat的Spring引导-HTTPS重定向

Amazon web services AWS ELB后面嵌入Tomcat的Spring引导-HTTPS重定向,amazon-web-services,amazon-ec2,spring-security,spring-boot,amazon-elb,Amazon Web Services,Amazon Ec2,Spring Security,Spring Boot,Amazon Elb,在EC2实例上运行Spring引导应用程序端口8080 AWS ELB配置为重定向 80 -> 8080 443 (SSL termination happens here) -> 8080 应用程序使用Spring安全性,若用户到达,它将重定向到。我想登录页面使用SSL Spring安全代码段: http.requiresChannel().antMatchers("/login", "/logout").requiresSecure(); 我们正在运行重

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

AWS ELB配置为重定向

     80 -> 8080
     443 (SSL termination happens here) -> 8080
应用程序使用Spring安全性,若用户到达,它将重定向到。我想登录页面使用SSL

Spring安全代码段:

 http.requiresChannel().antMatchers("/login", "/logout").requiresSecure();
我们正在运行重定向循环,这是有意义的

对于Spring引导应用程序,看起来所有请求都是向非安全端口8080发出的,它重定向到,经过ELB,然后再次在8080上获得请求

关于如何使用AWS ELB运行此功能,您有什么想法吗?

您必须将此容器的连接器上的
secure
属性设置为
true
。在那之后,你的蚂蚁匹配规则将起作用


或者,您可以在Github上查看可用的,添加用于读取名为
server.channel.secure
的布尔配置属性的代码,将其设置在嵌入式Tomcat连接器上,并提交一个pull请求,供Spring团队合并到下一个版本中。

看起来这样做了:

@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);  
        }
    });

}

}

感谢您指出安全的财产。。看起来有一个相当简单的方法来设置它。我把答案贴在上面。