【解决方案】SpringBoot项目添加ssl证书后不生效问题

SpringBoot项目添加ssl证书后不生效问题

缘

在我的网站中添加证书后报错,不是打包失败,就是证书添加后启动失败,亦或者是前端页面请求后端地址失败。

解决方案

. 打包报错

报错:
bash 复制代码
(default-resources) on project pig-boot: filtering G:\INFINITE-SISM\cursor\mysite\platform-irule - cursor\pig-boot\src\main\resources\liufei.xyz.pfx to G:\INFINITE-SISM\cursor\mysite\platform-irule - cursor\pig-boot\target\classes\liufei.xyz.pfx failed with MalformedInputException: Input length = 1 -> [Help 1]
原因:

springboot编译resource中二进制文件导致的报错。

解决方案:

在pom.xml中添加以下内容:

bash 复制代码
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-resources-plugin</artifactId>
				<configuration>
					<!-- SSL 密钥库是二进制文件,必须禁止 Maven 资源过滤 -->
					<nonFilteredFileExtensions>
						<nonFilteredFileExtension>pfx</nonFilteredFileExtension>
						<nonFilteredFileExtension>p12</nonFilteredFileExtension>
						<nonFilteredFileExtension>jks</nonFilteredFileExtension>
					</nonFilteredFileExtensions>
				</configuration>
			</plugin>

.证书报错

报错
bash 复制代码
Unable to create key store: Could not load store from 'classpath:game.*.xyz.pfx'
DerInputStream.getLength(): lengthTag=111, too big.
原因:

resource中的证书文件与target中的证书文件不一致,文件被当成文本编译。

解决方案:

在pom.xml中添加以下内容:

bash 复制代码
			
		<resources>
			<resource>
				<directory>src/main/resources</directory>
				<filtering>true</filtering>
				<excludes>
					<exclude>ssl/**</exclude>
				</excludes>
			</resource>
			<resource>
				<directory>src/main/resources</directory>
				<filtering>false</filtering>
				<includes>
					<include>ssl/**</include>
				</includes>
			</resource>
		</resources>

.前端访问接口报错

报错
bash 复制代码
Unable to create key store: Could not load store from 'classpath:game.*.xyz.pfx'
DerInputStream.getLength(): lengthTag=111, too big.
原因:

HTTPS 会校验证书域名。你的证书只包含:

.xyz
www.
.xyz

不包含 127.0.0.1

解决方案:

在vite.config.js的转发中配置允许非 HTTPS 连接:

bash 复制代码
    proxy: {
        '/api': {
          target: env.VITE_ADMIN_PROXY_PATH, 
          ws: true,  
          changeOrigin: true,  
          secure: false 
相关推荐
高频因子挖掘机22 分钟前
回测收益为什么可能不真实?从时间戳、数据可用性到未来函数检查
后端·github·api
行百里er9 小时前
Redis 完结篇——Redis 热点问题在 Spring Boot 项目中实战
redis·后端
高频因子挖掘机10 小时前
只取最近 60 个交易日,却每次拉全历史:量化脚本变慢的原因与改法
后端·github·api
孟健10 小时前
从5000亿Token到83%字节匹配:AI逆向工程与重构的工程闭环实践
后端·架构·ai编程
paopaokaka_luck11 小时前
滑雪场管理系统(AI智能助手流式对话,ECharts 运营数据分析、教练、雪具与场地一体化预约,钱包支付退款、订单核销与服务评价)
java·人工智能·spring boot·网络协议·数据分析·echarts
SL_staff11 小时前
JVS混合架构实践:能力引擎+动态模型如何支撑企业级可持续交付
spring boot·低代码·全栈
码事漫谈11 小时前
丢得静悄悄,谈一谈 Agent 的记忆是怎么没的
后端
迪飞特科技11 小时前
智慧物业平台实战:Spring Boot 整合小程序实现报修巡检费用全流程
spring boot·后端·小程序
考虑考虑11 小时前
@JsonInclude注解使用
spring boot·后端·spring
IT_陈寒12 小时前
Vite的这个问题坑了我三天,原来如此解决!
前端·人工智能·后端