Forked from DaddyMoe/SpringRestTemplateBasicAuthExample.java
Created
July 20, 2018 15:15
-
-
Save makelefy/4d054c01eef967af7861767a292cfc21 to your computer and use it in GitHub Desktop.
Spring RestTemplate Basic Auth Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SpringRestTemplateBasicAuthExample { | |
public static void main(String... args){ | |
RestTemplate rt = new RestTemplate(); | |
rt.getMessageConverters().add(new MappingJackson2HttpMessageConverter()); | |
rt.getMessageConverters().add(new StringHttpMessageConverter()); | |
String uri = new String("https://some.api.provider.com/rest/authenticate"); | |
String plainCreds = "user@awesome.com:sfdfsdf$%&^$%4"; | |
byte[] plainCredsBytes = plainCreds.getBytes(); | |
byte[] base64CredsBytes = Base64.encodeBase64(plainCredsBytes); | |
String base64Creds = new String(base64CredsBytes); | |
HttpHeaders headers = new HttpHeaders(); | |
headers.add("Authorization", "Basic " + base64Creds); | |
HttpEntity<String> request = new HttpEntity<String>(headers); | |
ResponseEntity<String> response = rt.exchange(uri, HttpMethod.POST, request, String.class); | |
log.info("Account Content:" + response.getBody()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment