title: Spring RestTemplate Interceptor not execute request
body: <p>When I configure RestTemplate use HttpClient then my interceptor only execute for first time, in second time it'll hang up when execute, in this block below. There is no exception, I don't know why!
  If I remove httpClient then no problem.
  (My interceptor intention is catch 401 unauthorized status to refresh access token)</p>
  
  <pre><code>if (url.contains("/auth") || url.contains("/custcare-common")) {
      return execution.execute(request, body);
  }
  </code></pre>
  
  <p>Full code below:</p>
  
  <pre><code>public class RestTemplateHeaderInterceptor implements ClientHttpRequestInterceptor {
  
  @Override
  public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution)
          throws IOException {
      String url = request.getURI().getPath();
      if (url.contains("/auth") || url.contains("/custcare-common")) {
          return execution.execute(request, body);
      }
  
      HttpSession session = SessionBean.getSession();
      if (session != null) {
          // check header does not has token then add to header
          if (request.getHeaders().isEmpty() || !request.getHeaders().containsKey(HttpHeaders.AUTHORIZATION)) {
              // add jwt token to header
              Object accessToken = session.getAttribute(AppConstants.Token.ACCESS_TOKEN);
              if (accessToken != null) {
                  request.getHeaders().add(HttpHeaders.AUTHORIZATION, SecurityConstants.TOKEN_PREFIX + accessToken);
              }
          }
  
          // refresh token when expire
          ClientHttpResponse response = execution.execute(request, body);
          if (response.getStatusCode() == HttpStatus.UNAUTHORIZED) {
              Object refreshToken = session.getAttribute(AppConstants.Token.REFRESH_TOKEN);
              if (refreshToken != null) {
                  TokenData newTokenData = getNewToken(refreshToken.toString());
                  if (newTokenData != null) {
                      session.setAttribute(AppConstants.Token.ACCESS_TOKEN, newTokenData.getAccessToken());
                      session.setAttribute(AppConstants.Token.REFRESH_TOKEN, newTokenData.getRefreshToken());
  
                      request.getHeaders().set(HttpHeaders.AUTHORIZATION, SecurityConstants.TOKEN_PREFIX + newTokenData.getAccessToken());
  
                      return execution.execute(request, body);
                  }
              }
          }
      }
  
      return execution.execute(request, body);
  }
  
  private TokenData getNewToken(String refreshToken) {
      TokenData tokenData = null;
  
      RefreshTokenRequest request = new RefreshTokenRequest();
      request.setRefreshToken(refreshToken);
  
      MessagesResponse&lt;TokenData&gt; response = RestUtil.post(RestUtil.getCcApiUrl("cc-api.authen.refresh-token"), request, new ParameterizedTypeReference&lt;MessagesResponse&lt;TokenData&gt;&gt;() {});
  
      if (response != null) {
          tokenData = response.getData();
      }
  
      return tokenData;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>My RestTemplate config:</p>
  
  <pre><code>@Configuration
  public class RestTemplateConfig {
  
      @Bean
      public PoolingHttpClientConnectionManager poolingHttpClientConnectionManager() {
          PoolingHttpClientConnectionManager result = new PoolingHttpClientConnectionManager();
          result.setMaxTotal(20);
          return result;
      }
  
      @Bean
      public RequestConfig requestConfig() {
          RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(60000).setConnectTimeout(60000)
                  .setSocketTimeout(60000).build();
  
          return requestConfig;
      }
  
      @Bean
      public CloseableHttpClient httpClient() throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException {
          TrustStrategy trustStrategy = new TrustStrategy() {
              @Override
              public boolean isTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
                  return true;
              }
          };
          SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, trustStrategy).build();
          SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslContext, new NoopHostnameVerifier());
          CloseableHttpClient result = HttpClientBuilder.create()
                  .setConnectionManager(poolingHttpClientConnectionManager())
                  .setDefaultRequestConfig(requestConfig())
                  .setSSLSocketFactory(csf)
                  .build();
  
          return result;
      }
  
      @Bean
      @Primary
      public RestTemplate restTemplate() throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException {
          HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient());
  
          RestTemplate restTemplate = new RestTemplate(requestFactory);
  
          restTemplate.setErrorHandler(new RestResponseErrorHandler());
  
          List&lt;ClientHttpRequestInterceptor&gt; interceptors = restTemplate.getInterceptors();
          if (interceptors.isEmpty()) {
              interceptors = new ArrayList&lt;&gt;();
          }
          interceptors.add(new RestTemplateHeaderInterceptor());
          restTemplate.setInterceptors(interceptors);
  
          return restTemplate;
      }
  }
  </code></pre>
  
  <p>Finally, I done deal with this issue by setting DefaultMaxPerRoute, but I still confuse about this. :D</p>
  
  <pre><code>@Bean
  public PoolingHttpClientConnectionManager poolingHttpClientConnectionManager() {
      PoolingHttpClientConnectionManager result = new PoolingHttpClientConnectionManager();
      result.setMaxTotal(100);
      result.setDefaultMaxPerRoute(100);
      return result;
  }
  </code></pre>