Results 1 to 3 of 3

Thread: How to consume spring security enabled REST webservice in Android Spring RestTemplate

  1. #1
    Join Date
    Aug 2011
    Posts
    21

    Default How to consume spring security enabled REST webservice in Android Spring RestTemplate

    Hi Guys,

    I created a rest webservice using spring, then later I added a spring authentication module. I would say it is working great as each time I access the REST URL my request ib being intercepted prompting me for username and password.

    Now, I would like to consume the REST webservice in android (authentication spring enabled) using the Spring RestTemplate. My question now is how do I consume the webservice in android given that it has authentication security?

    Could someone help me guys have a code snippet on how to pass the username and password in RestTemplate, or could anyone give me a way to do it.

    Thank you

    Lightyear

  2. #2
    Join Date
    Aug 2011
    Posts
    21

    Default

    Guys,

    To be more specific here's what I was dong but still was not able to run RestTemplate without being challenged for authentication:

    HttpClient client = new HttpClient();
    UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("your_user","your_pass word");
    client.getState().setCredentials(new AuthScope("thehost", 9090, AuthScope.ANY_REALM), credentials);
    CommonsClientHttpRequestFactory commons = new CommonsClientHttpRequestFactory(client);

    RestTemplate template = new RestTemplate(commons);
    JobList results = template.getForObject(
    "http://thehost:9090/foo/job/history?order=desc&limit=5&status=completed"
    ,JobList.class);

    Any idea on how to call a secured (spring authentication) webservice using rest template?

  3. #3
    Join Date
    Jan 2008
    Posts
    1,833

    Default

    You will need to set HttpClient to do Preemptive Authentication. See the 4.x documentation or the 3.x documentation.
    Rob Winch
    Twitter @rob_winch
    Spring Security Lead
    Spring by Pivotal

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •