From db41bf9c945bbd79fbe534e5a9e70d0e2ee83a2c Mon Sep 17 00:00:00 2001 From: ajayveluru Date: Mon, 21 Nov 2022 09:29:27 +0530 Subject: [PATCH] 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 --- .../CustomRestTemplateConfiguration.java | 45 ++++++++++++++++++ .../custom/RestTemplateClientApplication.java | 17 +++++++ .../custom/RestTemplateClientController.java | 20 ++++++++ .../web/https/HttpsEnabledApplication.java | 17 +++++++ .../baeldung/web/https/WelcomeController.java | 12 +++++ .../application-ssl-client.properties | 8 ++++ .../main/resources/application-ssl.properties | 14 ++++++ .../src/main/resources/keystore/baeldung.jks | Bin 0 -> 2187 bytes .../src/main/resources/keystore/baeldung.p12 | Bin 0 -> 2669 bytes 9 files changed, 133 insertions(+) create mode 100644 spring-web-modules/spring-resttemplate-3/src/main/java/com/baeldung/resttemplate/custom/CustomRestTemplateConfiguration.java create mode 100644 spring-web-modules/spring-resttemplate-3/src/main/java/com/baeldung/resttemplate/custom/RestTemplateClientApplication.java create mode 100644 spring-web-modules/spring-resttemplate-3/src/main/java/com/baeldung/resttemplate/custom/RestTemplateClientController.java create mode 100644 spring-web-modules/spring-resttemplate-3/src/main/java/com/baeldung/web/https/HttpsEnabledApplication.java create mode 100644 spring-web-modules/spring-resttemplate-3/src/main/java/com/baeldung/web/https/WelcomeController.java create mode 100644 spring-web-modules/spring-resttemplate-3/src/main/resources/application-ssl-client.properties create mode 100644 spring-web-modules/spring-resttemplate-3/src/main/resources/application-ssl.properties create mode 100644 spring-web-modules/spring-resttemplate-3/src/main/resources/keystore/baeldung.jks create mode 100644 spring-web-modules/spring-resttemplate-3/src/main/resources/keystore/baeldung.p12 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 0000000000000000000000000000000000000000..5500156d827c594d1b66e80b8c31af3845ced77c GIT binary patch literal 2187 zcmb`H={wX58^`B2i)9#OFl3h|Tbi*HB_&H_t&T$D*o`e)$o5!Wlws)}!VWb59_AXG)k~#H+W^zdvJBJVD0AsDcWQ+tHdDoj0GncD(t_Kvz=b z?Kxv9a!w9KSM*x!TP3@+z`3KgUlwOa{=Ty35??QCm0^q9NlKxr_U6mWwH}^C+ncmq zz_~K3Cu&MBnfQ5pId6C}N=F#V)X;oIA3Na-LF=LI5oY()3Ch$me$t5JMX|CXq@mU| zmPT~_9<4ZPje?Usdv`a#yxDWR@DaaF_ zTzD%(3(8s57F?+}4cnn1tScKXm9&nea>-cRCL~OzjD8${ac;i0Qyjma%!9*Tm9BBJ zkhbtNo_4W|?J8MIq*{D0eT$+D(Jx-JQS&T6sPAY02uU^|oSj~VDwGbr^aTLa>1|J~POp>iIikgTef zOP(eXY*@OEw>oo4)vv!TT(d!Ibwgjq+~5vcdBtJ0zG7P7%a49y+olL|bS2n|FG`_y znN9s@mSggy2^=6KyY`9h{z|6wLM%>tB=P08HoYod8}bvir8>s>yjFT>K=On4b#Ilu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1z#wfjEno&a)Zw#-B($K(vhu2rQFg~y3S;; zTbI7`T=2Ws73k;7NK?ECh5#VoFIkK60 zzGU$G5)VG(V2J=of{oTB^LI$f2ch^r<#&<0=Rf0#QtaRgvqsaLzzip eMY+MoND9?4-mU_np1YJ)@}V=#&hd*cv;GSubl6k? literal 0 HcmV?d00001 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 0000000000000000000000000000000000000000..06e184c117cdf6a97f4516e0133e689f5cdb13dd GIT binary patch literal 2669 zcma)+X*d*&7RP7CEOwHmvaeB=8OzurJ1>f{i){3v+Ggu4kE*B66 zBx2Z)gE8zpzcB|K19|pe5~LiAfjs(+3x8KWl;b~Joa{gl5d$&!4bQ>-{%kA+~*LOY# ze38$u^&Ftw*XN&9l@{NlORw0DES0t>7UNYj>KkkNNP(Z5wKJaXiWC*lux&QbGHP>W zI7>>FsgZJ!J?*mIHG0O2yX$^L^2WW)@~GB~dFnu*`JDpgDsRxuQ~J1y%NsMw-?Bpn z5Oecq+JK>G8J+{7$()2=d%dYQJ2+0V z$p*}%P~2&6qs*s?s0SCtSuuY1nUo^+6;Mb7h|6^woqRyLk8cA!_z zOxwR8U{s2xsIKK{gAq$VSK@+n+$%oc?it>Q=X)(Qqw<;lUd*`zsHLKzR6S%jHcD7* zY>_RfR?a2|=LhQ1jj#A18Ijie(BK@Oc+w@xo!EjehBYw%lgAqUVMpI~0|s_@A(iJZ zZhSextrcvu-sLrJsiWmbSa5E?^p*c zGEoUVk8@XlIVLIhD-H53K584f7A%)#LB5V+{h7Zcb%{HFtjR*|!?RUlvb|;NZ3for zW`$$5)BBhWeZGN&h|?M)rmeleC!tNj_*0^DK_YD%=8k4JBqDH`JoL?^%XLwds}llDqv9i_YZaR;zN>;b}~ZNWthNO|fSuF#)#i|Tvn z1eeEzQpqYYXt)3)tuQ})wVwvRv6_&RZT1W(RKhr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