在Spring Boot 2.0.4中从http重定向到https

时间:2018-12-24 08:55:26

标签: java spring spring-boot https

我使用Spring boot 2.0.4。我想为所有网址模式配置从http到https的自动重定向。我在我的application.yml中添加以下行:

server:
  ssl:
    enabled: true
    key-alias: tomcat
    key-store: "classpath:tomcat.keystore"
    key-store-type: jks
    key-store-password: 123456
    key-password: 123456

然后我创建了bean:

    @Bean
    public TomcatServletWebServerFactory httpsRedirectConfig() {
        return 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);
            }
        };
    }

但是,当我运行我的应用程序时,出现错误:

Caused by: org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to multiple ServletWebServerFactory beans : tomcatServletWebServerFactory,httpsRedirectConfig

怎么了?我该如何解决?谢谢。

1 个答案:

答案 0 :(得分:2)

尝试以下配置。

weighted_masked_objective