Spring Boot和ELB - 如何使负载均衡器将http重定向到https?

3
我已经通过Elastic Beanstalk部署了一个Spring Boot应用程序。我正在使用负载均衡器,因此这是流程(就我所知):
互联网/浏览器请求 ---HTTPS---> 负载均衡器 ---HTTP---> Spring Boot应用服务器
因此,基本上SSL在负载均衡器处终止,应用程序服务器只处理普通的HTTP。
但是,在浏览器发出HTTP请求的情况下,我希望负载均衡器自动重定向到HTTPS。
有几个关于此问题的问题: Spring Boot with Embedded Tomcat behind AWS ELB - HTTPS redirect
How to redirect automatically to https with Spring Boot
Spring Boot redirect HTTP to HTTPS 但是这些问题的答案都让我感到困惑。也许是我误解了,但所有的答案基本上都使Spring Boot应用程序仅服务于HTTPS请求(例如,在使用http.requiresChannel().anyRequest().requiresSecure()时)。然而,这与流程相矛盾,因为我非常满意SSL在负载均衡器上终止,而Spring Boot应用程序服务器只处理HTTP。因此,如果我在Spring Boot级别要求SSL,那么我将需要进行端到端的SSL连接,这对我的应用程序并不真正需要。我还使用了以下属性,似乎也没有帮助:
server.tomcat.remote-ip-header=x-forwarded-for
server.tomcat.protocol-header=x-forwarded-proto  

相关链接:https://dev59.com/pK_la4cB1Zd3GeqPycop - Chloe
2个回答

2
通过这篇文章的帮助,我终于能够弄清楚如何在ELB环境中针对Spring Boot应用程序执行此操作。
我必须在src/main/webapp/.ebextensions/nginx/conf.d中创建一个配置文件。我只是称其为myconf.conf。
myconf.conf中,我放置了以下代码:
server {
    listen  80;
    server_name www.my-site.com;
    if ($http_x_forwarded_proto != "https") {
        rewrite ^(.*)$ https://$server_name$REQUEST_URI permanent;
    }
}

此外,请确保负载均衡器的HTTP和HTTPS侦听器都处于打开状态。
另外,我的Spring Boot应用程序只开放了HTTP,因为负载均衡器已经终止了SSL。

-1

AWS负载均衡器无法处理重定向。您可以通过服务器或使用CloudFront分发来完成。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接