-1

I am searching for working code sample/ snippet of spring boot to post json content to HTTPS restful web service(developed in python). Below is sample code of standalone program which does the same, But I want to do it in spring boot Restful app. I found many examples in google and stack overflows example example but these are for get request and not suites for what I am looking for. Someone please share full working example for "https post request using spring boot service". Thanks in advance.

import java.io.PrintStream;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import javax.net.ssl.HttpsURLConnection;

public class App{

     public static void main(String []args) throws NoSuchAlgorithmException, KeyManagementException{

        String https_url = "https://192.168.4.5:55543/books";
        String content = "{\"data\":{\"a\"}}";
        System.setProperty("javax.net.useSystemProxies", "true");

        System.setProperty("javax.net.ssl.keyStore", "C:/cert/client.jks");
        System.setProperty("javax.net.ssl.keyStorePassword", "testdev");

        System.setProperty("javax.net.ssl.trustStore", "C:/cert/server.jks");
        System.setProperty("javax.net.ssl.trustStorePassword", "testdev");

        System.setProperty("jdk.tls.client.protocals", "TLSv1.2");
        System.setProperty("https.protocals", "TLSv1.2");

        HttpsURLConnection.setDefaultHostnameVerifier((hostname, session) -> { return true;});
        URL url = new URL(https_url);
        HttpsURLConnection https_con = (HttpsURLConnection) url.openConnection();

        https_con.setRequestProperty("Content-Type", "application/json");
        https_con.setRequestMethod("POST");
        https_con.setDoOutput(true);
        PrintStream ps = new PrintStream(https_con.getOutputStream());
        ps.println(content);
        ps.close();
        https_con.connect();
        https_con.getResponseCode();
        https_con.disconnect();
     }
}
user5282318
  • 31
  • 1
  • 6

1 Answers1

0

Ok, so here is the forked Github repo.

Following are the changes I made:

secure-server -> Added post endpoint with simply String payload:

@RestController
public class HomeRestController {
    //...
    @PostMapping(value = "/", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String consumeData(@RequestBody String data, Principal principal){
        return String.format("Hello %s! You sent: %s", principal.getName(), data);
    }
}

secure-client -> added call to that post method:

@RestController
public class HomeRestController {
    // . . .

    @GetMapping("/post")
    public String post() throws RestClientException {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);

        String data = "Test payload";

        HttpEntity<String> request = new HttpEntity<>(data, headers);

        return restTemplate.postForEntity("https://localhost:8443", request , String.class ).getBody();
    }
}

So, when you make call to client endpoint as:

http://localhost:8086/post

You will get response:

Hello codependent-client! You sent: Test payload
Yogen Rai
  • 2,961
  • 3
  • 25
  • 37
  • your example is not simulating my example, Can you please share the accurate code by refactoring your code, I am looking for similar mutual authentication as https://github.com/codependent/spring-boot-ssl-mutual-quest authentication example for post request. thanks – user5282318 Jul 12 '18 at 22:15
  • here is the similar mitual auth get request example[https://github.com/codependent/spring-boot-ssl-mutual-authentication/blob/master/secure-client2/src/main/java/com/codependent/mutualauth/SecureClient2Application.java] for which I am looking for "post" request – user5282318 Jul 12 '18 at 22:50
  • Ok, but is there any specific reason for not loading keystore and truststore certificates in the spring boot Rest client? I don't see the code base – user5282318 Jul 13 '18 at 06:32
  • here is the example for it https://stackoverflow.com/questions/27724544/specifying-trust-store-information-in-spring-boot-application-properties – user5282318 Jul 13 '18 at 06:34
  • What is the difference between the cert loading SSLContextBuilder() .loadTrustMaterial() vs System.setProperty() approach? Thanks – user5282318 Jul 13 '18 at 06:52
  • @user5282318 they are being set as environment variables on initSsl method in main app on client. I will go with this approach instead of loadjng them manually which obviously is better for coding and less error prone – Yogen Rai Jul 13 '18 at 12:51
  • Thanks Rai, what is the right approach for https interactions. I need to adjust my http client. For this purpose I have two parameters: connection timeout and connection request timeout. So, my question is what do this two terms mean and how they distinct from each other in https(ssl) context eg: https://stackoverflow.com/questions/20271017/connection-and-connection-request-timeout – user5282318 Jul 16 '18 at 19:14
  • @user5282318 they are as same as in described in answer independent of http being secure or not! – Yogen Rai Jul 16 '18 at 19:27
  • @user5282318 and if you are ok.. you can upvote the answer :) – Yogen Rai Jul 16 '18 at 20:33