package com.ard.common.config;
|
|
import io.minio.MinioClient;
|
import okhttp3.OkHttpClient;
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
import org.springframework.context.annotation.Bean;
|
import org.springframework.stereotype.Component;
|
|
import javax.net.ssl.SSLContext;
|
import javax.net.ssl.TrustManager;
|
import javax.net.ssl.X509TrustManager;
|
import java.security.KeyManagementException;
|
import java.security.NoSuchAlgorithmException;
|
import java.security.cert.X509Certificate;
|
|
/**
|
* @author sean
|
* @version 0.2
|
* @date 2021/12/9
|
*/
|
@Component
|
@ConfigurationProperties(prefix = "oss")
|
public class OssConfiguration {
|
|
/**
|
* @see com.ard.sample.component.oss.model.enums.OssTypeEnum
|
*/
|
public static String provider;
|
|
/**
|
* Whether to use the object storage service.
|
*/
|
public static boolean enable;
|
|
/**
|
* The protocol needs to be included at the beginning of the address.
|
*/
|
public static String endpoint;
|
|
public static String accessKey;
|
|
public static String secretKey;
|
|
public static String region;
|
|
public static Long expire;
|
|
public static String roleSessionName;
|
|
public static String roleArn;
|
|
public static String bucket;
|
|
public static String objectDirPrefix;
|
|
public void setProvider(String provider) {
|
OssConfiguration.provider = provider;
|
}
|
|
public void setEnable(boolean enable) {
|
OssConfiguration.enable = enable;
|
}
|
|
public void setEndpoint(String endpoint) {
|
OssConfiguration.endpoint = endpoint;
|
}
|
|
public void setAccessKey(String accessKey) {
|
OssConfiguration.accessKey = accessKey;
|
}
|
|
public void setSecretKey(String secretKey) {
|
OssConfiguration.secretKey = secretKey;
|
}
|
|
public void setRegion(String region) {
|
OssConfiguration.region = region;
|
}
|
|
public void setExpire(Long expire) {
|
OssConfiguration.expire = expire;
|
}
|
|
public void setRoleSessionName(String roleSessionName) {
|
OssConfiguration.roleSessionName = roleSessionName;
|
}
|
|
public void setRoleArn(String roleArn) {
|
OssConfiguration.roleArn = roleArn;
|
}
|
|
public void setBucket(String bucket) {
|
OssConfiguration.bucket = bucket;
|
}
|
|
public void setObjectDirPrefix(String objectDirPrefix) {
|
OssConfiguration.objectDirPrefix = objectDirPrefix;
|
}
|
|
|
@Bean
|
public MinioClient getMinioClient() throws KeyManagementException, NoSuchAlgorithmException {// 忽略 SSL 证书校验(仅开发环境)
|
// 忽略 SSL 验证
|
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[0]; }
|
}
|
};
|
|
SSLContext sslContext = SSLContext.getInstance("TLS");
|
sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
|
|
OkHttpClient okHttpClient = new OkHttpClient.Builder()
|
.sslSocketFactory(sslContext.getSocketFactory(), (X509TrustManager)trustAllCerts[0])
|
.hostnameVerifier((hostname, session) -> true)
|
.build();
|
|
return MinioClient.builder()
|
.endpoint(endpoint)
|
.credentials(accessKey, secretKey)
|
.httpClient(okHttpClient)
|
.build();
|
}
|
}
|