引起:java.net.ConnectException:连接被拒绝:连接org.springframework.web.client.ResourceAccessException:POST上的I / O错误

时间:2016-01-06 16:35:39

标签: wso2esb wso2is wso2carbon resttemplate wso2-am

我有以下CURL命令可以正常工作。但是在使用java代码时。我使用的是WSO2 API Manager版本1.9.1

 curl -k -d "grant_type=password&username=test&password=test" -H 
"Authorization: Basic QU01dE04MXFrdzg5ZFNFTjk2Vm0waGgwWnBNYTpzeHcxbko3c2gzdm5NVVlmUDEzVmV1bWtsbTRh, 
Content-Type: application/x-www-form-urlencoded" https://XXXXXX:8243/token

我使用RestTemplate API

开发了以下代码
public class Demo {

    public static void main(String[] args) {
        String url = "https://xxxxxxxx:8243/token";

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.add("Authorization", "Basic QU01dE04MXFrdzg5ZFNFTjk2Vm0waGgwWnBNYTpzeHcxbko3c2gzdm5NVVlmUDEzVmV1bWtsbTRh");
        headers.add("Content-Type", "application/x-www-form-urlencoded");
        headers.add("data", "grant_type=password&username=mahesh&password=mahesh00");

        String json =" {\"data\":\"grant_type=password&username=test&password=test\"}";
        HttpEntity<String> postEntity = new HttpEntity<String>(json, headers);

        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> response =restTemplate.exchange(url, HttpMethod.POST,postEntity,String.class);

        System.out.println("\nSTATUS : "+ response.getStatusCode()+" "+response.getStatusCode().getReasonPhrase());
        System.out.println("Response :"+ response.getBody());
    }
}

当我在下面执行时,我看到以下错误:

Exception in thread "main" org.springframework.web.client.ResourceAccessException: I/O error on POST request for "https://192.168.0.102:8243/token":java.security.cert.CertificateException: No subject alternative names present; nested exception is javax.net.ssl.SSLHandshakeException: java.security.cert.CertificateException: No subject alternative names present
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:580)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:530)
    at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:448)
    at com.java.pushNotifications.Demo.main(Demo.java:25)
Caused by: javax.net.ssl.SSLHandshakeException: java.security.cert.CertificateException: No subject alternative names present
    at sun.security.ssl.Alerts.getSSLException(Alerts.java:192)
    at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1904)
    at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:279)
    at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:273)
    at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1446)
    at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:209)
    at sun.security.ssl.Handshaker.processLoop(Handshaker.java:901)
    at sun.security.ssl.Handshaker.process_record(Handshaker.java:837)
    at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1023)
    at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1332)
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1359)
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1343)
    at sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:563)
    at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:185)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:153)
    at org.springframework.http.client.SimpleBufferingClientHttpRequest.executeInternal(SimpleBufferingClientHttpRequest.java:81)
    at org.springframework.http.client.AbstractBufferingClientHttpRequest.executeInternal(AbstractBufferingClientHttpRequest.java:48)
    at org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:53)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:569)
    ... 3 more
Caused by: java.security.cert.CertificateException: No subject alternative names present
    at sun.security.util.HostnameChecker.matchIP(HostnameChecker.java:142)
    at sun.security.util.HostnameChecker.match(HostnameChecker.java:91)
    at sun.security.ssl.X509TrustManagerImpl.checkIdentity(X509TrustManagerImpl.java:347)
    at sun.security.ssl.X509TrustManagerImpl.checkTrusted(X509TrustManagerImpl.java:203)
    at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:126)
    at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1428)
    ... 17 more

2 个答案:

答案 0 :(得分:1)

您忘记在java代码中指定端口:

https://192.168.0.102:/token

应该是:

https://192.168.0.102:8243/token

你也忘了隐藏IP,就像你使用curl命令一样,但由于它是一个内部IP,没有理由打扰它,它在你自己的局域网之外没有任何意义。所以我也没有掩盖它。

答案 1 :(得分:0)

我正面临类似的问题 -

  

org.springframework.web.client.ResourceAccessException:I / O错误   POST请求   &#34; http://hostname:port_number/projectname/inbound/message&#34;:连接   拒绝:连接嵌套异常是java.net.ConnectException:   连接被拒绝:连接

所以,我在application.yml下使用src/main/resources,我将主机值从服务器ip_address更改为localhost ,以便在我的电脑上使用Swagger进行测试,

gui:
  protocol: http
  host: localhost
  port: 8280

希望通过帮助某人获得任何信息。