问题
以编程方式配置Spring Boot以使用我的自定义SSLContext
。并用于mTLS。
上下文
Spring的文档只提供了一种明确的方法来配置SSL(通过application.properties
):
server.port=8443
server.ssl.key-store=classpath:keystore.jks
server.ssl.key-store-password=secret
server.ssl.trust-store=classpath:truststore.jks
...
然而,该解决方案缺乏深度,因为在某些情况下,我希望利用自定义自己的SSLContext
。例如,将mTLS配置为不仅信任通过keytool
生成的单个证书,而且信任我的自签名证书和放置在Java默认TrustStore(lib/security/cacerts
)中的证书。
当然,我可以使用已经提到的keytool
来组合它们,但我正在寻找更灵活的方法,因此提供了我自己的SSLContext
。
Spring提供了一个关于配置Web服务器的部分,其中说使用TomcatServletWebServerFactory
或ConfigurableServletWebServerFactory
之类的东西,但它们并没有真正深入。
我已经尝试创建一个Component
:
@Component
public class CustomServletCustomizer implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {
private static final String SERVER_CERTIFICATE_PATH = "identity.jks";
private static final char[] PASSWORD = "secret".toCharArray();
private static final String CLIENT_CERTIFICATE_PATH = "certs/client.cer";
@Override
public void customize(ConfigurableServletWebServerFactory factory) {
factory.setSslStoreProvider(new SslStoreProvider() {
@Override
public KeyStore getKeyStore() throws Exception {
var certificateAsInputStream = this.getClass().getClassLoader().getResourceAsStream(SERVER_CERTIFICATE_PATH);
var keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
keyStore.load(certificateAsInputStream, PASSWORD);
return keyStore;
}
@Override
public KeyStore getTrustStore() throws Exception {
var keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
keyStore.load(null);
var organisationRootCertBytes = this.getClass().getClassLoader().getResourceAsStream(CLIENT_CERTIFICATE_PATH).readAllBytes();
var certificateFactory = CertificateFactory.getInstance("X.509");
var certificate = certificateFactory.generateCertificate(new ByteArrayInputStream(organisationRootCertBytes));
keyStore.setCertificateEntry("server", certificate);
return keyStore;
}
});
}
}
但无济于事。
不幸的是,这在Spring Boot+Tomcat中是不可能的。它没有注入SSLContext或SSLServerSocketFactory、TrustManager或KeyManager等其他属性的选项。
然而,如果你仍然想使用Spring Boot并想对其进行全面配置,如果你不在乎Spring Boot使用的是什么类型的服务器,我推荐Jetty。
以下是如何使用SpringBoot+Jetty:实现这一点的基本实现
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.embedded.jetty.JettyServerCustomizer;
import org.springframework.boot.web.embedded.jetty.JettyServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import java.util.Collections;
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
@Bean
public SslContextFactory.Server sslContextFactory() {
SslContextFactory.Server sslContextFactory = new SslContextFactory.Server();
sslContextFactory.setSslContext(sslContext);
sslContextFactory.setIncludeProtocols(protocols);
sslContextFactory.setIncludeCipherSuites(ciphers);
sslContextFactory.setNeedClientAuth(true);
return sslContextFactory;
}
@Bean
public ConfigurableServletWebServerFactory webServerFactory(SslContextFactory.Server sslContextFactory) {
JettyServletWebServerFactory factory = new JettyServletWebServerFactory();
factory.setPort(8443);
JettyServerCustomizer jettyServerCustomizer = server -> server.setConnectors(new Connector[]{new ServerConnector(server, sslContextFactory)});
factory.setServerCustomizers(Collections.singletonList(jettyServerCustomizer));
return factory;
}
}
您还需要告诉spring不要再使用tomcat,并切换到jetty。您可以通过在pom中添加以下代码段来做到这一点:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>