gpt4 book ai didi

ssl - 证书异常 : No name matching localhost found using OAuth2RestTemplate NOT RestTemplate

转载 作者:太空宇宙 更新时间:2023-11-03 13:22:16 25 4
gpt4 key购买 nike

我正在尝试使用 OAuth2RestTemplate 访问自签名证书以通过 HTTPS 检索 token ,但我仍然收到此错误:

javax.net.ssl.SSLHandshakeException: java.security.cert.CertificateException: No name matching localhost found

这是我抛出错误的单元测试:

    public class OAuth2ClientTest {

private static final Logger logger = LoggerFactory
.getLogger(SecurityConfig.class);

@Value("${oauth.resource:https://localhost:8443}")
private String baseUrl;

@Value("${oauth.token:https://localhost:8443/oauth/token}")
private String tokenUrl;

@Value("${oauth.resource.id:microservice-test}")
private String resourceId;

@Value("${oauth.resource.client.id:client1}")
private String resourceClientId;

@Value("${oauth.resource.client.secret:changit}")
private String resourceClientSecret;



@Test
public void execute_post_to_tokenUrl()
throws ClientProtocolException, IOException {

OAuth2RestTemplate template = template();

ResponseEntity<String> response = template.exchange(
tokenUrl,
HttpMethod.POST,
null,
String.class);

assertThat(response.getStatusCode().value(), equalTo(200));
}

private OAuth2RestTemplate template(){
ResourceOwnerPasswordResourceDetails resource = new ResourceOwnerPasswordResourceDetails();
resource.setAccessTokenUri(tokenUrl);
resource.setId(resourceId);
resource.setClientId(resourceClientId);
resource.setClientSecret(resourceClientSecret);

resource.setGrantType("password");

resource.setScope(Arrays.asList("openid"));

resource.setUsername("user1@example.com");
resource.setPassword("user1");

OAuth2RestTemplate template = new OAuth2RestTemplate(resource);

ClientHttpRequestFactory factory = template.getRequestFactory();

template.setRequestFactory(requestFactory());
return template;
}

private HttpComponentsClientHttpRequestFactory requestFactory(){

CloseableHttpClient httpClient
= HttpClients.custom()
.setSSLHostnameVerifier(new NoopHostnameVerifier())
.build();
HttpComponentsClientHttpRequestFactory requestFactory
= new HttpComponentsClientHttpRequestFactory();
requestFactory.setHttpClient(httpClient);

return requestFactory;
}
}

当我使用非 HTTPS 连接时,OAuth2 代码工作正常。只是不使用 HTTPS

最佳答案

如果您希望 TLS 正常工作,您必须使用别名“localhost”创建证书。
另一种可能性是将其静态设置或在您的测试配置中设置:

final HostnameVerifier defaultHostnameVerifier = javax.net.ssl.HttpsURLConnection.getDefaultHostnameVerifier ();
final HostnameVerifier localhostAcceptedHostnameVerifier = new javax.net.ssl.HostnameVerifier () {

public boolean verify ( String hostname, javax.net.ssl.SSLSession sslSession ) {
if ( hostname.equals ( "localhost" ) ) {
return true;
}
return defaultHostnameVerifier.verify ( hostname, sslSession );
}
};
javax.net.ssl.HttpsURLConnection.setDefaultHostnameVerifier ( localhostAcceptedHostnameVerifier );

...

关于ssl - 证书异常 : No name matching localhost found using OAuth2RestTemplate NOT RestTemplate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47076697/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com