Alex Rivera | Logout

Spring Security Authentication using RestTemplate

Asked 2011-01-06T12:47:45.253
11

I have 2 spring web apps that provide 2 separate set of services. Web App 1 has Spring Security implemented using a user-based authentication.

Now, Web App 2 needs to access the service of Web App 1. Normally, we would use the RestTemplate class to make requests to other web services.

How do we pass the authentication credentials in the request of Web App 2 to Web App 1

Edit
Report

1 Answer

2

There's a simple way to do this in case you are someone who's looking for a simple call and not a API consumer.

HttpClient client = new HttpClient();
    client.getParams().setAuthenticationPreemptive(true);
    Credentials defaultcreds = new UsernamePasswordCredentials("username", "password");
    RestTemplate restTemplate = new RestTemplate();
    restTemplate.setRequestFactory(new CommonsClientHttpRequestFactory(client));
    client.getState().setCredentials(AuthScope.ANY, defaultcreds);
answered 2013-05-23T15:21:14.603

Your Answer