Linux系统下minio设置SSL证书进行HTTPS远程连接访问

文章目录

1.配置SSL证书使用HTTPS访问

生成域名对应的SSL证书,下载Apache版本,我目前只发现Apache这个里面有对应的私钥和证书
私钥重命名为private.key证书重命名为public.crt,不更改为指定格式则会无法被识别。

将公钥和证书放入root/.minio/certs文件夹中,此文件夹安装minio时自动生成的

重新启动minion可以看到启动日志中http变成了https,此时可以使用https://域名.com:9001进行访问了

2.MINIO SDK 忽略证书验证

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

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

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

bash 复制代码
@Configuration
@EnableConfigurationProperties(MinioProperties.class)
public class MinioConfig {
    private static final Logger LOGGER = LoggerFactory.getLogger(MinioConfig.class);

    @Bean
    public MinioClient minioClient(MinioProperties properties){
        properties.check();

        // Create a trust manager that does not validate certificate chains
        TrustManager[] 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 manager
        SSLContext 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 certificates
        OkHttpClient customHttpClient = new OkHttpClient.Builder()
                .sslSocketFactory(sslContext.getSocketFactory(), (X509TrustManager) trustAllCerts[0])
                .hostnameVerifier((hostname, session) -> true)
                .build();

        // Set the custom SSLContext for MinioClient
        return MinioClient.builder()
                .endpoint(properties.getEndpoint())
                .credentials(properties.getAccessKey(), properties.getSecretKey())
                .httpClient(customHttpClient)
                .build();

    }

}

1.在上面的代码中,我们创建了一个SSLContext,其中的X509TrustManager会信任所有证书,无论其是否由信任的证书颁发机构(CA)签署。

2.创建了一个自定义的OkHttpClient,该客户端信任所有证书。然后,我们使用MinioClient.builder()方法,将自定义的OkHttpClient传递给httpClient()方法

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

代码优化

bash 复制代码
@Configuration
@EnableConfigurationProperties(MinioProperties.class)
public class MinioConfig {
    private static final Logger LOGGER = LoggerFactory.getLogger(MinioConfig.class);

    @Bean
    public MinioClient minioClient(MinioProperties properties){
        properties.check();

        OkHttpClient customHttpClient = null;
        if (properties.getEndpoint().startsWith("https://")) {
            // 如果是HTTPS,使用自定义的OkHttpClient处理自签名的HTTPS请求
            customHttpClient = createCustomOkHttpClient();
        }

        MinioClient minioClient;
        if (customHttpClient != null) {
            // 如果使用了自定义的OkHttpClient
            minioClient = MinioClient.builder()
                    .endpoint(properties.getEndpoint())
                    .credentials(properties.getAccessKey(), properties.getSecretKey())
                    .httpClient(customHttpClient)
                    .build();

        } else {
            // 如果是普通HTTP,使用默认的OkHttpClient
            minioClient = 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 chains
        TrustManager[] 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 manager
        SSLContext 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 certificates
        OkHttpClient 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;
    }


}

3.使用受信任的证书

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

报错日志

bash 复制代码
PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

问题原因

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

解决问题

1.使用受信任的证书

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

CA比较新或自行颁发的证书,需要将证书加入到jdk的信任证书库中,

把该证书导入java中的cacerts证书库里

Jdk的安装目录 C:\Program Files\Java\jdk1.8.0\jre\lib\security

执行系统命令:

1、进入安装目录

cd C:\Program Files\Java\jdk1.8.0\jre\lib\security

2、自签证书添加到jdk的信任证书库中

bash 复制代码
keytool -import -alias <alias-name> -file <certificate-file> -keystore <keystore-file> -storepass <password>

在这个命令中:

是您为导入的证书指定的别名。

是包含要导入证书的文件的路径。

是密钥库文件的路径。在Java的默认安装中,这通常是$JAVA_HOME/lib/security/cacerts,但也可以是您指定的任何其他密钥库文件。

是密钥库的密码。对于Java的默认cacerts文件,这通常是changeit,但如果您更改了密码,则需要使用新密码。

bash 复制代码
keytool -import -alias minio -file .\www.oa.cn_public.crt -keystore /home/jdk/jdk-8u251-linux-x64/jdk1.8.0_251/jre/lib/security/cacerts -storepass changeit

2.同2忽略证书验证

相关推荐
大鱼>1 小时前
eBPF内核编程:从TC到XDP的全栈可观测性
linux·服务器·php
sxstj2 小时前
老旧电脑 Linux 系统完整推荐(按内存分档,新手友好)
linux
Lyra_Infra2 小时前
OpenClaw 服务异常故障分析报告
linux·人工智能
冷莫溪4 小时前
Zabbix——认识及部署Zabbix(基于Rocky9.7)
linux·运维·php·zabbix
风曦Kisaki4 小时前
Kubernetes(K8s)笔记Day03: Pod命名空间,标签,Pod 的调度,污点与容忍度,Pod 常见状态和重启策略,Pod 生命周期
linux·运维·笔记·docker·云原生·容器·kubernetes
xuhe24 小时前
一劳永逸!解决 AutoDL 系统盘(30GB)爆满与 pip 缓存迁移
linux·python·ai·jupyter
学无止境_永不停歇5 小时前
9. 缓冲区
linux·服务器·c++
dok125 小时前
Johannes 《Linux内核模块与设备驱动开发:编写Linux驱动程序》(4) devicefile 设备号相关
linux·运维·服务器
Fu2067215 小时前
结课项目考试
linux·运维·服务器
Albert·Lin6 小时前
LVS相关知识点总结-一
linux·服务器·lvs