Redirect Post method HTTP -> HTTPS - HTTP Status 405 (Spring boot)

前端 未结 2 495
抹茶落季
抹茶落季 2021-01-17 01:27

Hi I am trying to have both http & https protocols available in Spring Boot web service. I was following this tutorial :https://drissamri.be/blog/java/enable-https-in-sp

相关标签:
2条回答
  • 2021-01-17 01:48

    I had the same error 405 for POST requests. I found a way to fix it.

    To enable both https and http with GET and POST I had to add the following to a @Configuration class instead of the TomcatEmbeddedServletContainerFactory as described in the post:

    @Bean
    public EmbeddedServletContainerCustomizer containerCustomizer() {
        return container -> {
            if (container instanceof TomcatEmbeddedServletContainerFactory) {
                TomcatEmbeddedServletContainerFactory containerFactory = (TomcatEmbeddedServletContainerFactory) container;
                Connector connector = new Connector(TomcatEmbeddedServletContainerFactory.DEFAULT_PROTOCOL);
                connector.setPort(httpPort);
                containerFactory.addAdditionalTomcatConnectors(connector);
            }
        };
    }
    

    Hope this helps for people will have the same problem in future.

    0 讨论(0)
  • 2021-01-17 02:07

    After fiddling with with multiple tutorials, answer is to follow official guide...

    http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#howto-enable-multiple-connectors-in-tomcat

    UPDATE:

    import org.apache.catalina.connector.Connector;
    import org.apache.coyote.http11.Http11NioProtocol;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
    import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    import java.io.File;
    
    @Configuration
    public class HttpsConnector {
    
        @Value("${cert.keyStore}")
        private String keyStorePath;
    
        @Value("${cert.trustStore}")
        private String trustStorePath;
    
        @Value("${cert.keyStorePass}")
        private String keyStorePass;
    
        @Value("${cert.trustStorePass}")
        private String trustStorePass;
    
        @Value("${cert.keyStoreType}")
        private String keyStoreType;
    
        @Value("${cert.trustStoreType}")
        private String trustStoreType;
    
        @Value("${cert.keyAlias}")
        private String keyAlias;
    
        @Value("${cert.httpsPort}")
        private int httpsPort;
    
        @Bean
        public EmbeddedServletContainerFactory servletContainer() {
            TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory();
            tomcat.addAdditionalTomcatConnectors(createSslConnector());
            return tomcat;
        }
    
        private Connector createSslConnector() {
            Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
            Http11NioProtocol protocol = (Http11NioProtocol) connector.getProtocolHandler();
    
            String absKeyStorePath = getAbsolutePath(keyStorePath);
            String absTrustStorePath = getAbsolutePath(trustStorePath);
    
            connector.setScheme("https");
            connector.setSecure(true);
            connector.setPort(httpsPort);
            protocol.setSSLEnabled(true);
            protocol.setKeystoreType(keyStoreType);
            protocol.setKeystoreFile(absKeyStorePath);
            protocol.setKeystorePass(keyStorePass);
            protocol.setTruststoreType(trustStoreType);
            protocol.setTruststoreFile(absTrustStorePath);
            protocol.setTruststorePass(trustStorePass);
            protocol.setKeyAlias(keyAlias);
            return connector;
        }
    
        private String getAbsolutePath(String path) {
            File file = new File(path);
            if (!file.isAbsolute()) {
                path = file.getAbsolutePath();
            }
            return path;
        }
    }
    

    configuration:

    #SERVER CONFIG
    server.port=8090    
    
    #Certificate
    cert.keyStore=src/main/resources/keystore.p12
    cert.trustStore=src/main/resources/keystore.p12
    cert.keyStorePass=...
    cert.trustStorePass=...
    cert.keyStoreType=pkcs12
    cert.trustStoreType=pkcs12
    cert.keyAlias=...
    cert.httpsPort=8443
    
    0 讨论(0)
提交回复
热议问题