SpringBoot_minio sdk使用自签名https证书错误处理

minio sdk使用自签名https证书错误处理

  • 1.问题描述
    • 1.1 报错日志
    • 1.2 maven 依赖配置
    • 1.3 当前spring MinioClient配置
  • 2.问题分析
  • 3.问题解决
    • 3.1 使用受信任的证书
    • 3.2 忽略证书验证
      • 3.2.1 minio客户端
      • 3.2.2 minio sdk 忽略证书验证
        • 3.2.2.1 拓展: 补充minioclient请求日志
  • 4. 问题总结
  • 5.附录

1.问题描述

minio 8.4.4 使用自签名的https的api连接会报错证书错误

1.1 报错日志

PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

1.2 maven 依赖配置

        <!--minio java sdk--><dependency><groupId>io.minio</groupId><artifactId>minio</artifactId><version>8.4.4</version><exclusions><exclusion><groupId>com.squareup.okhttp3</groupId><artifactId>okhttp</artifactId></exclusion></exclusions></dependency><dependency><groupId>com.squareup.okhttp3</groupId><artifactId>okhttp</artifactId><version>4.10.0</version></dependency>

1.3 当前spring MinioClient配置

@Configuration
@EnableConfigurationProperties(MinioProperties.class)
public class MinioConfig {@Beanpublic MinioClient minioClient(MinioProperties properties){properties.check();return MinioClient.builder().endpoint(properties.getEndpoint()).credentials(properties.getAccessKey(), properties.getSecretKey()).build();}
}

2.问题分析

通常是因为MinIO默认情况下会验证服务器的TLS证书。在生产环境中,使用自签名证书并不推荐,因为它们不受信任的证书颁发机构(CA)签署,可能会导致安全问题。

3.问题解决

3.1 使用受信任的证书

为了在生产环境中确保安全性,建议获取一个受信任的SSL证书,可以从证书颁发机构(CA)购买,或者使用免费的证书颁发机构(例如Let’s Encrypt)获取SSL证书。

3.2 忽略证书验证

3.2.1 minio客户端

在MinIO客户端(例如mc命令行工具)中,可以使用–insecure选项来忽略证书验证。例如:

mc --insecure <command>

这会告诉MinIO客户端不要验证服务器的TLS证书。请注意,这种做法会降低安全性,不建议在生产环境中使用。

3.2.2 minio sdk 忽略证书验证

在使用Java SDK与自签名证书的服务器进行通信时,一般可以通过自定义SSLContext来忽略证书验证。

MinIO的Java SDK(version 8.0.6及以上)允许自定义OkHttpClient,我们可以使用httpClient方法传递一个自定义的OkHttpClient实例。以便在HTTP、正常HTTPS和自签名HTTPS之间实现兼容性

下面是如何使用自定义的OkHttpClient实现对HTTP、正常HTTPS和自签名HTTPS的兼容性

@Configuration
@EnableConfigurationProperties(MinioProperties.class)
public class MinioConfig {private static final Logger LOGGER = LoggerFactory.getLogger(MinioConfig.class);@Beanpublic MinioClient minioClient(MinioProperties properties){properties.check();// Create a trust manager that does not validate certificate chainsTrustManager[] trustAllCerts = new TrustManager[] {new X509TrustManager() {public X509Certificate[] getAcceptedIssuers() {return new X509Certificate[0];}public void checkClientTrusted(X509Certificate[] certs, String authType) {// Do nothing (trust any client certificate)}public void checkServerTrusted(X509Certificate[] certs, String authType) {// Do nothing (trust any server certificate)}}};// Install the all-trusting trust managerSSLContext sslContext = null;try {sslContext = SSLContext.getInstance("SSL");sslContext.init(null, trustAllCerts, new java.security.SecureRandom());} catch (Exception e) {LOGGER.error("Install the all-trusting trust manager error:{}", e.getMessage());}// Create a custom OkHttpClient that trusts all certificatesOkHttpClient customHttpClient = new OkHttpClient.Builder().sslSocketFactory(sslContext.getSocketFactory(), (X509TrustManager) trustAllCerts[0]).hostnameVerifier((hostname, session) -> true).build();// Set the custom SSLContext for MinioClientreturn MinioClient.builder().endpoint(properties.getEndpoint()).credentials(properties.getAccessKey(), properties.getSecretKey()).httpClient(customHttpClient).build();}}
  1. 在上面的代码中,我们创建了一个SSLContext,其中的X509TrustManager会信任所有证书,无论其是否由信任的证书颁发机构(CA)签署。
  2. 创建了一个自定义的OkHttpClient,该客户端信任所有证书。然后,我们使用MinioClient.builder()方法,将自定义的OkHttpClient传递给httpClient()方法

这种方法会将所有证书都视为受信任的,因此请仅在非生产环境中使用此方法,以确保通信的安全性和完整性。在生产环境中,建议使用受信任的SSL证书。

3.2.2.1 拓展: 补充minioclient请求日志

之前minioclient与服务器端交互使用默认的httpclient的客户端,请求没有打印详细日志. 既然上面自定义自己的httpclient那么可以补充自定义拦截器打印日志

public class CustomLoggingInterceptor implements Interceptor {@Overridepublic Response intercept(Chain chain) throws IOException {Request request = chain.request();long startTime = System.nanoTime();System.out.println("Sending request " + request.url() + " on " + chain.connection() + "\n" + request.headers());Response response = chain.proceed(request);long endTime = System.nanoTime();System.out.println("Received response for " + response.request().url() + " in " + ((endTime - startTime) / 1e6) + "ms\n" + response.headers());MediaType contentType = response.body().contentType();String content = response.body().string();System.out.println("Response body:\n" + content);ResponseBody wrappedBody = ResponseBody.create(contentType, content);return response.newBuilder().body(wrappedBody).build();}
}

修改MinioConfig增加okhttp拦截器

        // Create a custom OkHttpClient that trusts all certificatesOkHttpClient customHttpClient = new OkHttpClient.Builder().sslSocketFactory(sslContext.getSocketFactory(), (X509TrustManager) trustAllCerts[0]).hostnameVerifier((hostname, session) -> true).addInterceptor(new CustomLoggingInterceptor()) // Add custom interceptor here.build();

效果
在这里插入图片描述

4. 问题总结

minio客户端本质使用httpclient与服务端交互,因此证书问题处理其实只是httpclient对证书的兼容处理。该处理方式可以运用到其他使用到httpclient的场景。

5.附录

代码优化

@Configuration
@EnableConfigurationProperties(MinioProperties.class)
public class MinioConfig {private static final Logger LOGGER = LoggerFactory.getLogger(MinioConfig.class);@Beanpublic MinioClient minioClient(MinioProperties properties){properties.check();OkHttpClient customHttpClient = null;if (properties.getEndpoint().startsWith("https://")) {// 如果是HTTPS,使用自定义的OkHttpClient处理自签名的HTTPS请求customHttpClient = createCustomOkHttpClient();}MinioClient minioClient;if (customHttpClient != null) {// 如果使用了自定义的OkHttpClientminioClient = MinioClient.builder().endpoint(properties.getEndpoint()).credentials(properties.getAccessKey(), properties.getSecretKey()).httpClient(customHttpClient).build();} else {// 如果是普通HTTP,使用默认的OkHttpClientminioClient = MinioClient.builder().endpoint(properties.getEndpoint()).credentials(properties.getAccessKey(), properties.getSecretKey()).build();}return minioClient;}/*** Set the custom SSLContext for MinioClient* @return*/private static OkHttpClient createCustomOkHttpClient() {// 创建自定义的OkHttpClient,用于处理自签名的HTTPS请求// Create a trust manager that does not validate certificate chainsTrustManager[] trustAllCerts = new TrustManager[] {new X509TrustManager() {public X509Certificate[] getAcceptedIssuers() {return new X509Certificate[0];}public void checkClientTrusted(X509Certificate[] certs, String authType) {// Do nothing (trust any client certificate)}public void checkServerTrusted(X509Certificate[] certs, String authType) {// Do nothing (trust any server certificate)}}};// Install the all-trusting trust managerSSLContext sslContext = null;try {sslContext = SSLContext.getInstance("SSL");sslContext.init(null, trustAllCerts, new java.security.SecureRandom());} catch (Exception e) {LOGGER.error("Install the all-trusting trust manager error:{}", e.getMessage());}// Create a custom OkHttpClient that trusts all certificatesOkHttpClient customHttpClient = new OkHttpClient.Builder().sslSocketFactory(sslContext.getSocketFactory(), (X509TrustManager) trustAllCerts[0]).hostnameVerifier((hostname, session) -> true)// 增加minio http请求日志打印//.addInterceptor(new CustomLoggingInterceptor()) // Add custom interceptor here.build();return customHttpClient;}}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/119721.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

树形数据增删改查

功能描述&#xff1a; 默认展示所有项目点击项目展示当前项目下的所有区域点击区域展示当前区域下的所有工位以上以树形图格式展示项目&#xff0c;区域&#xff0c;和工位都可进行增加 修改 和删除&#xff0c;每个图标hover时显示对应提示信息项目&#xff0c;区域&#xff…

illuminate/database 使用 二

上一篇文章写怎么单独使用illuminate/database&#xff0c;这回讲下怎么整合到项目里使用。为此特意看了下laravel对其使用。本篇文章&#xff0c;参照laravel的使用&#xff0c;简单实现。 一 原理 laravel 里使用illuminate/config。 illuminate/config composer 地址&…

Spring Boot整合OAuth2实现GitHub第三方登录

Spring Boot整合OAuth2&#xff0c;实现GitHub第三方登录 1、第三方登录原理 第三方登录的原理是借助OAuth授权来实现&#xff0c;首先用户先向客户端提供第三方网站的数据证明自己的身份获取授权码&#xff0c;然后客户端拿着授权码与授权服务器建立连接获得一个Access Token…

vuejs实现点击导出按钮把数据加密后传到json/txt格式文件中并下载,以及上传json文件解密获得json内容

vuejs实现点击导出按钮把数据加密后传到json/txt格式文件中并下载&#xff0c;以及上传json文件解密获得json内容 &#xff08;1&#xff09;在Vue.js中使用crypto-js进行加密和解密&#xff0c;首先安装crypto-js库 npm install crypto-js&#xff08;2&#xff09;在需要使…

研发效能认证学员作品:快速进行持续集成应用实践丨IDCF

作者&#xff1a;赖嘉明 研发效能&#xff08;DevOps&#xff09;工程师认证学员 随着数字化转型的推进及市场竞争的加剧&#xff0c;越来越多的企业也意识到持续集成的重要性。 而持续集成作为一种先进的软件开发实践和工具链&#xff0c;可以帮助企业实现自动化构建、集成和…

systemctl 自启软件闪屏桌面

一、问题分析 systemctl 服务启动在桌面系统之前&#xff0c;启动界面加载到 100% 时桌面系统开始加载&#xff0c;会强制隐藏我们的界面并显示桌面&#xff0c;待桌面彻底加载完毕&#xff0c;才能显示我们的软件界面。这期间就是闪屏并显示桌面的原因。 不过正常情况桌面系…

STM32CubeMX和STM32F4

目录 嵌入式开发的硬件相关STM32CubeMXSTM32F4Cortex-M4-FSFPU 嵌入式开发的软件相关μC/OS-II 嵌入式开发的硬件相关 STM32CubeMX STM32CubeMX是一个基于图形界面的工具&#xff08;是软件&#xff09;,用于配置和生成STM32微控制器的初始化代码和库文件。它适用于基于ARM C…

文件上传漏洞(1), 文件上传绕过原理

文件上传漏洞 一, 前端校验上传文件 添加 Javascript 代码&#xff0c;然后在 form 表单中 添加 onsubmit"returb checkFile()" <script>function checkFile() {// var file document.getElementsByName(photo)[0].value;var file document.getElementByI…

基于YOLOv8模型的烟雾目标检测系统(PyTorch+Pyside6+YOLOv8模型)

摘要&#xff1a;基于YOLOv8模型的烟雾目标检测系统可用于日常生活中检测与定位烟雾目标&#xff0c;利用深度学习算法可实现图片、视频、摄像头等方式的目标检测&#xff0c;另外本系统还支持图片、视频等格式的结果可视化与结果导出。本系统采用YOLOv8目标检测算法训练数据集…

elasticsearch-7.9.3 单节点启动配置

一、elasticsearch-7.9.3 单节点启动配置 node.name: node-1 network.host: 192.168.227.128 http.port: 9200 discovery.seed_hosts: ["192.168.227.128"] node.max_local_storage_nodes: 1 discovery.type: single-node二、kibana-7.9.3-linux-x86_64 单节点启动配…

Ubuntu22.04安装,SSH无法连接

Ubuntu初始化安装后&#xff0c;系统默认不允许root通过ssh连接&#xff0c;因此需要完成三个设置 1.修改ssh配置文件 vim /etc/ssh/sshd_config 将PermitRootLogin注释打开&#xff0c;并将值改为yes 保存修改并退出 :wq 2.重启ssh服务 sudo service ssh restart 3.重新打…

spark3.3.x处理excel数据

环境: spark3.3.x scala2.12.x 引用: spark-shell --jars spark-excel_2.12-3.3.1_0.18.5.jar 或项目里配置pom.xml <!-- https://mvnrepository.com/artifact/com.crealytics/spark-excel --> <dependency><groupId>com.crealytics</groupId><art…

八大排序算法(C语言版)之插入排序

八大排序详解 目录&#xff1a;一、排序的概念1.1 排序的概念1.2 排序的应用 二、直接插入排序三、希尔排序四、排序算法复杂度及稳定性分析 目录&#xff1a; 八大排序算法&#xff1a; #mermaid-svg-7qCaGEYz0Jyj9dYw {font-family:"trebuchet ms",verdana,arial,…

[极客大挑战 2019]Havefun

1.打开链接 2.检查一下源代码 发现一段代码。 3.分析代码 <!-- $cat$_GET[cat]; echo $cat; if($catdog){ echo Syc{cat_cat_cat_cat}; } --> 询问ChatGPT&#xff1a; 从您提供的代码片段来看&#xff0c;这是…

C++中低级内存操作

C中低级内存操作 C相较于C有一个巨大的优势&#xff0c;那就是你不需要过多地担心内存管理。如果你使用面向对象的编程方式&#xff0c;你只需要确保每个独立的类都能妥善地管理自己的内存。通过构造和析构&#xff0c;编译器会帮助你管理内存&#xff0c;告诉你什么时候需要进…

GB/T 29734.2-2013 铝塑复合门窗检测

铝合金门窗是指采用铝塑复合型材制作框、扇杆件结构的门、窗的总称&#xff0c;根据门窗开启形式的不同&#xff0c;可分为固定窗、平开窗、推拉窗&#xff0c;悬窗等。 GB/T 29734.2-2013 铝塑复合门窗检测项目 测试项目 测试标准 外观质量 GB/T 29734.2 尺寸 GB/T 2973…

面试经验——面试项目准备工作

摘要 本博文主要是分享个人在面试中对于项目思考&#xff0c;希望帮助大家能够面试中能够很好的介绍和分享自己的项目。在面试官心中留下一个好印象&#xff0c;希望你能拿到自己满意的offer。 一、面试项目常见问题 1.1 工作经历中&#xff0c;最优技术挑战/亮点的事情是什…

Elasticsearch聚合----aggregations的简单使用

文章目录 Getting started1、搜索 address 中包含 mill 的所有人的年龄分布以及平均年龄&#xff0c;但不显示这些人的详情2、size0不展示命中记录&#xff0c;只展示聚合结果3、按照年龄聚合&#xff0c;并且请求这些年龄段的这些人的平均薪资4、查出所有年龄分布&#xff0c;…

【Android Studio】工程中文件Annotate with Git Blame 不能点击

问题描述 工程文件中想要查看代码提交信息但是相关按钮不可点击 解决方法 Android Studio -> Preferences -> Version Control-> 在Unregistered roots里找到你想要的工程文件 点击左上角➕号 然后右下角Apply即可

Python分享之多进程初步 (multiprocessing包)

我们已经见过了使用subprocess包来创建子进程&#xff0c;但这个包有两个很大的局限性&#xff1a;1) 我们总是让subprocess运行外部的程序&#xff0c;而不是运行一个Python脚本内部编写的函数。2) 进程间只通过管道进行文本交流。以上限制了我们将subprocess包应用到更广泛的…