如何使用Spring Boot自动重定向到https

问题描述 投票:12回答:4

如何轻松配置嵌入式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 amazon-ec2 spring-security spring-boot
4个回答
9
投票

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

最初,我认为将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设置为ProxyPort,否则Spring Boot的所有内部重定向都将路由到端口80。

希望有所帮助:-)


2
投票

禁用基本身份验证时配置属性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


0
投票

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

以下链接详细解释了它。

Https on embedded tomcat


0
投票

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;
    }
© www.soinside.com 2019 - 2024. All rights reserved.