小编典典

重定向发布方法HTTP-> HTTPS-HTTP状态405(Spring Boot)

spring-boot

嗨,我正在尝试在Spring Boot
Web服务中同时使用http和https协议。我正在关注本教程:https : //drissamri.be/blog/java/enable-https-in-spring-
boot/

HTTPS Post方法有效,但http无效。我通过SoapUI对其进行测试,并抛出异常HTTP status 405 - Method not allowed

这是请求/响应方法在代码中的外观:

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

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

我应该附上任何代码段吗?

谢谢你的帮助。


阅读 777

收藏
2020-05-30

共1个答案

小编典典

在摆弄了多个教程之后,答案是遵循官方指南…

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

更新:

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

组态:

#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
2020-05-30