【问题标题】:Spring Boot & ELB - How do I make the load balancer redirect http to https?Spring Boot & ELB - 如何让负载均衡器将 http 重定向到 https?
【发布时间】:2018-06-07 12:11:40
【问题描述】:

我已经通过 Elastic Beanstalk 部署了一个 Spring Boot 应用程序。我正在使用负载均衡器,所以这是流程(据我所知):

Internet/浏览器请求---HTTPS--->负载均衡器---HTTP---> Spring Boot App Server

因此,从本质上讲,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  

【问题讨论】:

标签: spring amazon-web-services spring-boot amazon-elastic-beanstalk


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    article 的帮助下,我终于能够弄清楚如何在 ELB 环境中为 Spring Boot 应用程序执行此操作。

    我必须在src/main/webapp/.ebextensions/nginx/conf.d 中创建一个conf 文件。我只是将其命名为 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。

    【讨论】:

      猜你喜欢
      • 2018-12-11
      • 1970-01-01
      • 1970-01-01
      • 2014-08-27
      • 2015-03-18
      • 2017-02-19
      • 2017-04-18
      • 2016-11-26
      • 2020-08-21
      相关资源
      最近更新 更多