Redirect Post method HTTP -> HTTPS - HTTP Status 4

2019-02-21 00:09发布

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-spring-boot/

HTTPS Post method works, but http doesn't. I test it via SoapUI and it throws HTTP status 405 - Method not allowed.

This is how request/response method looks in code:

private static final String NAMESPACE_URI = "http://....";

@PayloadRoot(namespace = NAMESPACE_URI, localPart = "getRequest")
@ResponsePayload
public GetResponse getRequest(@RequestPayload GetRequest request){...}

Any code snippet I should attach?

Thank you for help.

2条回答
Explosion°爆炸
2楼-- · 2019-02-21 00:31

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.

查看更多
何必那么认真
3楼-- · 2019-02-21 00:33

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
查看更多
登录 后发表回答