GET请求给Connection拒绝了

时间:2016-01-10 12:02:18

标签: java ssl ssl-certificate resttemplate wso2-am

我有以下CURL命令,使用-k选项可以正常工作,这意味着:不安全,允许连接到没有证书的SSL站点。

我使用的是<View style={{flexDirection: 'row'}}> <Text> Start here, </Text> <Text> finish here </Text> </View> 工具版WSO2 API Manager

1.9.1

现在我想在Spring中使用RestTemplate实现相同的功能,所以到目前为止我开发了以下代码,并且它给了我一个 Connection refused 错误:

我的代码

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

@Test     public void testGetTokenFromWSo2(){         String url =&#34; https://XXXXXXXXXXX:8243/token&#34;;

@Before
public void testBefore(){
    disableSslVerification();
}

private static void disableSslVerification() {
    try {
        TrustManager[] trustAllCerts = new TrustManager[]{
            new X509TrustManager() {
                @Override
                public X509Certificate[] getAcceptedIssuers() {
                    return null;
                }

                @Override
                public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                }

                @Override
                public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                }
            }
        };

        SSLContext sc = SSLContext.getInstance("SSL");
        sc.init(null, trustAllCerts, new java.security.SecureRandom());
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

        HostnameVerifier allHostsValid = new HostnameVerifier() {
            public boolean verify(String hostname, SSLSession session) {
                return true;
            }
        };

        HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (KeyManagementException e) {
        e.printStackTrace();
    }
}

我看到的错误

    MultiValueMap<String, Object> map = new LinkedMultiValueMap<String, Object>();
    map.add("grant_type", "password");
    map.add("username", "test");
    map.add("password", "test");

    HttpHeaders headers =new HttpHeaders();
    headers.add("Authorization", "Basic XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
    headers.add("Content-Type", "application/x-www-form-urlencoded");

    RestTemplate restTemplate = new RestTemplate();
    HttpEntity<String> entity = new HttpEntity<String>(headers);

    HttpEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, entity, String.class, map);
    System.out.println("RESPONSE : "+response.getBody());
}

1 个答案:

答案 0 :(得分:0)

我能够解决这个错误。我们可以通过SSL protected site方式连接到insecure,即连接到SSL sites without verifying cert。你可以在这里找到解决方案,它可以正常工作java.security.cert.CertificateException: No subject alternative names present;