带有 AWS ELB 后面的嵌入式 Tomcat 的 Spring Boot - HTTPS 重定向 [英] Spring Boot with Embedded Tomcat behind AWS ELB - HTTPS redirect

查看:43
本文介绍了带有 AWS ELB 后面的嵌入式 Tomcat 的 Spring Boot - HTTPS 重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 EC2 实例上运行 Spring Boot 应用程序端口 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.

Spring 安全代码段:

Spring security snippet:

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

}

}

这篇关于带有 AWS ELB 后面的嵌入式 Tomcat 的 Spring Boot - HTTPS 重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆