How to use cookies to authenticate in an API?

Asked

Viewed 124 times

0

I have a problem to authenticate in an API, it generates a cookie after logging in with user and password, soon after, to perform a GET I need to send the generated cookie.

Explaining in detail:

To log in with Curl

Curl -X POST -c /tmp/admincookies.txt -H "Content-Type: application/json" -- insecure https://sd_wan_center_device_ip/sdwan_center/nitro/v1/config/login --data '{"login":{"username":"admin","password":"password"}}'

Consuming

Curl -X GET -b /tmp/admincookies.txt --insecure "https://sd_wan_center_device_ip/sdwan_center/nitro/v1/Reports/sites"

Now I need to move this to Java: Follows code:

String url = "https://IP_DO_DISP/sdwan_center/nitro/v1/config/login";
     HeadLog hea = new HeadLog();

     hea.login.setUsername("admin");
     hea.login.setPassword("senha");

     GsonBuilder builder = new GsonBuilder();
     Gson gson = builder.create();
     System.out.println(gson.toJson(hea));

     RestTemplate restTemplate = new RestTemplate();
     ResponseEntity<String> headReturn = restTemplate.postForEntity(url,gson.toJson(hea) , String.class);

     HttpHeaders headers = headReturn.getHeaders();
     String set_cookie = headers.getFirst(headers.SET_COOKIE);

     System.out.println("Response: " + headReturn.toString() + "\n");
     System.out.println("Response: " + headReturn.getStatusCode() + "\n");
     System.out.println("Response: " + headReturn.getBody() + "\n");
     System.out.println("Set-Cookie: " + set_cookie + "\n");
     System.out.println("********* FINISH AUTH *******\n\n");

Answer:

{"login":{"username":"admin","password":"password"}} Response: 200 OK Response: {"status":"Success","user_level":"1","user_level_str":"admin","message":"Login Success - Created Session for user admin - level: 1"} Set-Cookie: COOKIE_NAME=value; expires=Wed, 30-Oct-2019 18:33:33 GMT; path=/; Secure; Httponly ********FINISH AUTH *******

Consuming with GET:

String urlWan_Links = "https://IP_DO_DISP/sdwan_center/nitro/v1/reports/sites";
     RestTemplate restTemplateDois = new RestTemplate();
     HttpHeaders requestHeaders = new HttpHeaders();
     requestHeaders.add("Cookie",set_cookie);
     System.out.println("Request Header: "+ requestHeaders);
     HttpEntity requestEntity = new HttpEntity(requestHeaders);
     System.out.println("Request Entity: " + requestEntity);
     ResponseEntity<String> response;
     try {           
         response = restTemplateDois.exchange(urlWan_Links, HttpMethod.GET, requestEntity, String.class);
         System.out.println(response.getBody());
         System.out.println("\n Resposta: " + response);
     }
     catch(Exception e) {
         System.out.println("Erro: "+ e);
     }

Answer:

Request Header: [Cookie:"COOKIE_NAME=value; expires=Wed, 30-Oct-2019 18:33:33 GMT; path=/; Secure; Httponly"] Request Entity: <[Cookie:"COOKIE_NAME=value; expires=Wed, 30-Oct-2019 18:33:33 GMT; path=/; Secure; Httponly"]> Error: org.springframework.web.client.Httpclienterrorexception$Unauthorized: 401 Unauthorized

Anyway, I’m not finding a solution.... Could someone give me a hand?

  • Have you tried working with the Factory standard? That way I believe you can keep the same session

  • I do not know, but I will research on the subject. Thanks for the tip!!!

  • Update here later if you have any progress

  • Now I understand..... it sends 3 set-cookies back, in this.. Voce must pick up the last received, so it will be the key to future queries....

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.