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

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

    Hello,

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


    Code:
    <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 = "..."/>
                    </bean>
                </property>
            </bean>
        </constructor-arg>
    </bean>
    
    
    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;
        }
    
    
        @Override
        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 - http://goo.gl/xwic

    But this code works Ok:

    Code:
    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

    Comment


    • #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.

      Comment


      • #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.

        Comment


        • #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.

          Comment


          • #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:

            Code:
            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:

            Code:
            echo -n server:password | base64

            Comment

            Working...
            X