【问题标题】:How to redirect automatically to https with Spring Boot如何使用 Spring Boot 自动重定向到 https
【发布时间】:2014-05-02 18:31:13
【问题描述】:

如何轻松配置嵌入式 tomcat 服务器以将所有 http 流量重定向到 https?我在弹性负载均衡器后面的 ec2 实例上运行 Spring Boot。我已将 ELB 配置为为我处理 ssl(这很棒),并将 X-FORWARDED-PROTO 标头设置为“https”。我想检测何时未设置,并重定向用户以强制他们使用 https(如果尚未设置)。

到目前为止,我尝试将以下内容添加到我的 application.properties 文件中,但没有成功:

server.tomcat.protocol-header=x-forwarded-proto
security.require-ssl=true

【问题讨论】:

  • 您是否有一个安全的应用程序(Spring Security 在类路径上,它是否拦截了您的请求)?请注意,即使那样重定向也不是自动的(Spring Security 与 HSTS 一起工作,这需要客户端理解并遵循标头中的指令)。
  • 嗨。你解决了吗?我们面临着类似的问题。我们有一个 ec2 负载均衡器,它终止 ssl 并使用 http 返回到 Spring 引导应用程序。我们可以从负载均衡器中删除 https 侦听器,但我们宁愿重定向到 https。

标签: spring amazon-ec2 spring-security spring-boot


【解决方案1】:

您将需要一个密钥库文件和几个配置类。

下面的链接详细解释了它。

Https on embedded tomcat

【讨论】:

  • 在这种情况下,使用 AWS 负载均衡器并在 AWS 负载均衡器上处理 SSL。因此,不需要密钥库文件。
【解决方案2】:

我的回答有点晚了,但我最近遇到了这个问题,想发布一个对我有用的解决方案。

最初,我认为将 tomcat 设置为使用 X-Forwarded 标头就足够了,但是通常应该处理这种情况的 Tomcat 的 RemoteIPValve 对我不起作用。

我的解决方案是添加一个 EmbeddedServletContainerCustomizer 并添加一个 ConnectorCustomizer: (注意我这里使用的是Tomcat 8)

@Component
public class TomcatContainerCustomizer implements EmbeddedServletContainerCustomizer {

    private static final Logger LOGGER = LoggerFactory.getLogger(TomcatContainerCustomizer.class);

    @Override
    public void customize(final ConfigurableEmbeddedServletContainer container) {
        if (container instanceof TomcatEmbeddedServletContainerFactory) {
            final TomcatEmbeddedServletContainerFactory tomcat = (TomcatEmbeddedServletContainerFactory) container;
            tomcat.addConnectorCustomizers(connector -> { 
                connector.setScheme("https");
                connector.setProxyPort(443);
            });
            LOGGER.info("Enabled secure scheme (https).");
        } else {
            LOGGER.warn("Could not change protocol scheme because Tomcat is not used as servlet container.");
        }
    }
}

重要的是,您不仅将 Scheme 设置为 https,而且还设置了 ProxyPort,没有它,来自 Spring Boot 的所有内部重定向都被路由到端口 80。

【讨论】:

    【解决方案3】:

    配置属性 security.require-ssl 在禁用基本身份验证时不起作用(至少在旧版本的 Spring Boot 上)。因此,您可能需要使用与此类似的代码手动保护所有请求:

    @Configuration
    public class SecurityConfig extends WebSecurityConfigurerAdapter {
    
        @Inject private SecurityProperties securityProperties;
    
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            if (securityProperties.isRequireSsl()) http.requiresChannel().anyRequest().requiresSecure();
        }
    }
    

    你可以在这里查看我的完整答案:Spring Boot redirect HTTP to HTTPS

    【讨论】:

      【解决方案4】:

      Spring Boot 2.0 重定向 http 到 https:

      将以下内容添加到@Configuration

         @Bean
          public ServletWebServerFactory servletContainer() {
              TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
                  @Override
                  protected void postProcessContext(Context context) {
                      SecurityConstraint securityConstraint = new SecurityConstraint();
                      securityConstraint.setUserConstraint("CONFIDENTIAL");
                      SecurityCollection collection = new SecurityCollection();
                      collection.addPattern("/*");
                      securityConstraint.addCollection(collection);
                      context.addConstraint(securityConstraint);
                  }
              };
              tomcat.addAdditionalTomcatConnectors(redirectConnector());
              return tomcat;
          }
      
          private Connector redirectConnector() {
              Connector connector = new Connector(
                      TomcatServletWebServerFactory.DEFAULT_PROTOCOL);
              connector.setScheme("http");
              connector.setPort(8080);
              connector.setSecure(false);
              connector.setRedirectPort(8443);
              return connector;
          }
      

      【讨论】:

        猜你喜欢
        • 2019-05-15
        • 2016-11-06
        • 2021-03-31
        • 1970-01-01
        • 2014-12-26
        • 2020-10-07
        • 2015-11-20
        • 2017-02-04
        • 2021-12-12
        相关资源
        最近更新 更多