How to disable SSL certificate checking with Spring RestTemplate?

后端 未结 9 1239
余生分开走
余生分开走 2020-11-29 18:11

I am trying to write an integration test where our test launches an embedded HTTPS server using Simple. I created a self-signed certificate using keytool and am able to acce

9条回答
  •  没有蜡笔的小新
    2020-11-29 19:04

    Here's a solution where security checking is disabled (for example, conversing with the localhost) Also, some of the solutions I've seen now contain deprecated methods and such.

    /**
     * @param configFilePath
     * @param ipAddress
     * @param userId
     * @param password
     * @throws MalformedURLException
     */
    public Upgrade(String aConfigFilePath, String ipAddress, String userId, String password) {
        configFilePath = aConfigFilePath;
        baseUri = "https://" + ipAddress + ":" + PORT + "/";
    
        restTemplate = new RestTemplate(createSecureTransport(userId, password, ipAddress, PORT));
        restTemplate.getMessageConverters().add(new MappingJacksonHttpMessageConverter());
        restTemplate.getMessageConverters().add(new StringHttpMessageConverter());
     }
    
    ClientHttpRequestFactory createSecureTransport(String username,
            String password, String host, int port) {
        HostnameVerifier nullHostnameVerifier = new HostnameVerifier() {
            public boolean verify(String hostname, SSLSession session) {
                return true;
            }
        };
        UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(username, password);
        CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(
                new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM), credentials);
    
        HttpClient client = HttpClientBuilder.create()
                .setSSLHostnameVerifier(nullHostnameVerifier)
                .setSSLContext(createContext())
                .setDefaultCredentialsProvider(credentialsProvider).build();
    
        HttpComponentsClientHttpRequestFactory requestFactory = 
                new HttpComponentsClientHttpRequestFactory(client);
    
        return requestFactory;
    }
    
    private SSLContext createContext() {
        TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                return null;
            }
    
            public void checkClientTrusted(
                    java.security.cert.X509Certificate[] certs, String authType) {
            }
    
            public void checkServerTrusted(
                    java.security.cert.X509Certificate[] certs, String authType) {
            }
        } };
    
        try {
            SSLContext sc = SSLContext.getInstance("SSL");
            sc.init(null, trustAllCerts, null);
            SSLContext.setDefault(sc);
            HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
            HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
                public boolean verify(String hostname, SSLSession session) {
                        return true;
                    }
                });
            return sc;
    
        } catch (Exception e) {
        }
        return null;
    }
    

提交回复
热议问题