Announcement Announcement Module
No announcement yet.
Sending credentials (user/pass) with RestTemplates - doesn't work for me ? Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Sending credentials (user/pass) with RestTemplates - doesn't work for me ?


    Here's my REST Dao definition and custom request factory providing HttpClient with credentials:

    <bean name="artifactoryDAO" class="com.clearforest.wd40.governor.dao.ArtifactoryDAO">
        <constructor-arg name  = "rest">
            <bean class="org.springframework.web.client.RestTemplate">
                <property name  = "requestFactory">
                    <bean class = "com.clearforest.wd40.governor.dao.ArtifactoryCommonsClientHttpRequestFactory">
                        <constructor-arg name  = "user"
                                         value = "..."/>
                        <constructor-arg name  = "password"
                                         value = "..."/>
    public class ArtifactoryCommonsClientHttpRequestFactory extends CommonsClientHttpRequestFactory
        private final String user;
        private       String user () { return user; }
        private final String password;
        private       String password () { return password; }
        public ArtifactoryCommonsClientHttpRequestFactory ( String user, String password )
            this.user     = user;
            this.password = password;
        public HttpClient getHttpClient ()
            HttpClient client = super.getHttpClient();
            if ( user() != null )
                client.getState().setCredentials( AuthScope.ANY, new UsernamePasswordCredentials( user(), password()));
            return client;
    For some reason, when I access REST resource with this RestTemplate as rest().getForObject() I always get 406 -

    But this code works Ok:

    String     request    = "...";
    GetMethod  getMethod  = new GetMethod( request );
    HttpClient httpClient = new HttpClient();
    httpClient.getState().setCredentials( AuthScope.ANY,
                                          new org.apache.commons.httpclient.UsernamePasswordCredentials( "...", "..." ));
    httpClient.executeMethod( getMethod );
    String s = getMethod.getResponseBodyAsString();
    It seems as if using RestTemplate doesn't send the credentials. I've debugged the RestTemplate instance and made sure it contains HttpClient with a state holding the right credentials. I'm yet to sniff the HTTP traffic to see what exactly is sent over the network.

    My question is: is there anybody else experiencing similar issues when sending credentials with RestTemplate? I guess I'll have more information after sniffing but thought I'll ask it here as well.

    Thank you!

  • #2
    The problem was caused by RestTemplate sending "Accept: text/plain, */*\r\n" header.
    Changing it to "Accept: */*" solved the issue


    • #3
      And, btw, the difference between using RestTemplate and plain HttpClient was that HttpClient didn't send Accept header. Thats what caused the request to go through.


      • #4
        Have you found a way to use HttpClient 4.x to do this? I've looked at the tickets for upgrading to 4.x, and it seems they are languishing away after a year.


        • #5
          I think yes, it was quite a while ago. I'll be able to get back with more details on Sunday, when I'll be in the office.


          • #6
            Originally posted by David Harkness View Post
            Have you found a way to use HttpClient 4.x to do this?
            I see what I did is to set the "Authorization" header before sending a request:

            org.springframework.http.HttpHeaders headers = new HttpHeaders();
            headers.set( "Authorization", "..." );
            T    response = rest( /* returns RestTemplate instance */).exchange( url, method, new HttpEntity<String>( headers ), responseType, args ).getBody();
            Used a basic authentication so "..." above is:

            echo -n server:password | base64