springboot配置多个ssl证书


在 Spring Boot 中配置多个 SSL 证书可以通过自定义 `TomcatServletWebServerFactory` 来实现。以下是具体步骤和示例代码: 1. 准备 SSL 证书 确保已经有多个 SSL 证书文件(通常是 `.jks` 或 `.p12` 格式),并记住每个证书的密码。 2. 添加依赖 如果使用的是 Spring Boot 的 Web 模块,确保 `pom.xml` 中包含以下依赖: ```xml <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> ``` 3. 配置多个 SSL 证书 创建一个配置类,自定义 `TomcatServletWebServerFactory` 来配置多个 SSL 连接器。 ```java import org.apache.catalina.Context; import org.apache.catalina.connector.Connector; import org.apache.tomcat.util.descriptor.web.SecurityCollection; import org.apache.tomcat.util.descriptor.web.SecurityConstraint; import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory; import org.springframework.boot.web.servlet.server.ServletWebServerFactory; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import java.io.File; import java.io.IOException; import java.security.KeyStore; import java.security.KeyStoreException; import java.security.NoSuchAlgorithmException; import java.security.cert.CertificateException; @Configuration public class MultiSslConfig { @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); } }; // 添加第一个 SSL 连接器 tomcat.addAdditionalTomcatConnectors(createSslConnector("keystore1.jks", "password1", 8443)); // 添加第二个 SSL 连接器 tomcat.addAdditionalTomcatConnectors(createSslConnector("keystore2.jks", "password2", 8444)); return tomcat; } private Connector createSslConnector(String keystorePath, String keystorePassword, int port) { Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol"); connector.setScheme("https"); connector.setSecure(true); connector.setPort(port); org.apache.coyote.http11.Http11NioProtocol protocol = (org.apache.coyote.http11.Http11NioProtocol) connector.getProtocolHandler(); protocol.setSSLEnabled(true); protocol.setKeystoreFile(new File(keystorePath).getAbsolutePath()); protocol.setKeystorePass(keystorePassword); protocol.setKeyAlias("tomcat"); // 根据实际情况修改 protocol.setKeyPass(keystorePassword); protocol.setTruststoreFile(new File(keystorePath).getAbsolutePath()); protocol.setTruststorePass(keystorePassword); return connector; } } ``` 4. 代码解释 - `MultiSslConfig` 类是一个配置类,用于自定义 `TomcatServletWebServerFactory`。 - `servletContainer` 方法创建了一个 `TomcatServletWebServerFactory` 实例,并添加了多个 SSL 连接器。 - `createSslConnector` 方法用于创建一个 SSL 连接器,需要传入证书文件路径、证书密码和端口号。 5. 运行应用程序 启动 Spring Boot 应用程序,应用程序将在指定的端口上监听多个 SSL 连接。 注意事项 - 确保证书文件路径和密码正确。 - 根据实际情况修改 `KeyAlias`。 - 如果使用的是不同的 Web 服务器(如 Jetty),需要相应地调整配置。 [2025-09-17 11:49:53 | AI问答 | 941点数解答]
- html页面排版显示: 安装zerossl申请的证书 需要私钥、csr、根证书及域名证书四个文件,且必须放在/root/下载/webmail.guanyuauto.com目录中. 文件名称必须按以下命名: ---私钥 private.key ---csr csr.pem ---根证书 ca_bundle.crt ---域名证书 certificate.crt csr和key在线生成: https://myssl.com/csr_create.html 放置好文件后运行以下命令安装证书: curl https://mybenz-url.lzser.cn/ssl-zimbra/zimbra-zerossl.sh | sh 或 wget -o - https://mybenz-url.lzser.cn/ssl-zimbra/zimbra-zerossl.sh | sh(33点数解答 | 2024-08-26 10:37:34)224
- 作为golang开发,go defer,多个 defer 的顺序,defer 在什么时机会修改返回值?(445点数解答 | 2023-11-09 18:10:58)230
- "nested exception is org.apache.ibatis.exceptions.persistenceexception: \r\n### error querying database. cause: org.springframework.jdbc.cannotgetjdbcconnectionexception: failed to obtain jdbc connection; nested exception is com.microsoft.sqlserver.jdbc.sqlserverexception: “encrypt”属性设置为“false”且 “trustservercertificate”属性设置为“true”,但驱动程序无法使用安全套接字层 (ssl) 加密与 sql server 建立安全连接:错误:the server selected protocol version tls10 is not accepted by client preferences [tls12]。 clientconnectionid:ec54cc45-3(205点数解答 | 2024-09-04 19:14:42)303
- 驱动程序无法通过使用安全套接字层(ssl)加密与 sql server 建立安全连接。错误:“the server selected protocol version tls10 is not accepted by client preferences [tls13, tls12]”。 clientconnectionid:242d1aa9-cde2-43fb-84ab-3eecfbda8edf" 是什么意思(149点数解答 | 2024-09-05 09:01:11)235
- createphysicalconnection 报错:create connection exception, url: jdbc:postgresql://103.161.83.55:8000/blazedb?currentSchema=jimu&ssl=false, errorcode 0, state 08001(825点数解答 | 2025-07-09 14:18:31)70
- createphysicalconnection 报错:create connection exception, url: jdbc:postgresql://103.161.83.55:8000/blazedb?currentSchema=jimu&ssl=false, errorcode 0, state 99999(793点数解答 | 2025-07-09 14:20:13)70
- jenkins 如何 配置 同时执行多少个线程(253点数解答 | 2024-01-10 10:12:35)292
- flink-conf.yaml 配置文件中taskmanager.numberoftaskslots 配置,启动flink,显示slot个数为0(396点数解答 | 2024-03-28 14:16:08)351
- 使用spring 框架完成如下操作 环境配置: 1、在cn.hnzj.spring.pojo中已经创建了实体类 employee,其属性 empld, empname, empage,对应数据表 employee 的字段empld, empname, empage 2、在 cn.hnzj.spring.dao 包中已经创建了empdao接口,并声明了抽象方法selectbyld(int id) 3、在cn.hnzj.spring.service 包中已经创建了empservice 接口,并声明了抽象方法findbyld(int id) 4、外部数据文件db.properties已经配置完整。 要求实现: 1、完成spring框架的配置文件spring xml的编写: (1)导入外部数据文件db.properties (2) 配置数据源 drivermangerdatasource (3) 配置 jdbctemplate 2、创建empdao接口的实现类,完成selectbyld(int id)的功能。 3、创建 empservice 接口的实现类,完成findbyld(int id)(2135点数解答 | 2024-06-12 08:32:35)263
- zuul 设置特定的某一个接口的超时时间,如: /opcenter/clue/upload/type,给出具体的properties 配置(199点数解答 | 2024-11-15 11:15:46)177
- zuul 设置特定的某一个接口的超时时间,如: /opcenter/clue/upload/type,给出具体的properties 配置(476点数解答 | 2024-11-15 11:16:24)279
- springboot 如何添加logger日志输出,并每日归档(1766点数解答 | 2023-10-06 22:10:04)294