SpringBoot【问题 05】PostgreSQL数据库启用SSL后使用默认配置进行数据库连接(Navicat工具与Java程序)

官网SSL说明:https://www.postgresql.org/docs/9.1/libpq-ssl.html

1.配置

1.1 文件

使用SSL需要的4个文件,名称要一致:

  1. 客户端密钥:postgresql.key
  2. Java客户端密钥:postgresql.pk8
  3. 客户端证书:postgresql.crt
  4. 根证书:root.crt

1.2 目录

  • Linux
File Contents Effect
~/.postgresql/postgresql.crt client certificate requested by server
~/.postgresql/postgresql.key client private key proves client certificate sent by owner; does not indicate certificate owner is trustworthy
~/.postgresql/root.crt trusted certificate authorities checks that server certificate is signed by a trusted certificate authority
~/.postgresql/root.crl certificates revoked by certificate authorities server certificate must not be on this list
  • Windows

%APPDATA%\postgresql\实例:C:\Users\Administrator\AppData\Roaming\postgresql

2.测试

  • 使用SSL 模式选择 require或verify-ca都可以
  • C:\Users\Administrator\AppData\Roaming\postgresql下放置postgresql.key postgresql.crt root.crt3个文件

测试:

2.2 SpringBoot项目(Linux)

在使用 Spring Boot 连接 PostgreSQL 数据库时,如果需要使用 SSL 连接,那么私钥(sslkey)应该是 PKCS8 格式。可以使用 OpenSSL 工具将私钥转换为这种格式。来自StackOverflow的解决方案原文https://stackoverflow.com/questions/54257758/spring-boot-connection-to-postgresql-with-ssl。以下是具体的命令:

bash 复制代码
openssl pkcs8 -topk8 -inform PEM -outform DER -in postgresql.key -out postgresql.pk8 -nocrypt

未使用默认路径的自定义配置:

yaml 复制代码
driver-class-name: org.postgresql.Driver
url: jdbc:postgresql://localhost:25432/dbname?ssl=true&sslrootcert=pathTo/root.crt&sslcert=pathTo/postgresql.crt&sslkey=pathTo/postgresql.pk8
username: 'username'
password: 'password'

使用默认路径的配置:

yaml 复制代码
driver-class-name: org.postgresql.Driver
url: jdbc:postgresql://localhost:25432/dbname?sslmode=require
username: 'username'
password: 'password'

使用默认路径配置时:

  • sslmode为require或verify-ca都可以
  • ~/.postgresql下放置postgresql.pk8 postgresql.crt root.crt3个文件
  1. 测试 sslmode=require
bash 复制代码
# 目录下没有文件时
[FATAL: connection requires a valid client certificate]
# 添加root.crt后
[FATAL: connection requires a valid client certificate]
# 添加postgresql.crt后
[SSL error: Received fatal alert: unexpected_message]
# 添加postgresql.pk8
# 正常访问
  1. 测试 sslmode=verify-ca
bash 复制代码
# 目录下没有文件时
[Could not open SSL root certificate file C:\Users\Administrator\AppData\Roaming\postgresql\root.crt.]
# 添加root.crt后
[FATAL: connection requires a valid client certificate]
# 添加postgresql.crt后
[SSL error: Received fatal alert: unexpected_message]
# 添加postgresql.pk8
# 正常访问

3.总结

  • 使用默认配置要注意文件路径和文件名称
  • 其他应用程序要注意文件的权限
bash 复制代码
# 文件权限错误导致的连接失败
# 数据库连接失败:Could not open SSL root certificate file ~./postgresql/root.crt.
相关推荐
青春逝如流水3 小时前
Java 线程与操作系统、硬件的关系:状态、映射与差异
java·开发语言·多线程·ai文章
深入云栈3 小时前
Netty 4.2.x 源码深度解析 (十八):ChannelOutboundBuffer 写缓冲区 —— 发送队列与流量控制
java·后端·架构
我是人✓3 小时前
Java核心术语与内存机制深度解析
java
xuxigifxfh3 小时前
面试记录2026
java·mysql·面试·职场和发展
liangbo73 小时前
04-List 到底该用哪个?ArrayList、LinkedList、Vector、CopyOnWriteArrayList 选型实战
java
Wang's Blog3 小时前
Java 中间件之 RabbitMQ 快速入门: SpringAMQP WorkQueue 工作队列模型
java·中间件·java-rabbitmq
代码山河4 小时前
第一个Java程序:Hello World从零编写与运行
java·学习·架构·教程·面向对象·项目
codigger4 小时前
Java 26 深度解析:一次“性能兑现 + 契约收紧”的版本
java·后端·编程·编程语言
盖世英雄酱581364 小时前
格式化成日期为什么提前一天呢,同样的时间点还是非必现???
java·后端
威哥爱编程4 小时前
【AI全栈12-05】Spring Boot 用 Tool Calling 让 AI 查实时订单与库存
java·spring boot·后端