Xiajijian
2026-01-23 1609b77ba4715a7f17030d61d20c8e526861ca1a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package com.ard.sample.utils;
 
import com.ard.oss.model.CredentialsDTO;
import io.minio.credentials.AssumeRoleProvider;
import lombok.extern.slf4j.Slf4j;
import okhttp3.ConnectionPool;
import okhttp3.OkHttpClient;
import org.springframework.stereotype.Component;
 
import java.security.NoSuchAlgorithmException;
import java.security.ProviderException;
import java.util.concurrent.TimeUnit;
 
import static com.ard.sample.config.DockConfiguration.*;
 
/**
 * 获取http的STS凭证的工具类,用于简化STS临时凭证的获取过程。
 */
@Slf4j
@Component
public class MinioStsUtils {
 
    public CredentialsDTO getCredentials() {
        try {
            // 构建适配HTTP协议的OkHttpClient(移除所有SSL相关配置)
            OkHttpClient httpClient = new OkHttpClient.Builder()
                    // 保留原代码的连接池配置(禁用连接复用,如需复用可删除此行)
                    .connectionPool(new ConnectionPool(0, 1, TimeUnit.SECONDS))
                    .build();
 
            // 构建 AssumeRoleProvider,使用简化后的HTTP客户端
            AssumeRoleProvider provider = new AssumeRoleProvider(
                    endpoint,          // 注意:此处endpoint需改为HTTP地址(如 http://sts.example.com)
                    accessKey,         // 主账号AccessKey
                    secretKey,         // 主账号SecretKey
                    Math.toIntExact(expire), // 凭证过期时间(转int)
                    null,              // 可选参数(如RoleArn,根据实际SDK调整)
                    region, // 地域(如cn-hangzhou)
                    null, null, null,  // 其他可选参数
                    httpClient         // 传入HTTP版本的OkHttpClient
            );
 
            // 获取 STS 临时凭证
            var stsCredentials = provider.fetch();
            return new CredentialsDTO(stsCredentials, expire);
 
        } catch (ProviderException e) {
            log.error("Failed to fetch/parse STS response (HTTP access)", e);
            throw new RuntimeException("STS response parse failed", e);
        } catch (NoSuchAlgorithmException e) {
            log.error("Failed to fetch/parse STS response (HTTP access)", e);
            throw new RuntimeException(e);
        }
    }
}