Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring boot springboot:如何使用多个rest模板?有条件地使用SSL证书验证和不使用SSL证书验证进行调用_Spring Boot_Ssl - Fatal编程技术网

Spring boot springboot:如何使用多个rest模板?有条件地使用SSL证书验证和不使用SSL证书验证进行调用

Spring boot springboot:如何使用多个rest模板?有条件地使用SSL证书验证和不使用SSL证书验证进行调用,spring-boot,ssl,Spring Boot,Ssl,我们有一个spring引导组件,可以调用不同的第三方系统,例如 需要SSL握手的沃达丰 以及其他需要忽略所有证书的证书 仅在没有任何SSL的情况下调用#2时: try (CloseableHttpClient httpclient = HttpClients.createDefault()) { LOG.info("Sending request to url#{}", url); LOG.info("URI {}", url); HttpGe

我们有一个spring引导组件,可以调用不同的第三方系统,例如

  • 需要SSL握手的沃达丰
  • 以及其他需要忽略所有证书的证书
  • 仅在没有任何SSL的情况下调用#2时:

     try (CloseableHttpClient httpclient = HttpClients.createDefault()) {
            LOG.info("Sending request to url#{}", url);
            LOG.info("URI {}", url);
    
            HttpGet h = new HttpGet(url);
            CloseableHttpResponse response = httpclient.execute(h);
    

    我们得到一个错误:

     SimpleAsyncTaskExecutor-1 | exstart [javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target 
        at sun.security.ssl.Alerts.getSSLException(Alerts.java:192) 
        at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1946) 
    
    我们试图实例化一个忽略所有证书的本地RestTemplate,但它在其他地方破坏了@autowiredRESTTemplatebean

    正确的做法是什么

    更多信息:

    尝试了微笑的建议,对于需要SSL的客户端仍然失败

        @Bean
        @Qualifier("RestTemplate")
        public RestTemplate restTemplate() {
            return new RestTemplate();
        }
    
    
        /**
         * This will return a restTemplate object that will ignore all ssl
         * certificate validation
         *
         * @return
         * @throws KeyStoreException
         * @throws NoSuchAlgorithmException
         * @throws KeyManagementException
         */
        @Bean
        @Qualifier("RestTemplateNonSSL")
        public RestTemplate RestTemplateNonSSL() throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException {
            TrustStrategy acceptingTrustStrategy = (X509Certificate[] chain, String authType) -> true;
    
            SSLContext sslContext = org.apache.http.ssl.SSLContexts.custom()
                    .loadTrustMaterial(null, acceptingTrustStrategy)
                    .build();
    
            SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslContext);
            CloseableHttpClient httpClient = HttpClients.custom()
                    .setSSLSocketFactory(csf)
                    .build();
            HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
            requestFactory.setHttpClient(httpClient);
            return new RestTemplate(requestFactory);
        }
    

    restemplate
    保留两个不同的bean,并使用
    @限定符(“表示SSL与否的名称”)
    来区分这两个bean

    例如

    @Bean(name=“restemplatessl”)
    公共RestTemplate restTemplateSSL(){
    //创建并返回特定于SSL的RestTemplate
    }
    @Bean(name=“restemplatenonssl”)
    公共RestTemplate RESTTemplateNonSL(){
    //创建并返回非SSL特定的RestTemplate
    }
    
    用法:

    @Autowired
    @限定符(“RestTemplateSSL”)
    私有RestTemplate restTemplateSSL;
    @自动连线
    @限定符(“RestTemplateNonSL”)
    私有RestTemplate RESTTemplateNonSL;
    
    restemplate
    保留两个不同的bean,并使用
    @限定符(“表示SSL与否的名称”)
    来区分这两个bean

    例如

    @Bean(name=“restemplatessl”)
    公共RestTemplate restTemplateSSL(){
    //创建并返回特定于SSL的RestTemplate
    }
    @Bean(name=“restemplatenonssl”)
    公共RestTemplate RESTTemplateNonSL(){
    //创建并返回非SSL特定的RestTemplate
    }
    
    用法:

    @Autowired
    @限定符(“RestTemplateSSL”)
    私有RestTemplate restTemplateSSL;
    @自动连线
    @限定符(“RestTemplateNonSL”)
    私有RestTemplate RESTTemplateNonSL;
    
    听起来不错,但同样失败。在问题中添加更多信息。请使用您的代码更新问题,其中您正在自动连接和使用resttemplate对象,并且错误日志听起来不错,但同样失败。添加问题的更多信息..请使用您正在自动连接的代码更新问题,并使用resttemplate对象和错误日志