4

I'm trying to redirect http to https through Catalina Connector in my Spring Boot application. If the incoming POST request is "https" then it is working as expected. But if my incoming POST request is "http" and after the redirection to "https" through below code, somewhere it is getting changed to GET because of which I'm getting -

WARN 45028 --- [nio-8443-exec-8] o.s.web.servlet.PageNotFound : Request method 'GET' not supported

Below are the methods in my @SpringBootApplication class:

@Bean
public EmbeddedServletContainerFactory servletContainer() {
    TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory() {
        @Override
        protected void postProcessContext(org.apache.catalina.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(10024);
    connector.setSecure(false);
    connector.setRedirectPort(8443);

    return connector;
 }
Saeid
  • 3,911
  • 7
  • 25
  • 41

1 Answers1

1

Add collection.addMethod(DEFAULT_PROTOCOL); this line in postProcessContext() override method from code. It working properly with all HTTP request methods like POST,PUT,DELETE,GET etc..

Rohit Poudel
  • 1,604
  • 2
  • 16
  • 19