您好我正在尝试同时拥有http和& Spring Boot Web服务中提供的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){...}
我应该附加任何代码段吗?
感谢您的帮助。
答案 0 :(得分:-1)
我对POST请求有同样的错误405。我找到了解决问题的方法。
要使用GET和POST启用https和http,我必须将以下内容添加到@Configuration类而不是TomcatEmbeddedServletContainerFactory中,如帖子中所述:
@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);
}
};
}
希望这有助于人们将来遇到同样的问题。
答案 1 :(得分:-4)
在摆弄多个教程后,回答是遵循官方指南......
更新:
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