当前位置: 首页 > article >正文

本地 springboot 项目如何使用 https 进行访问

  1. 创建或获取 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

  1. 配置 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
	```





http://www.kler.cn/a/292348.html

相关文章:

  • AdaBoost 二分类问题
  • Springboot定时任务
  • 【Java Web】Ajax 介绍及 jQuery 实现
  • git命令提交项目
  • STM32学习笔记-----UART的概念
  • 深度解析:Android APP集成与拉起微信小程序开发全攻略
  • Java项目:141 springboot大学生智能消费记账系统的设计与实现
  • 拥抱分布式云:云基础设施的下个新时代
  • 目标检测-YOLOv1
  • IDEA运行Java程序提示“java: 警告: 源发行版 11 需要目标发行版 11”
  • java项目热部署
  • 蚂蚁SEO|AI养站程序是什么|蚂蚁蜘蛛池
  • 职场关系课:职场上的基本原则(安全原则、进步原则、收益原则、逃生舱原则)
  • 【Godot4.3】CanvasShape资源化改造
  • ChatGPT付费创作系统V3.0.6独立版 WEB+H5+小程序端 (新增AI全网搜索+文档解析+豆包AI通道)安装部署教程
  • 专访|“技术+市场”双轮驱动,积鼎科技如何打造国产CFD流体仿真软件标杆品牌?
  • WPS中JS宏使用说明(持续优化...)
  • Flutter中添加崩溃分析
  • 【云原生•容器】Docker架构剖析,它还是从前那个Docker吗?(上)
  • 记录Java秋招面经(网上找的)
  • 江协科技STM32学习- P11 中断系统,EXTI外部中断
  • Java项目:139 springboot基于SpringBoot的论坛系统设计与实现
  • SpringBoot开发——整合Logbook进行HTTP API请求响应日志输出
  • 【算法分享】双向长短期记忆(BiLSTM)及长短期记忆 (LSTM)网络详细文档
  • pdf怎么压缩小一些?推荐的几种PDF压缩方法
  • Ansible自动化运维实战:打造高效、可靠的系统管理方案!