diff --git a/spring-web-modules/spring-resttemplate-3/src/main/java/com/baeldung/resttemplate/custom/CustomRestTemplateConfiguration.java b/spring-web-modules/spring-resttemplate-3/src/main/java/com/baeldung/resttemplate/custom/CustomRestTemplateConfiguration.java new file mode 100644 index 0000000000..9383b584c4 --- /dev/null +++ b/spring-web-modules/spring-resttemplate-3/src/main/java/com/baeldung/resttemplate/custom/CustomRestTemplateConfiguration.java @@ -0,0 +1,45 @@ +package com.baeldung.resttemplate.custom; + +import java.io.IOException; +import java.net.MalformedURLException; +import java.security.KeyManagementException; +import java.security.KeyStoreException; +import java.security.NoSuchAlgorithmException; +import java.security.cert.CertificateException; + +import javax.net.ssl.SSLContext; + +import org.apache.http.conn.ssl.SSLConnectionSocketFactory; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.ssl.SSLContextBuilder; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.io.Resource; +import org.springframework.http.client.ClientHttpRequestFactory; +import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; +import org.springframework.web.client.RestTemplate; + +@Configuration +public class CustomRestTemplateConfiguration { + + @Value("${trust.store}") + private Resource trustStore; + + @Value("${trust.store.password}") + private String trustStorePassword; + + @Bean + public RestTemplate restTemplate() throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException, + CertificateException, MalformedURLException, IOException { + + SSLContext sslContext = new SSLContextBuilder() + .loadTrustMaterial(trustStore.getURL(), trustStorePassword.toCharArray()).build(); + SSLConnectionSocketFactory sslConFactory = new SSLConnectionSocketFactory(sslContext); + + CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(sslConFactory).build(); + ClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient); + return new RestTemplate(requestFactory); + } +} diff --git a/spring-web-modules/spring-resttemplate-3/src/main/java/com/baeldung/resttemplate/custom/RestTemplateClientApplication.java b/spring-web-modules/spring-resttemplate-3/src/main/java/com/baeldung/resttemplate/custom/RestTemplateClientApplication.java new file mode 100644 index 0000000000..ed98a7ed54 --- /dev/null +++ b/spring-web-modules/spring-resttemplate-3/src/main/java/com/baeldung/resttemplate/custom/RestTemplateClientApplication.java @@ -0,0 +1,17 @@ +package com.baeldung.resttemplate.custom; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.PropertySource; + +@ComponentScan(basePackages = "com.baeldung.resttemplate.custom") +@PropertySource("classpath:application.properties") +@SpringBootApplication +public class RestTemplateClientApplication { + public static void main(String[] args) { + SpringApplication application = new SpringApplication(RestTemplateClientApplication.class); + application.setAdditionalProfiles("ssl-client"); + application.run(args); + } +} diff --git a/spring-web-modules/spring-resttemplate-3/src/main/java/com/baeldung/resttemplate/custom/RestTemplateClientController.java b/spring-web-modules/spring-resttemplate-3/src/main/java/com/baeldung/resttemplate/custom/RestTemplateClientController.java new file mode 100644 index 0000000000..29a2c0f8e4 --- /dev/null +++ b/spring-web-modules/spring-resttemplate-3/src/main/java/com/baeldung/resttemplate/custom/RestTemplateClientController.java @@ -0,0 +1,20 @@ +package com.baeldung.resttemplate.custom; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.client.RestTemplate; + +@RestController +public class RestTemplateClientController { + private static final String WELCOME_URL = "https://localhost:8443/welcome"; + + @Autowired + private RestTemplate restTemplate; + + @GetMapping("/welcomeclient") + public String greetMessage() { + String response = restTemplate.getForObject(WELCOME_URL, String.class); + return response; + } +} diff --git a/spring-web-modules/spring-resttemplate-3/src/main/java/com/baeldung/web/https/HttpsEnabledApplication.java b/spring-web-modules/spring-resttemplate-3/src/main/java/com/baeldung/web/https/HttpsEnabledApplication.java new file mode 100644 index 0000000000..3ed1fe4263 --- /dev/null +++ b/spring-web-modules/spring-resttemplate-3/src/main/java/com/baeldung/web/https/HttpsEnabledApplication.java @@ -0,0 +1,17 @@ +package com.baeldung.web.https; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.PropertySource; + +@ComponentScan(basePackages = "com.baeldung.web.https") +@PropertySource("classpath:application.properties") +@SpringBootApplication +public class HttpsEnabledApplication { + public static void main(String... args) { + SpringApplication application = new SpringApplication(HttpsEnabledApplication.class); + application.setAdditionalProfiles("ssl"); + application.run(args); + } +} diff --git a/spring-web-modules/spring-resttemplate-3/src/main/java/com/baeldung/web/https/WelcomeController.java b/spring-web-modules/spring-resttemplate-3/src/main/java/com/baeldung/web/https/WelcomeController.java new file mode 100644 index 0000000000..f1f6b8f9b2 --- /dev/null +++ b/spring-web-modules/spring-resttemplate-3/src/main/java/com/baeldung/web/https/WelcomeController.java @@ -0,0 +1,12 @@ +package com.baeldung.web.https; + +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class WelcomeController { + @GetMapping(value = "/welcome") + public String welcome() { + return "Welcome To Secured REST Service"; + } +} diff --git a/spring-web-modules/spring-resttemplate-3/src/main/resources/application-ssl-client.properties b/spring-web-modules/spring-resttemplate-3/src/main/resources/application-ssl-client.properties new file mode 100644 index 0000000000..47c058c230 --- /dev/null +++ b/spring-web-modules/spring-resttemplate-3/src/main/resources/application-ssl-client.properties @@ -0,0 +1,8 @@ +server.port=8080 + +server.servlet.context-path=/ + +#trust store location +trust.store=classpath:keystore/baeldung.p12 +#trust store password +trust.store.password=password \ No newline at end of file diff --git a/spring-web-modules/spring-resttemplate-3/src/main/resources/application-ssl.properties b/spring-web-modules/spring-resttemplate-3/src/main/resources/application-ssl.properties new file mode 100644 index 0000000000..a606ab6c60 --- /dev/null +++ b/spring-web-modules/spring-resttemplate-3/src/main/resources/application-ssl.properties @@ -0,0 +1,14 @@ +http.port=8080 + +server.port=8443 + +server.servlet.context-path=/ + +# The format used for the keystore +server.ssl.key-store-type=PKCS12 +# The path to the keystore containing the certificate +server.ssl.key-store=classpath:keystore/baeldung.p12 +# The password used to generate the certificate +server.ssl.key-store-password=password +# The alias mapped to the certificate +server.ssl.key-alias=baeldung \ No newline at end of file diff --git a/spring-web-modules/spring-resttemplate-3/src/main/resources/keystore/baeldung.jks b/spring-web-modules/spring-resttemplate-3/src/main/resources/keystore/baeldung.jks new file mode 100644 index 0000000000..5500156d82 Binary files /dev/null and b/spring-web-modules/spring-resttemplate-3/src/main/resources/keystore/baeldung.jks differ diff --git a/spring-web-modules/spring-resttemplate-3/src/main/resources/keystore/baeldung.p12 b/spring-web-modules/spring-resttemplate-3/src/main/resources/keystore/baeldung.p12 new file mode 100644 index 0000000000..06e184c117 Binary files /dev/null and b/spring-web-modules/spring-resttemplate-3/src/main/resources/keystore/baeldung.p12 differ