Hi, its been a while since my last post. there are some project that taking more time, so i’m not updating this blog until now, some friends contact me asking about my post, i try my best to reply them. How to Configure Basic Auth Spring RestTemplate.
Today, i will share about How to Configure Basic Auth on Spring RestTemplate.
Spring ResTemplate is very useful class to consuming any rest api, by default its not using any Authentication Method.
The example of Basic Auth is something like this:
There are many API Authentication Type you can read How to Secure Your REST API using Proven Best Practices. i think the most popular is using OAuth 2, but the easiest way is using Basic Auth. you can modify this method to use another Auth Type.
Ok, lets down to the code.. First, create BasicRestClient.java that extends Spring’s RestTemplate, and create constructor with username and password parameter. The constructor will create Base64 encoded String used for Authentication header.