[英]How to run Spring Boot HTTPS server on multiple HTTPS ports
我需要使用 HTTPS 在 2 個或更多端口上運行相同的服務器。 在早期的生產中,我們為服務配置了10500端口。 目前,我們需要在啟用 SSL 的 443 和 10500 上運行它。
我找到了許多用於為 Spring Boot 啟用 HTTP 和 HTTPS 的資源。 但是我找不到任何允許在 2 個或更多端口上啟用 HTTPS 服務的方法。
我試過配置端口重定向。 但這也不起作用。 它在使用 HTTP 手動連接到端口 443 時起作用。 但是每當使用 HTTPS 時,程序都會拋出異常。
java.lang.IllegalArgumentException異常:無效字符在方法名實測值[0x160x030x010x020x000x010x000x010xfc0x030x030x810x00aC0x1b0x10`0xb80x8d0xae0x9e0xe40xc7V0xf60x08:e0xcc0x8f <0xf70x8b0xc2y0xa40xfe0xa3(0xc7-0xe6]。HTTP方法名稱必須是令牌
@Bean
public ServletWebServerFactory servletContainer() {
TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
@Override
protected void postProcessContext(Context context) {
SecurityConstraint securityConstraint = new SecurityConstraint();
securityConstraint.setUserConstraint("CONFIDENTIAL");
SecurityCollection collection = new SecurityCollection();
collection.addPattern("/*");
securityConstraint.addCollection(collection);
context.addConstraint(securityConstraint);
}
};
tomcat.addAdditionalTomcatConnectors(oldPortRedirectConnector());
return tomcat;
}
private Connector oldPortRedirectConnector() {
Connector connector = new Connector(TomcatServletWebServerFactory.DEFAULT_PROTOCOL);
connector.setScheme("https");
connector.setPort(443);
connector.setSecure(true);
connector.setRedirectPort(10500);
return connector;
}
應用程序屬性
server.port=10500
任何幫助是極大的贊賞。 在 2 個端口上運行相同的服務或從一個 HTTPS 端口轉發到另一個對我們來說真的很棒。
最后,我讓它工作了。 問題是,即使我使用 HTTPS 方案添加了額外的連接器,也沒有為該額外的連接器設置 SSL 配置。
通過設置 SSLHostConfig,我們可以根據需要擁有盡可能多的額外 https 端口。
@Bean
public ServletWebServerFactory servletContainer() {
TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
@Override
protected void postProcessContext(Context context) {
SecurityConstraint securityConstraint = new SecurityConstraint();
securityConstraint.setUserConstraint("CONFIDENTIAL");
SecurityCollection collection = new SecurityCollection();
collection.addPattern("/*");
securityConstraint.addCollection(collection);
context.addConstraint(securityConstraint);
}
};
tomcat.addAdditionalTomcatConnectors(extraHttpsConnector());
return tomcat;
}
private Connector extraHttpsConnector() {
Connector connector = new Connector();
connector.setScheme("https");
connector.setPort(443);
connector.setSecure(true);
connector.setProperty("SSLEnabled", "true");
//Add SSL configuration to your extra connector
SSLHostConfig sslConfig = new SSLHostConfig();
SSLHostConfigCertificate certConfig = new SSLHostConfigCertificate(sslConfig, Type.RSA);
certConfig.setCertificateKeystoreFile("YOUR_KEYSTORE");
certConfig.setCertificateKeystorePassword("YOUR_KEYSTORE_PASSWORD");
certConfig.setCertificateKeyAlias("YOUR_KEYSTORE_ALIAS");
sslConfig.addCertificate(certConfig);
//Link the configuration to the connector
connector.addSslHostConfig(sslConfig);
return connector;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.