Feature rt 3 (#12914)

* Clone Article Initial Version

* Customized RestTemplate to access HTTPS Rest Service

* Customized RestTemplate to access HTTPS Rest Service

* removed unused files

* removed unused files

* removed unused changes

* Added Customized RestTemplate

* Formatted java components

* Create application-ssl.properties

added app props ssl

* Secured REST Service and Customized Rest Template

Secured REST Service and Customized Rest Template

* Delete CustomRestTemplateConfiguration.java

* Delete RestTemplateClientApplication.java

* Delete RestTemplateClientController.java

* Delete application-ssl-client.properties

* Format changes

Format changes

* Update CustomRestTemplateConfiguration.java

* Update RestTemplateClientApplication.java

* Update RestTemplateClientController.java

* Update HttpsEnabledApplication.java

* Update WelcomeController.java

* Update HttpsEnabledApplication.java

* Update CustomRestTemplateConfiguration.java

* Update RestTemplateClientController.java

* Update RestTemplateClientApplication.java

Co-authored-by: PADMAJA <PADMAJA@192.168.100.7>
This commit is contained in:
ajayveluru 2022-11-21 09:29:27 +05:30 committed by GitHub
parent 0107dc4d11
commit 74d6c30d3c
9 changed files with 133 additions and 0 deletions

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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;
}
}

View File

@ -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);
}
}

View File

@ -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";
}
}

View File

@ -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

View File

@ -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