Announcement Announcement Module
Collapse
No announcement yet.
How to pass user and password to Spring REST service with Spring RESTTemplate Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to pass user and password to Spring REST service with Spring RESTTemplate

    How to pass user and password to Spring REST service with Spring RESTTemplate..

    I have a Spring MVC project using Spring Secuirty and I need to write a client with Spring RESTTemplate to access it.. I have the following code but it does not work:

    Code:
    public static void main(String[] args)
        {
            LOGGER.debug("Starting REST Client!!!!");
    
    
            HttpClient client = new HttpClient();
    
            UsernamePasswordCredentials credentials =
                    new UsernamePasswordCredentials("test","test");
    
            client.getState().setCredentials(
                    new AuthScope("127.0.0.1", 8080, AuthScope.ANY_REALM),
                    credentials);
    
            CommonsClientHttpRequestFactory commons = new CommonsClientHttpRequestFactory(client);
    
            RestTemplate restTemplate = new RestTemplate(commons);
    
    
            String jsonreturn = restTemplate.getForObject("http://127.0.0.1:8080/springmvc-rest-secured-test/json/Regan", String.class);
    
            LOGGER.debug(jsonreturn);
        }

    I am getting the spring login screen in my return and not the data I am looking for so this tells me that my credentials is not working..

  • #2
    And how have you secured your service? Currently you are supplying credentials voor BASIC authentication, if you have a form login that obviously will not work.

    Comment


    • #3
      thanks Marten,

      yes that was my issue I was using BASIC authentication in the REST client but using FORM in the REST service so I changed my REST service to you BASIC authentication...

      Comment


      • #4
        Is there any other alternate other than change the service... I am facing the same issue.. But don't want to change the service

        Comment

        Working...
        X