Tuesday 8 December 2015

How to write Spring Rest Client with SSL

Step-1
create rest-template.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
            http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
            http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">

<bean id="crestTemplate" class="org.springframework.web.client.RestTemplate">
</bean>          
</beans>  

Step-2
Create client side keystore

E:\CERTIFICATE>keytool -genkey -keyalg RSA -alias calbum  -keystore  ckeystore.jks  -validity 360
Enter keystore password:
Re-enter new password:
What is your first and last name?
  [Unknown]:  localhost
What is the name of your organizational unit?
  [Unknown]:  client
What is the name of your organization?
  [Unknown]:  CL
What is the name of your City or Locality?
  [Unknown]:  Delhi
What is the name of your State or Province?
  [Unknown]:  Delhi
What is the two-letter country code for this unit?
  [Unknown]:  IN
Is CN=localhost, OU=client, O=CL, L=Delhi, ST=Delhi, C=IN correct?
  [no]:  yes

Enter key password for <calbum>
        (RETURN if same as keystore password):

Step-3
We need server side certificate  for client side keystore
Exporting the service side certifcate into a file=frog.cer

E:\CERTIFICATE>keytool -export -alias album -file frog.cer -keystore keystore.jks
Enter keystore password:root@123
Certificate stored in file <frog.cer>


Step-4
Importing the server side certificate(frog.cer) into client side keystore

E:\CERTIFICATE>keytool -importcert -noprompt -trustcacerts  -file frog.cer   -keystore ckeystore.jks
Enter keystore password:root@123
Certificate was added to keystore


Step-5
Write Rest client program using RestTemplate to access restful web service running over HTTPS

Client side change in java code

add below line to enable certificate information for client

System.setProperty("https.protocols", "TLSv1");
System.setProperty("javax.net.debug", "ssl");
System.setProperty("javax.net.ssl.trustStore", "E:/CERTIFICATE/ckeystore.jks");
System.setProperty("javax.net.ssl.trustStorePassword", "root@123");


// //////////Write code to access the restful web service//////////// // Write a client code to access the restful web service ApplicationContext applicationContext = new ClassPathXmlApplicationContext( "rest-template.xml"); RestTemplate restTemplate = (RestTemplate) applicationContext .getBean("crestTemplate"); Scanner scanner = new Scanner(System.in); System.out.println("Enter the fruitid please"); String fruitid = scanner.next(); List<MediaType> acceptableMediaTypes = new ArrayList<MediaType>(); acceptableMediaTypes.add(MediaType.APPLICATION_JSON); HttpHeaders headers = new HttpHeaders(); // what output we are expecting headers.setAccept(acceptableMediaTypes); // /We are setting the format of data which will from client to server headers.setContentType(MediaType.TEXT_PLAIN); HttpEntity requestEntity = new HttpEntity(headers); // Java Http Client ResponseEntity<FruitForm> response = restTemplate.exchange( "https://localhost:8443/spring-app-fruit/rest/v1/fruit/" + fruitid, HttpMethod.GET, requestEntity,FruitForm.class); FruitForm result = response.getBody(); System.out.println(response.getHeaders());
  System.out.println(result);


}



No comments:

Post a Comment