【发布时间】:2019-08-22 12:13:59
【问题描述】:
我正在尝试使用来自第 3 方服务器的 API。第 3 方向我发送了一个名为 certificate.p12 的 SSL 证书,这是我用来进行握手的证书文件。我使用 SSL 创建了一个自定义 RestTemplate,如下所示:
@Configuration
public class CustomRestTemplate {
private static final String PASSWORD = "fake_password";
private static final String RESOURCE_PATH = "keystore/certificate.p12";
private static final String KEY_TYPE = "PKCS12";
@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) throws Exception {
char[] password = PASSWORD.toCharArray();
SSLContext sslContext = SSLContextBuilder
.create()
// .loadKeyMaterial(keyStore(RESOURCE_PATH, password), password)
.loadKeyMaterial(keyStore(getClass().getClassLoader().getResource(RESOURCE_PATH).getFile(), password), password)
.loadTrustMaterial(null, new TrustSelfSignedStrategy())
.build();
HttpClient client = HttpClients
.custom()
.setSSLContext(sslContext)
.build();
return builder
.requestFactory(() -> new HttpComponentsClientHttpRequestFactory(client))
.build();
}
private KeyStore keyStore(String file, char[] password) throws Exception {
KeyStore keyStore = KeyStore.getInstance(KEY_TYPE);
File key = ResourceUtils.getFile(file);
try (InputStream in = new FileInputStream(key)) {
keyStore.load(in, password);
}
return keyStore;
}
}
然后我使用以下代码调用端点:
@Component
@Service
public class TransactionService implements TransactionInterface {
@Autowired
private CustomRestTemplate restTemplate = new CustomRestTemplate();
private static final String BASE_URL = "https://41.x.x.x:xxxx/";
@Override
public List<Transaction> getUnsentTransactions(int connectionId) throws Exception {
HttpEntity<?> httpEntity = new HttpEntity<>(null, new HttpHeaders());
ResponseEntity<Transaction[]> resp = restTemplate
.restTemplate(new RestTemplateBuilder())
.exchange(BASE_URL + "path/end_point/" + connectionId, HttpMethod.GET, httpEntity, Transaction[].class);
return Arrays.asList(resp.getBody());
}
}
我在尝试使用 api 时得到以下堆栈跟踪:
org.springframework.web.client.ResourceAccessException: I/O error on GET request for \"https://41.x.x.x:xxxx/path/endpoint/parameters\": Certificate for <41.x.x.x> doesn't match any of the subject alternative names: [some_name_here, some_name_here];
我对 TLS 或 SSL 证书没有太多经验。我现在真的被困住了,希望我能在这里得到一些帮助。第 3 方为我提供了一个测试站点,我可以在其中测试端点,将 certificate.p12 文件导入浏览器后,我可以使用他们的测试站点访问端点,但我的 Springboot 应用程序仍然无法到达端点。
我需要将证书复制到特定文件夹吗?情况似乎并非如此,因为如果我更改路径或文件名,我会收到 FileNotFoundException,如果我为 certificate.p12 文件输入错误的密码,我会收到密码不正确的错误。我尝试使用 Postman 对其进行测试,但 Postman 返回的堆栈跟踪与我的 Web 应用程序相同。
看了上面的信息,我是不是遗漏了什么?运行时是否未创建密钥库?我是否需要将证书绑定到 JVM 或我的传出请求?
任何帮助将不胜感激。
谢谢
【问题讨论】:
-
似乎 41.x.x.x 未列出您的 SSL 证书的 SAN 条目。当您通过浏览器访问您的应用程序时,请检查您的证书 SAN 条目。在浏览器地址栏中,单击锁定图标 -> 证书 -> 详细信息 -> 检查主题备用名称
-
@Haran 当我在浏览器的详细信息下查看证书时,它只有一个 CN,根本没有列出 SAN
标签: java api spring-boot ssl sslhandshakeexception