I developed a Rest API (A) that returns some results by making calls to another API (B). The (B) API needs an authentication so in my properties file i defined the username and the password that I inject in my RestTemplate to make it work.
What I want to do : Add a basic authentication system to my API (A) so that a user can input his username and password, and use these credentials in the RestTemplate to call API (B). Thanks
You have two options here. You can use Spring basic authentication filter:
Sample code:
<bean id="basicAuthenticationFilter"
class="org.springframework.security.web.authentication.www.BasicAuthenticationFilter">
<property name="authenticationManager" ref="authenticationManager"/>
<property name="authenticationEntryPoint" ref="authenticationEntryPoint"/>
</bean>
<bean id="authenticationEntryPoint"
class="org.springframework.security.web.authentication.www.BasicAuthenticationEntryPoint">
<property name="realmName" value="Name Of Your Realm"/>
</bean>
Or you could use Spring Security that's a Framework designed just to add the security layer to your web application. This is a complete tutorial that explains how to use it to add Basic Authentication to your Rest API:
http://websystique.com/spring-security/secure-spring-rest-api-using-basic-authentication/
Personal opinion, i would use Spring Security.