From b9461fd1ec44382c27ea35c66970d5ba14b64056 Mon Sep 17 00:00:00 2001
From: liusuyi <1951119284@qq.com>
Date: Mon, 13 Oct 2025 14:23:12 +0800
Subject: [PATCH] 增加存储模块

---
 ard-oss/src/main/java/com/ard/oss/service/impl/MinIOServiceImpl.java |   73 ++++++++++++++++++++++++++----------
 1 files changed, 53 insertions(+), 20 deletions(-)

diff --git a/ard-sample/src/main/java/com/ard/sample/component/oss/service/impl/MinIOServiceImpl.java b/ard-oss/src/main/java/com/ard/oss/service/impl/MinIOServiceImpl.java
similarity index 60%
rename from ard-sample/src/main/java/com/ard/sample/component/oss/service/impl/MinIOServiceImpl.java
rename to ard-oss/src/main/java/com/ard/oss/service/impl/MinIOServiceImpl.java
index 901ef70..758b8b3 100644
--- a/ard-sample/src/main/java/com/ard/sample/component/oss/service/impl/MinIOServiceImpl.java
+++ b/ard-oss/src/main/java/com/ard/oss/service/impl/MinIOServiceImpl.java
@@ -1,23 +1,32 @@
-package com.ard.sample.component.oss.service.impl;
+package com.ard.oss.service.impl;
 
 import com.ard.common.config.OssConfiguration;
-import com.ard.sample.component.oss.model.enums.OssTypeEnum;
-import com.ard.sample.component.oss.service.IOssService;
-import com.ard.sample.media.model.CredentialsDTO;
+import com.ard.oss.model.CredentialsDTO;
+import com.ard.oss.model.enums.OssTypeEnum;
+import com.ard.oss.service.IOssService;
 import io.minio.*;
 import io.minio.credentials.AssumeRoleProvider;
 import io.minio.errors.*;
 import io.minio.http.Method;
 import lombok.extern.slf4j.Slf4j;
+import okhttp3.OkHttpClient;
 import org.springframework.stereotype.Service;
 
+import javax.net.ssl.SSLContext;
+import javax.net.ssl.SSLSocketFactory;
+import javax.net.ssl.TrustManager;
+import javax.net.ssl.X509TrustManager;
 import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.net.URL;
 import java.security.InvalidKeyException;
 import java.security.NoSuchAlgorithmException;
+import java.security.SecureRandom;
+import java.security.cert.X509Certificate;
 import java.util.Objects;
+
+import static com.ard.common.config.OssConfiguration.*;
 
 /**
  * @author sean
@@ -29,7 +38,7 @@
 public class MinIOServiceImpl implements IOssService {
 
     private MinioClient client;
-    
+
     @Override
     public String getOssType() {
         return OssTypeEnum.MINIO.getType();
@@ -38,8 +47,8 @@
     @Override
     public CredentialsDTO getCredentials() {
         try {
-            AssumeRoleProvider provider = new AssumeRoleProvider(OssConfiguration.endpoint, OssConfiguration.accessKey,
-                    OssConfiguration.secretKey, Math.toIntExact(OssConfiguration.expire),
+            AssumeRoleProvider provider = new AssumeRoleProvider(endpoint, accessKey,
+                    secretKey, Math.toIntExact(OssConfiguration.expire),
                     null, OssConfiguration.region, null, null, null, null);
             return new CredentialsDTO(provider.fetch(), OssConfiguration.expire);
         } catch (NoSuchAlgorithmException e) {
@@ -54,15 +63,15 @@
         try {
             return new URL(
                     client.getPresignedObjectUrl(
-                                    GetPresignedObjectUrlArgs.builder()
-                                            .method(Method.GET)
-                                            .bucket(bucket)
-                                            .object(objectKey)
-                                            .expiry(Math.toIntExact(OssConfiguration.expire))
-                                            .build()));
+                            GetPresignedObjectUrlArgs.builder()
+                                    .method(Method.GET)
+                                    .bucket(bucket)
+                                    .object(objectKey)
+                                    .expiry(Math.toIntExact(OssConfiguration.expire))
+                                    .build()));
         } catch (ErrorResponseException | InsufficientDataException | InternalException |
-                InvalidKeyException | InvalidResponseException | IOException |
-                NoSuchAlgorithmException | XmlParserException | ServerException e) {
+                 InvalidKeyException | InvalidResponseException | IOException |
+                 NoSuchAlgorithmException | XmlParserException | ServerException e) {
             throw new RuntimeException("The file does not exist on the OssConfiguration.");
         }
     }
@@ -114,10 +123,34 @@
         if (Objects.nonNull(this.client)) {
             return;
         }
-        this.client = MinioClient.builder()
-                .endpoint(OssConfiguration.endpoint)
-                .credentials(OssConfiguration.accessKey, OssConfiguration.secretKey)
-                .region(OssConfiguration.region)
-                .build();
+        try {
+            TrustManager[] trustAllCerts = new TrustManager[]{
+                    new X509TrustManager() {
+                        @Override
+                        public void checkClientTrusted(X509Certificate[] chain, String authType) {}
+                        @Override
+                        public void checkServerTrusted(X509Certificate[] chain, String authType) {}
+                        @Override
+                        public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[]{}; }
+                    }
+            };
+
+            SSLContext sslContext = SSLContext.getInstance("TLS");
+            sslContext.init(null, trustAllCerts, new SecureRandom());
+            SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
+
+            OkHttpClient httpClient = new OkHttpClient.Builder()
+                    .sslSocketFactory(sslSocketFactory, (X509TrustManager) trustAllCerts[0])
+                    .hostnameVerifier((hostname, session) -> true)
+                    .build();
+
+            this.client = MinioClient.builder()
+                    .endpoint(endpoint)
+                    .credentials(accessKey, secretKey)
+                    .httpClient(httpClient)
+                    .build();
+        } catch (Exception e) {
+            throw new RuntimeException("Failed to create unsafe MinioClient", e);
+        }
     }
 }

--
Gitblit v1.9.3