- 创建或获取 SSL 证书
首先,你需要一个 SSL 证书。可以使用自签名证书进行本地开发。使用 Java 自带的 keytool 工具生成自签名证书。
生成自签名证书:
keytool -genkeypair -alias myapp -keyalg RSA -keysize 2048 -storetype PKCS12 -keystore keystore.p12 -validity 3650
此命令将生成一个 keystore.p12 文件,该文件包含自签名证书,证书有效期为10年。你将被提示输入一些信息(如密码、名称等)。
如何判断升成的是否有效:
keytool -list -keystore keystore.p12 -storetype PKCS12
keytool -genkeypair -alias myapp -keyalg RSA -keysize 2048 -storetype PKCS12 -keystore keystore.p12 -validity 3650
输入密钥库口令:
再次输入新口令:
您的名字与姓氏是什么?
[Unknown]: cloud
您的组织单位名称是什么?
[Unknown]: juhe
您的组织名称是什么?
[Unknown]: juhecn
您所在的城市或区域名称是什么?
[Unknown]: suzhou
您所在的省/市/自治区名称是什么?
[Unknown]: js
该单位的双字母国家/地区代码是什么?
[Unknown]: CN
CN=cloud, OU=juhe, O=juhecn, L=suzhou, ST=js, C=CN是否正确?
[否]: 是
正在为以下对象生成 2,048 位RSA密钥对和自签名证书 (SHA256withRSA) (有效期为 3,650 天):
CN=cloud, OU=juhe, O=juhecn, L=suzhou, ST=js, C=CN
keytool -list -keystore keystore.p12 -storetype PKCS12
输入密钥库口令:
密钥库类型: PKCS12
密钥库提供方: SUN
您的密钥库包含 1 个条目
myapp, 2024年9月5日, PrivateKeyEntry,
证书指纹 (SHA-256): 74:5D:37:8F:6B:D3:9C:04:F9:4D:97:C2:4D:1B:54:62:2B:10:2C:AE:CB:5E:A2:68:49:E6:34:88:BF:76:B5:89
- 配置 Spring Boot 应用程序
接下来,你需要在 application.yml 或 application.properties 文件中配置 HTTPS。
如果使用 application.yml:
server:
port: 8080
ssl:
key-store: /Users/java0904/bayarea/src/main/resources/keystore.p12
key-store-password: wei123
key-store-type: PKCS12
key-alias: myapp
enabled: true
注意:这里的 key-store 写成绝对路径,不然启动报错
2024-09-05 14:06:27.917 WARN 71156 --- [ main] o.a.c.loader.WebappClassLoaderBase : The web application [api] appears to have started a thread named [ForkJoinPool.commonPool-worker-2] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
sun.misc.Unsafe.park(Native Method)
java.util.concurrent.ForkJoinPool.awaitWork(ForkJoinPool.java:1835)
java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1704)
java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:172)
2024-09-05 14:06:27.917 WARN 71156 --- [ main] o.a.c.loader.WebappClassLoaderBase : The web application [api] appears to have started a thread named [ForkJoinPool.commonPool-worker-3] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
sun.misc.Unsafe.park(Native Method)
java.util.concurrent.ForkJoinPool.awaitWork(ForkJoinPool.java:1835)
java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1704)
java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:172)
2024-09-05 14:06:27.918 WARN 71156 --- [ main] o.a.c.loader.WebappClassLoaderBase : The web application [api] appears to have started a thread named [ForkJoinPool.commonPool-worker-4] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
sun.misc.Unsafe.park(Native Method)
java.util.concurrent.ForkJoinPool.awaitWork(ForkJoinPool.java:1835)
java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1704)
java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:172)
2024-09-05 14:06:27.918 WARN 71156 --- [ main] o.a.c.loader.WebappClassLoaderBase : The web application [api] appears to have started a thread named [ForkJoinPool.commonPool-worker-5] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
sun.misc.Unsafe.park(Native Method)
java.util.concurrent.ForkJoinPool.awaitWork(ForkJoinPool.java:1835)
java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1704)
java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:172)
2024-09-05 14:06:27.919 WARN 71156 --- [ main] o.a.c.loader.WebappClassLoaderBase : The web application [api] appears to have started a thread named [ForkJoinPool.commonPool-worker-6] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
sun.misc.Unsafe.park(Native Method)
java.util.concurrent.ForkJoinPool.awaitWork(ForkJoinPool.java:1835)
java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1704)
java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:172)
2024-09-05 14:06:27.919 WARN 71156 --- [ main] o.a.c.loader.WebappClassLoaderBase : The web application [api] appears to have started a thread named [ForkJoinPool.commonPool-worker-7] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
sun.misc.Unsafe.park(Native Method)
java.util.concurrent.ForkJoinPool.awaitWork(ForkJoinPool.java:1835)
java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1704)
java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:172)
2024-09-05 14:06:27.927 INFO 71156 --- [ main] ConditionEvaluationReportLoggingListener :
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2024-09-05 14:06:27.941 ERROR 71156 --- [ main] o.s.boot.SpringApplication : Application run failed
org.springframework.context.ApplicationContextException: Failed to start bean 'webServerStartStop'; nested exception is org.springframework.boot.web.server.WebServerException: Unable to start embedded Tomcat server
at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:181)
at org.springframework.context.support.DefaultLifecycleProcessor.access$200(DefaultLifecycleProcessor.java:54)
at org.springframework.context.support.DefaultLifecycleProcessor$LifecycleGroup.start(DefaultLifecycleProcessor.java:356)
at java.lang.Iterable.forEach(Iterable.java:75)
at org.springframework.context.support.DefaultLifecycleProcessor.startBeans(DefaultLifecycleProcessor.java:155)
at org.springframework.context.support.DefaultLifecycleProcessor.onRefresh(DefaultLifecycleProcessor.java:123)
at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:935)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:586)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:740)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:415)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:303)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1312)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1301)
at com.juhe.bayarea.BayAreaApplication.main(BayAreaApplication.java:16)
Caused by: org.springframework.boot.web.server.WebServerException: Unable to start embedded Tomcat server
at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.start(TomcatWebServer.java:229)
at org.springframework.boot.web.servlet.context.WebServerStartStopLifecycle.start(WebServerStartStopLifecycle.java:43)
at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:178)
... 14 common frames omitted
Caused by: java.lang.IllegalArgumentException: standardService.connector.startFailed
at org.apache.catalina.core.StandardService.addConnector(StandardService.java:238)
at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.addPreviouslyRemovedConnectors(TomcatWebServer.java:282)
at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.start(TomcatWebServer.java:213)
... 16 common frames omitted
Caused by: org.apache.catalina.LifecycleException: Protocol handler start failed
at org.apache.catalina.connector.Connector.startInternal(Connector.java:1075)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
at org.apache.catalina.core.StandardService.addConnector(StandardService.java:234)
... 18 common frames omitted
Caused by: java.lang.IllegalArgumentException: DerInputStream.getLength(): lengthTag=111, too big.
at org.apache.tomcat.util.net.AbstractJsseEndpoint.createSSLContext(AbstractJsseEndpoint.java:99)
at org.apache.tomcat.util.net.AbstractJsseEndpoint.initialiseSsl(AbstractJsseEndpoint.java:71)
at org.apache.tomcat.util.net.NioEndpoint.bind(NioEndpoint.java:234)
at org.apache.tomcat.util.net.AbstractEndpoint.bindWithCleanup(AbstractEndpoint.java:1227)
at org.apache.tomcat.util.net.AbstractEndpoint.start(AbstractEndpoint.java:1313)
at org.apache.coyote.AbstractProtocol.start(AbstractProtocol.java:614)
at org.apache.catalina.connector.Connector.startInternal(Connector.java:1072)
... 20 common frames omitted
Caused by: java.io.IOException: DerInputStream.getLength(): lengthTag=111, too big.
at sun.security.util.DerInputStream.getLength(DerInputStream.java:616)
at sun.security.util.DerValue.init(DerValue.java:391)
at sun.security.util.DerValue.<init>(DerValue.java:332)
at sun.security.util.DerValue.<init>(DerValue.java:345)
at sun.security.pkcs12.PKCS12KeyStore.engineLoad(PKCS12KeyStore.java:1895)
at java.security.KeyStore.load(KeyStore.java:1449)
at org.apache.tomcat.util.security.KeyStoreUtil.load(KeyStoreUtil.java:67)
at org.apache.tomcat.util.net.SSLUtilBase.getStore(SSLUtilBase.java:215)
at org.apache.tomcat.util.net.SSLHostConfigCertificate.getCertificateKeystore(SSLHostConfigCertificate.java:207)
at org.apache.tomcat.util.net.SSLUtilBase.getKeyManagers(SSLUtilBase.java:281)
at org.apache.tomcat.util.net.SSLUtilBase.createSSLContext(SSLUtilBase.java:245)
at org.apache.tomcat.util.net.AbstractJsseEndpoint.createSSLContext(AbstractJsseEndpoint.java:97)
... 26 common frames omitted
```