spring boot how to consume api with basic auth code example
Example 1: resttemplate authorization basic
HttpHeaders createHeaders(String username, String password){ return new HttpHeaders() {{ String auth = username + ":" + password; byte[] encodedAuth = Base64.encodeBase64( auth.getBytes(Charset.forName("US-ASCII")) ); String authHeader = "Basic " + new String( encodedAuth ); set( "Authorization", authHeader ); }};}
Example 2: resttemplate authorization basic
restTemplate.exchange (uri, HttpMethod.POST, new HttpEntity<T>(createHeaders(username, password)), clazz);