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

news/2024/7/9 23:06:09 标签: SSL, PostgreSQL, 数据库连接, 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
FileContentsEffect
~/.postgresql/postgresql.crtclient certificaterequested by server
~/.postgresql/postgresql.keyclient private keyproves client certificate sent by owner; does not indicate certificate owner is trustworthy
~/.postgresql/root.crttrusted certificate authoritieschecks that server certificate is signed by a trusted certificate authority
~/.postgresql/root.crlcertificates revoked by certificate authoritiesserver certificate must not be on this list
  • Windows

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

2.测试

NavicatWindows_24">2.1 Navicat工具(Windows)

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

Windows.png
测试:

<a class=Navicat.png" />

2.2 SpringBoot项目(Linux)

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

openssl pkcs8 -topk8 -inform PEM -outform DER -in postgresql.key -out postgresql.pk8 -nocrypt

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

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'

使用默认路径的配置:

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个文件

Linux.png

  1. 测试 sslmode=require
# 目录下没有文件时
[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
# 目录下没有文件时
[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.总结

  • 使用默认配置要注意文件路径和文件名称
  • 其他应用程序要注意文件的权限
# 文件权限错误导致的连接失败
# 数据库连接失败:Could not open SSL root certificate file ~./postgresql/root.crt.

http://www.niftyadmin.cn/n/5405688.html

相关文章

Unity 无缝地图-理解思想原理版

1、创建地形 创建地形&#xff0c;长宽设置55. 创建55个格子的地形 设定角色 创建一个胶囊&#xff0c;添加刚体组件&#xff0c;冻结旋转&#xff0c;将相机设置为胶囊的子物体&#xff08;相机跟随&#xff09;&#xff0c;把胶囊标签设置为player。 创建脚本&#xf…

【go从入门到精通】go基本类型和运算符用法

大家好&#xff0c;这是我给大家准备的新的一期专栏&#xff0c;专门讲golang&#xff0c;从入门到精通各种框架和中间件&#xff0c;工具类库&#xff0c;希望对go有兴趣的同学可以订阅此专栏。 --------------------------------------------------------------------------…

比sora早半年?元宇宙2086 中国影视AIGC视频的开创者之一

今年初&#xff0c;OpenAI公布文生视频模型Sora&#xff0c;输入寥寥数语&#xff0c;即可生成震撼人心的一分钟视频&#xff0c;远远超过Runway的18秒。Sora惊艳亮相&#xff0c;人们再次被人工智能的无限可能所震撼。世界各国对“Sora”的搜索量短时飙升&#xff0c;尤其是在…

ElasticSearch之文档的存储

写在前面 本文看下文档的存储相关内容。 1&#xff1a;如何确定文档存储在哪个分片&#xff1f; 我们需要确保文档均匀分布在所有的分片中&#xff0c;避免某些部分机器空闲&#xff0c;部分机器繁忙的情况出现&#xff0c;想要实现均匀分布我们可以考虑如下的几种分片路由算…

C++ //练习 10.31 修改前一题的程序,使其只打印不重复的元素。你的程序应使用unique_copy(参见10.4.1节,第359页)。

C Primer&#xff08;第5版&#xff09; 练习 10.31 练习 10.31 修改前一题的程序&#xff0c;使其只打印不重复的元素。你的程序应使用unique_copy&#xff08;参见10.4.1节&#xff0c;第359页&#xff09;。 环境&#xff1a;Linux Ubuntu&#xff08;云服务器&#xff09…

gpt生成器,批量gpt文章生成器

GPT&#xff08;生成式预训练模型&#xff09;生成器软件在当今的数字化时代扮演着越来越重要的角色&#xff0c;它们通过人工智能技术&#xff0c;可以自动生成各种类型的文章内容&#xff0c;为用户提供了无限的创作可能性。本文将介绍6款不同的GPT生成器软件&#xff0c;并介…

数据结构知识点总结12-(第六章.图)-图的存储结构及图的遍历

专栏主页&#xff1a;计算机专业基础知识总结&#xff08;适用于期末复习考研刷题求职面试&#xff09;系列文章https://blog.csdn.net/seeker1994/category_12585732.html ...... 数据结构知识点总结11-(第六章.图)-图的基本概念 数据结构知识点总结13-(第六章.图)-图的应用…

全员996!OpenAI匿名员工自曝3年工作感受

在OpenAI工作&#xff0c;是怎样的体验&#xff1f; 前段时间&#xff0c;华人员工Jason Wei自曝996作息表&#xff0c;走红全网&#xff0c;让网友纷纷看到了现实中的自己。 OpenAI视频生成的研究员Will Depue称&#xff0c;「这简直就是我的一天」。 近来&#xff0c;一位Op…