Spring-boot: redirect http to https in Spring boot 2.0.0 M2

Created on 22 Jul 2017  路  8Comments  路  Source: spring-projects/spring-boot

In the spring boot 1.5.3 I use the the following bean to redirect from http to https :

@Bean
    public EmbeddedServletContainerFactory servletContainer() {
        TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory() {
            @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(initiateHttpConnector());
        return tomcat;
    }

private Connector initiateHttpConnector() {
        Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
        connector.setScheme("http");
        connector.setPort(8000);
        connector.setSecure(false);
        connector.setRedirectPort(8448);
        return connector;
}

But when migrated to version 2.0.0 M2 the EmbeddedServletContainerFactory and TomcatEmbeddedServletContainerFactory are not availables anymore !!

stackoverflow invalid

Most helpful comment

For reference, this should be the equivalent in Spring Boot 2.0

    @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;
    }

All 8 comments

Thanks for getting in touch, but it feels like this is a question that would be better suited to Stack Overflow. As mentioned in the guidelines for contributing, we prefer to use GitHub issues only for bugs and enhancements. Feel free to update this issue with a link to the re-posted question (so that other people can find it) or add some more details if you feel this is a genuine bug.

You may also like to read the relevant section of the release notes

For reference, this should be the equivalent in Spring Boot 2.0

    @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;
    }

Thanks alot! @GitCash send .005 bch to @rubensa

Hey rubensa, user tbrannt tipped you 5000 bits in Bitcoin Cash ( ~ $3.515 ).

Click here to claim it!

You can also add the :-1: reaction to tbrannt's comment above to prevent future tips.

So, this solution assumes that there are two ports. But what if you only got one? My entire question can be found on stackoverflow: https://stackoverflow.com/questions/53969201/how-to-deploy-application-with-two-connectors-on-different-ports-http-https

In essence: I deploy towards Heroku, which only provides one port per dyno. I am also using Braintree which communicates via HTTP POST (not HTTPS) with my server. And that's already the problem. I don't understand how I would handle both protocols given only one port.

Is there anything I could do within Spring Boot 2 in order to get around this problem?

This issue is closed and we鈥檝e already indicated that we prefer not to use the issue tracker for questions, so please do not add one. Your SO question is the right place.

@snicoll I added my part because of

"Feel free to update this issue with a link to the re-posted question (so that other people can find it)"

Just trying to lay down bread crumbs for people with similar issues.

That was addressed to the reporter, in the context of their specific question.

Was this page helpful?
0 / 5 - 0 ratings