2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2018 European Software Marketing Ltd.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
20 package org.onap.aaf.fproxy;
22 import java.io.IOException;
23 import java.security.GeneralSecurityException;
24 import javax.net.ssl.SSLContext;
25 import org.apache.http.conn.ssl.NoopHostnameVerifier;
26 import org.apache.http.impl.client.HttpClientBuilder;
27 import org.apache.http.impl.client.HttpClients;
28 import org.apache.http.ssl.SSLContextBuilder;
29 import org.springframework.beans.factory.annotation.Value;
30 import org.springframework.boot.web.client.RestTemplateBuilder;
31 import org.springframework.context.annotation.Bean;
32 import org.springframework.context.annotation.Configuration;
33 import org.springframework.context.annotation.Profile;
34 import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
35 import org.springframework.util.ResourceUtils;
36 import org.springframework.web.client.RestTemplate;
39 public class RestTemplateConfig {
41 @Value("${server.ssl.client-cert}")
42 private String clientCertPath;
44 @Value("${server.ssl.key-store-password}")
45 private String clientCertPassword;
49 public RestTemplate restTemplate(RestTemplateBuilder builder) throws GeneralSecurityException, IOException {
50 return new RestTemplate(new HttpComponentsClientHttpRequestFactory(getClientBuilder().build()));
53 @Profile("noHostVerification")
55 public RestTemplate restTemplateNoHostVerification(RestTemplateBuilder builder)
56 throws GeneralSecurityException, IOException {
57 return new RestTemplate(new HttpComponentsClientHttpRequestFactory(
58 getClientBuilder().setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE).build()));
61 private HttpClientBuilder getClientBuilder() throws GeneralSecurityException, IOException {
63 SSLContext sslContext = SSLContextBuilder.create()
64 .loadKeyMaterial(ResourceUtils.getFile(clientCertPath), clientCertPassword.toCharArray(),
65 clientCertPassword.toCharArray())
66 .loadTrustMaterial(ResourceUtils.getFile(clientCertPath), clientCertPassword.toCharArray()).build();
68 return HttpClients.custom().setSSLContext(sslContext);