Commit fbc698401cba5cb1fecfef5dc0cf3eeb72f54b8d
Merge branch '20220802' into 'master'
fix: minio接口https无法调用问题 See merge request huang/thingsboard3.3.2!123
Showing
1 changed file
with
43 additions
and
1 deletions
... | ... | @@ -2,6 +2,7 @@ package org.thingsboard.server.common.data.yunteng.core.utils; |
2 | 2 | |
3 | 3 | import io.minio.*; |
4 | 4 | import lombok.extern.slf4j.Slf4j; |
5 | +import okhttp3.OkHttpClient; | |
5 | 6 | import org.apache.commons.lang3.RandomStringUtils; |
6 | 7 | import org.jetbrains.annotations.NotNull; |
7 | 8 | import org.springframework.beans.factory.annotation.Autowired; |
... | ... | @@ -18,10 +19,15 @@ import org.thingsboard.server.common.data.yunteng.core.exception.FileStorageExce |
18 | 19 | import org.thingsboard.server.common.data.yunteng.core.message.ErrorMessage; |
19 | 20 | import org.thingsboard.server.common.data.yunteng.dto.FileUploadResponse; |
20 | 21 | |
22 | +import javax.net.ssl.*; | |
21 | 23 | import javax.servlet.http.HttpServletRequest; |
22 | 24 | import javax.servlet.http.HttpServletResponse; |
23 | 25 | import java.io.InputStream; |
24 | 26 | import java.io.OutputStream; |
27 | +import java.security.KeyManagementException; | |
28 | +import java.security.NoSuchAlgorithmException; | |
29 | +import java.security.SecureRandom; | |
30 | +import java.security.cert.X509Certificate; | |
25 | 31 | import java.util.Objects; |
26 | 32 | |
27 | 33 | @Service |
... | ... | @@ -33,13 +39,15 @@ public class MinioFileStorageService implements FileStorageService { |
33 | 39 | private MinioClient minioClient; |
34 | 40 | |
35 | 41 | @Autowired |
36 | - public MinioFileStorageService(MinioFileStorageProperties fileStorageProperties) { | |
42 | + public MinioFileStorageService(MinioFileStorageProperties fileStorageProperties) | |
43 | + throws KeyManagementException { | |
37 | 44 | this.fileStorageProperties = fileStorageProperties; |
38 | 45 | // 创建客户端对象 |
39 | 46 | this.minioClient = |
40 | 47 | MinioClient.builder() |
41 | 48 | .endpoint(fileStorageProperties.getMinioUrl()) |
42 | 49 | .credentials(fileStorageProperties.getMinioName(), fileStorageProperties.getMinioPass()) |
50 | + .httpClient(getUnsafeOkHttpClient()) | |
43 | 51 | .build(); |
44 | 52 | // 创建储存桶 |
45 | 53 | checkBucket(); |
... | ... | @@ -172,4 +180,38 @@ public class MinioFileStorageService implements FileStorageService { |
172 | 180 | .concat("/") |
173 | 181 | .concat(fileName); |
174 | 182 | } |
183 | + | |
184 | + public static OkHttpClient getUnsafeOkHttpClient() throws KeyManagementException { | |
185 | + try { | |
186 | + final TrustManager[] trustAllCerts = | |
187 | + new TrustManager[] { | |
188 | + new X509TrustManager() { | |
189 | + @Override | |
190 | + public void checkClientTrusted(X509Certificate[] x509Certificates, String s){} | |
191 | + | |
192 | + @Override | |
193 | + public void checkServerTrusted(X509Certificate[] x509Certificates, String s){} | |
194 | + | |
195 | + @Override | |
196 | + public X509Certificate[] getAcceptedIssuers() { | |
197 | + return new X509Certificate[] {}; | |
198 | + } | |
199 | + } | |
200 | + }; | |
201 | + | |
202 | + final SSLContext sslContext = SSLContext.getInstance("SSL"); | |
203 | + sslContext.init(null, trustAllCerts, new SecureRandom()); | |
204 | + final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory(); | |
205 | + OkHttpClient.Builder builder = new OkHttpClient.Builder(); | |
206 | + builder.sslSocketFactory(sslSocketFactory, (X509TrustManager) trustAllCerts[0]); | |
207 | + | |
208 | + builder.hostnameVerifier( | |
209 | + (s, sslSession) -> true); | |
210 | + return builder.build(); | |
211 | + | |
212 | + } catch (NoSuchAlgorithmException e) { | |
213 | + e.printStackTrace(); | |
214 | + } | |
215 | + return null; | |
216 | + } | |
175 | 217 | } | ... | ... |