zhaowenyu
2026-02-11 d301cbce7acb33e6752cd11b1eca058b7c57c827
2026-02-04 优化minio中转接口
1 files modified
214 ■■■■■ changed files
ard-admin/src/main/java/com/ard/web/controller/sample/media/MinioStsProxyController.java 214 ●●●●● patch | view | raw | blame | history
ard-admin/src/main/java/com/ard/web/controller/sample/media/MinioStsProxyController.java
@@ -18,7 +18,12 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.concurrent.TimeUnit;
@@ -28,79 +33,174 @@
@RequestMapping("/minioTransfer")
public class MinioStsProxyController {
    // ==================== 常量配置(适配大文件) ====================
    // ==================== 常量配置 ====================
    private static final String PROXY_PREFIX = "/minioTransfer";
    private static final int BUFFER_SIZE = 65536; // 64K缓冲区,提升大文件传输效率
    private static final long MAX_MEMORY_SIZE = 1024 * 1024 * 100; // 100MB,超过则用流式
    private static final int BUFFER_SIZE = 65536;
    private static final long MAX_MEMORY_SIZE = 1024 * 1024 * 100; // 100MB
    private static final String X_AMZ_SECURITY_TOKEN = "X-Amz-Security-Token";
    private static final String TRANSFER_ENCODING = "Transfer-Encoding";
    private static final String CONTENT_LENGTH = "Content-Length";
    private static final String HOST = "Host";
    // 配置文件注入(大幅提升超时时间)
    // 配置注入
    @Value("${oss.endpoint}")
    private String minioEndpoint;
    private static final Integer connectTimeout=30;
    private static final Integer readWriteTimeout=1800;
    private static final Integer connectTimeout = 30;
    private static final Integer readWriteTimeout = 1800;
    @Autowired
    private OssServiceContext ossService;
    // OkHttp 单例客户端(全局复用)
    // OkHttp客户端(全局)
    private static OkHttpClient httpClient;
    // 静态初始化,强制TLS 1.2,修复SSL兼容问题
    // 静态初始化:全局禁用SSL校验 + 强制兼容加密套件
    static {
        // 第一步:全局禁用SSL证书校验(终极兜底)
        disableSslVerification();
        // 第二步:创建兼容MinIO的OkHttp客户端
        createOkHttpClient();
    }
    /**
     * 重构OkHttp客户端:强制TLS 1.2 + 超长超时 + 连接保活
     * 全局禁用SSL证书校验(绕过所有SSL检查,仅内网/测试环境使用)
     */
    private static void disableSslVerification() {
        try {
            // 获取信任所有证书的TrustManager
            TrustManager[] trustAllCerts = new TrustManager[]{
                    new X509TrustManager() {
                        public X509Certificate[] getAcceptedIssuers() { return null; }
                        public void checkClientTrusted(X509Certificate[] certs, String authType) {}
                        public void checkServerTrusted(X509Certificate[] certs, String authType) {}
                    }
            };
            // 获取SSL上下文,强制TLS 1.2
            SSLContext sslContext = SSLContext.getInstance("TLSv1.2");
            sslContext.init(null, trustAllCerts, new SecureRandom());
            HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
            // 禁用主机名验证
            HostnameVerifier allHostsValid = (hostname, session) -> true;
            HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
            log.info("全局SSL证书校验已禁用(仅测试环境)");
        } catch (NoSuchAlgorithmException | KeyManagementException e) {
            log.error("全局禁用SSL校验失败", e);
        }
    }
    /**
     * 创建兼容MinIO的OkHttp客户端(强制加密套件 + TCP优化)
     */
    private static void createOkHttpClient() {
        try {
            // 1. 强制使用TLS 1.2,解决SSL版本不兼容导致的连接重置
            // 1. 初始化SSL上下文(强制TLS 1.2 + 信任所有证书)
            SSLContext sslContext = SSLContext.getInstance("TLSv1.2");
            TrustManager[] trustAllCerts = new TrustManager[]{
                    new X509TrustManager() {
                        @Override
                        public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) {}
                        public void checkClientTrusted(X509Certificate[] certs, String authType) {}
                        @Override
                        public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) {}
                        public void checkServerTrusted(X509Certificate[] certs, String authType) {}
                        @Override
                        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                            return new java.security.cert.X509Certificate[0];
                        }
                        public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[0]; }
                    }
            };
            sslContext.init(null, trustAllCerts, new SecureRandom());
            SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
            // 2. 配置连接工厂,强制TLS 1.2
            ConnectionSpec connectionSpec = new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS)
                    .tlsVersions(TlsVersion.TLS_1_2)
                    .build();
            // 2. 配置MinIO兼容的加密套件(关键!解决握手后断开)
            String[] compatibleCiphers = {
                    "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256",
                    "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384",
                    "TLS_RSA_WITH_AES_128_GCM_SHA256",
                    "TLS_RSA_WITH_AES_256_GCM_SHA384"
            };
            // 3. 构建客户端,增加连接保活和重试
            // 3. 自定义SSLSocketFactory,强制加密套件
            SSLSocketFactory customSslSocketFactory = new SSLSocketFactory() {
                @Override
                public String[] getDefaultCipherSuites() {
                    return compatibleCiphers;
                }
                @Override
                public String[] getSupportedCipherSuites() {
                    return compatibleCiphers;
                }
                @Override
                public Socket createSocket(Socket s, String host, int port, boolean autoClose) throws IOException {
                    SSLSocket sslSocket = (SSLSocket) sslSocketFactory.createSocket(s, host, port, autoClose);
                    sslSocket.setEnabledCipherSuites(compatibleCiphers);
                    sslSocket.setEnabledProtocols(new String[]{"TLSv1.2"});
                    return sslSocket;
                }
                @Override
                public Socket createSocket(String host, int port) throws IOException {
                    SSLSocket sslSocket = (SSLSocket) sslSocketFactory.createSocket(host, port);
                    sslSocket.setEnabledCipherSuites(compatibleCiphers);
                    sslSocket.setEnabledProtocols(new String[]{"TLSv1.2"});
                    return sslSocket;
                }
                @Override
                public Socket createSocket(String host, int port, java.net.InetAddress localHost, int localPort) throws IOException {
                    SSLSocket sslSocket = (SSLSocket) sslSocketFactory.createSocket(host, port, localHost, localPort);
                    sslSocket.setEnabledCipherSuites(compatibleCiphers);
                    sslSocket.setEnabledProtocols(new String[]{"TLSv1.2"});
                    return sslSocket;
                }
                @Override
                public Socket createSocket(java.net.InetAddress host, int port) throws IOException {
                    SSLSocket sslSocket = (SSLSocket) sslSocketFactory.createSocket(host, port);
                    sslSocket.setEnabledCipherSuites(compatibleCiphers);
                    sslSocket.setEnabledProtocols(new String[]{"TLSv1.2"});
                    return sslSocket;
                }
                @Override
                public Socket createSocket(java.net.InetAddress address, int port, java.net.InetAddress localAddress, int localPort) throws IOException {
                    SSLSocket sslSocket = (SSLSocket) sslSocketFactory.createSocket(address, port, localAddress, localPort);
                    sslSocket.setEnabledCipherSuites(compatibleCiphers);
                    sslSocket.setEnabledProtocols(new String[]{"TLSv1.2"});
                    return sslSocket;
                }
            };
            // 4. 构建OkHttp客户端(简化TCP优化,避免SocketFactory编译错误)
            httpClient = new OkHttpClient.Builder()
                    .sslSocketFactory(sslSocketFactory, (X509TrustManager) trustAllCerts[0])
                    .sslSocketFactory(customSslSocketFactory, (X509TrustManager) trustAllCerts[0])
                    .hostnameVerifier((hostname, session) -> true)
                    .connectionSpecs(java.util.Collections.singletonList(connectionSpec)) // 强制TLS 1.2
                    // 强制TLS 1.2和兼容加密套件
                    .connectionSpecs(Arrays.asList(
                            new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS)
                                    .tlsVersions(TlsVersion.TLS_1_2)
                                    .cipherSuites(compatibleCiphers)
                                    .build()
                    ))
                    // TCP优化:禁用Nagle算法(通过JVM参数或OkHttp的callTimeout间接优化)
                    .callTimeout(30, TimeUnit.SECONDS) // 调用总超时
                    .connectTimeout(connectTimeout, TimeUnit.SECONDS)
                    .readTimeout(readWriteTimeout, TimeUnit.SECONDS)
                    .writeTimeout(readWriteTimeout, TimeUnit.SECONDS)
                    .connectionPool(new ConnectionPool(20, 10, TimeUnit.MINUTES)) // 更大的连接池
                    .retryOnConnectionFailure(true) // 开启连接失败重试
                    .pingInterval(30, TimeUnit.SECONDS) // TCP保活,防止连接被断开
                    .connectionPool(new ConnectionPool(20, 10, TimeUnit.MINUTES))
                    .retryOnConnectionFailure(true)
                    .pingInterval(10, TimeUnit.SECONDS) // 缩短保活间隔
                    .build();
            log.info("OkHttp客户端初始化完成(兼容MinIO TLS 1.2)");
        } catch (Exception e) {
            log.error("OkHttp初始化失败,使用默认客户端", e);
            httpClient = new OkHttpClient();
        }
    }
    // ========== 以下方法与之前一致,无需修改 ==========
    @ApiOperation("MinIO STS临时凭证通用代理")
    @RequestMapping("/**")
    public void proxyRequest(HttpServletRequest request, HttpServletResponse response) {
@@ -110,14 +210,12 @@
        log.info("STS代理请求开始 | 方法:{} | URI:{} | 参数:{} | 内容长度:{}",
                method, requestUri, queryString, request.getContentLengthLong());
        // 构造目标地址
        String targetUrl = buildTargetUrl(requestUri, queryString);
        if (targetUrl == null) {
            sendError(response, HttpServletResponse.SC_BAD_REQUEST, "请求路径非法");
            return;
        }
        // 获取STS凭证
        CredentialsDTO credentials = getCredentials();
        if (credentials == null) {
            sendError(response, HttpServletResponse.SC_FORBIDDEN, "STS凭证无效");
@@ -125,9 +223,7 @@
        }
        try {
            // 构建请求(区分大/小文件上传方式)
            Request okRequest = buildRequest(request, method, targetUrl, credentials);
            // 执行并转发响应
            forwardResponse(okRequest, response);
            log.info("STS代理请求成功 | 目标地址:{}", targetUrl);
        } catch (Exception e) {
@@ -136,9 +232,6 @@
        }
    }
    /**
     * 构造MinIO请求地址
     */
    private String buildTargetUrl(String requestUri, String queryString) {
        try {
            String targetPath = requestUri.substring(PROXY_PREFIX.length());
@@ -153,9 +246,6 @@
        }
    }
    /**
     * 获取并校验STS临时凭证
     */
    private CredentialsDTO getCredentials() {
        try {
            CredentialsDTO dto = ossService.getCredentials();
@@ -163,7 +253,7 @@
                log.error("STS凭证核心字段缺失");
                return null;
            }
            log.info("STS凭证获取成功 | AK前缀:{}", dto.getAccessKeyId());  //.substring(0, 4) + "***"
            log.info("STS凭证获取成功 | AK前缀:{}", dto.getAccessKeyId().substring(0, 4) + "***");
            return dto;
        } catch (Exception e) {
            log.error("获取STS凭证异常", e);
@@ -171,13 +261,10 @@
        }
    }
    /**
     * 构建OkHttp请求:核心优化大文件上传逻辑
     */
    private Request buildRequest(HttpServletRequest request, String method, String url, CredentialsDTO credentials) throws IOException {
        Request.Builder builder = new Request.Builder().url(url);
        // 透传请求头:仅过滤Host,保留Content-Length
        // 透传请求头
        Enumeration<String> headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String name = headerNames.nextElement();
@@ -187,26 +274,24 @@
            builder.addHeader(name, request.getHeader(name));
        }
        // 强制添加STS安全令牌
        // 添加STS令牌
        builder.addHeader(X_AMZ_SECURITY_TOKEN, credentials.getSecurityToken());
        // 构造请求体:区分GET/HEAD和其他方法
        // 构建请求体
        RequestBody body = null;
        if (!"GET".equalsIgnoreCase(method) && !"HEAD".equalsIgnoreCase(method)) {
            long contentLength = request.getContentLengthLong();
            InputStream inputStream = request.getInputStream();
            // 策略1:小文件(≤100MB)→ 预读取到字节数组,避免流式传输的连接不稳定
            // 小文件预读取
            if (contentLength > 0 && contentLength <= MAX_MEMORY_SIZE) {
                log.info("小文件上传:预读取到内存,长度:{}MB", contentLength / 1024 / 1024);
                log.info("小文件上传:预读取到内存,长度:{:.2f}MB", contentLength / 1024.0 / 1024.0);
                byte[] content = readInputStreamToBytes(inputStream);
                MediaType mediaType = request.getContentType() != null
                        ? MediaType.parse(request.getContentType())
                        : MediaType.parse("application/octet-stream");
                body = RequestBody.create(content, mediaType);
            }
            // 策略2:大文件/未知长度 → 流式上传,优化超时和缓冲区
            else {
            } else {
                log.info("大文件/未知长度上传:使用流式传输");
                body = new RequestBody() {
                    @Override
@@ -217,12 +302,11 @@
                    @Override
                    public long contentLength() {
                        return contentLength; // 返回原始长度,避免chunked编码
                        return contentLength;
                    }
                    @Override
                    public void writeTo(BufferedSink sink) throws IOException {
                        // 优化:增加异常捕获和重试逻辑
                        byte[] buffer = new byte[BUFFER_SIZE];
                        int len;
                        long totalWritten = 0;
@@ -230,17 +314,16 @@
                            while ((len = in.read(buffer)) != -1) {
                                sink.write(buffer, 0, len);
                                totalWritten += len;
                                // 每写100MB刷新一次,防止缓冲区溢出
                                if (totalWritten % (1024 * 1024 * 100) == 0) {
                                    sink.flush();
                                    log.info("已上传:{}MB", totalWritten / 1024 / 1024);
                                    log.info("已上传:{:.2f}MB", totalWritten / 1024.0 / 1024.0);
                                }
                            }
                            sink.flush(); // 最终刷新
                            log.info("流式上传完成,总写入:{}MB", totalWritten / 1024 / 1024);
                            sink.flush();
                            log.info("流式上传完成,总写入:{:.2f}MB", totalWritten / 1024.0 / 1024.0);
                        } catch (IOException e) {
                            log.error("流式上传失败,已写入:{}MB", totalWritten / 1024 / 1024, e);
                            throw e; // 抛出异常让上层处理
                            log.error("流式上传失败,已写入:{:.2f}MB", totalWritten / 1024.0 / 1024.0, e);
                            throw e;
                        }
                    }
                };
@@ -251,9 +334,6 @@
        return builder.build();
    }
    /**
     * 工具方法:读取InputStream到字节数组(小文件专用)
     */
    private byte[] readInputStreamToBytes(InputStream inputStream) throws IOException {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        byte[] buffer = new byte[BUFFER_SIZE];
@@ -267,12 +347,15 @@
        return result;
    }
    /**
     * 执行请求并转发响应:增加响应体处理的容错
     */
    private void forwardResponse(Request request, HttpServletResponse response) throws IOException {
        try (Response okResponse = httpClient.newCall(request).execute()) {
            // 设置响应状态码
            // 新增:确保响应编码和类型正确
            response.setCharacterEncoding("UTF-8");
            String responseContentType = okResponse.header("Content-Type");
            if (responseContentType != null) {
                response.setContentType(responseContentType);
            }
            response.setStatus(okResponse.code());
            // 透传响应头
@@ -284,7 +367,7 @@
                response.setHeader(name, headers.get(name));
            }
            // 透传响应体:增加空判断和流关闭容错
            // 透传响应体
            ResponseBody responseBody = okResponse.body();
            if (responseBody != null) {
                try (InputStream in = responseBody.byteStream()) {
@@ -301,9 +384,6 @@
        }
    }
    /**
     * 统一错误响应
     */
    private void sendError(HttpServletResponse response, int code, String msg) {
        try {
            response.sendError(code, msg);