【问题标题】:How to create SSL certificate & implement in Spring Boot?如何在 Spring Boot 中创建 SSL 证书并实现?
【发布时间】:2025-12-12 04:15:01
【问题描述】:

遵循 spring security book,我尝试通过以下命令创建 SSL 证书,但出现了我在最后提到的错误。感谢您的帮助。我已经尝试过 openssl 以及 wimpty:

openssl req -newkey rsa:2048 -x509 -keyout key.pem -out cert.pem -days 365
openssl pkcs12 -export -in cert.pem -inkey key.pem -out certificate.p12
-name "certificate"

application.properties

server.port=8088
server.ssl.key-store-type=PKCS12
server.ssl.key-store=classpath:certificate.p12
server.ssl.key-password=12345

另外,我已将 certificate.p12 放在资源文件夹下。

但是所有的努力都以以下错误告终:

2021-11-21 23:49:20.738  INFO 16764 --- [           main] com.kash.SpringsecurityApplication       
: Starting SpringsecurityApplication using Java 11.0.12 on LAPTOP-BQ48GM36 with PID 16764 
(B:\spring\springsecurity\target\classes started by The Kash in B:\spring\springsecurity)
2021-11-21 23:49:20.742  INFO 16764 --- [           main] com.kash.SpringsecurityApplication       
: No active profile set, falling back to default profiles: default
2021-11-21 23:49:22.272  WARN 16764 --- [           main] 
ConfigServletWebServerApplicationContext : Exception encountered during context initialization 
- cancelling refresh attempt: org.springframework.context.ApplicationContextException: Unable 
to start web server; nested exception is 
org.springframework.boot.web.server.WebServerException: Could not load key store 
'classpath:certificate.p12'
2021-11-21 23:49:22.280  INFO 16764 --- [           main] 
ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application 
with 'debug' enabled.
2021-11-21 23:49:22.299 ERROR 16764 --- [           main] o.s.boot.SpringApplication               
: Application run failed

org.springframework.context.ApplicationContextException: Unable to start web server; nested 
exception is org.springframework.boot.web.server.WebServerException: Could not load key store 
'classpath:certificate.p12'
at g.springframework.boot.web.servlet.context.
 ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:163) 
~[spring-boot-2.6.0.jar:2.6.0]
 at org.springframework.context.
 support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:577) ~[spring- 
 context-5.3.13.jar:5.3.13]
at org.springframework.boot.web.servlet.context.
ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145) ~ 
[spring-boot-2.6.0.jar:2.6.0]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:730) ~[spring- 
boot-2.6.0.jar:2.6.0]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:412) ~ 
[spring-boot-2.6.0.jar:2.6.0]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:302) ~[spring-boot- 
 2.6.0.jar:2.6.0]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1301) ~[spring-boot- 
 2.6.0.jar:2.6.0]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1290) ~[spring-boot- 
2.6.0.jar:2.6.0]
at com.kash.SpringsecurityApplication.main(SpringsecurityApplication.java:10) ~[classes/:na]
 Caused by: org.springframework.boot.web.server.WebServerException: Could not load key store 
 'classpath:certificate.p12'
    at org.springframework.boot.web.embedded.tomcat
 .SslConnectorCustomizer.configureSslKeyStore(SslConnectorCustomizer.java:132) ~[spring-boot- 
 2.6.0.jar:2.6.0]org.springframework.boot.web.embedded.tomcat.S
 at slConnectorCustomizer.configureSsl(SslConnectorCustomizer.java:92) ~[spring-boot- 
 2.6.0.jar:2.6.0]
    at org.springframework.boot.web.embedded
   .tomcat.SslConnectorCustomizer.configureSslKeyStore(SslConnectorCustomizer.java:129) ~ 
  [spring-boot-2.6.0.jar:2.6.0]
    ... 15 common frames omitted

【问题讨论】:

  • 什么是wimpty?你在看什么书?

标签: spring-boot openssl ssl-certificate


【解决方案1】:

我删除该项目并重新创建它。 & 使用以下有效的命令:

keytool -genkey -alias -storetype JKS -keyalg RSA -keysize 2048 -validity 365 -keystore

还建议将jks迁移到pkcs512

keytool -importkeystore -srckeystore certificate.jks -destkeystore certificate.p12 -deststoretype pkcs12

确保生成的 .p12 文件应该放在资源文件夹下。

【讨论】:

    最近更新 更多