Announcement Announcement Module
Collapse
No announcement yet.
Resttemplate + basic authentication? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Resttemplate + basic authentication?

    I found some older threads about this, but it seems that the information is outdated. Does anyone have any new ideas on how to do this?

    Thanks!

  • #2
    I also need to solve this issue -basic authentication with spring RestTemplate- (used from an Android client). The following code example suggested by http://forum.springsource.org/showth...h-RestTemplate back in 2009 does not work with Spring 3.0.5 or 3.1 (milestone release).

    Code:
    HttpClient client = new HttpClient();
    UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("your_user","your_password");
    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);
    The server simply still gives code 401 (Unauthorized);
    If I'm missing something -obvious or otherwise- please advice.
    What is the best practice to do this with the latest Spring version? For example CommonsClientHttpRequestFactory is now Deprecated. In favor of HttpComponentsClientHttpRequestFactory, which cannot be replaced right off the bat with the code logic above. Any help would be most appreciated.

    Thanks.
    Last edited by Vassago; Aug 22nd, 2011, 09:03 AM.

    Comment


    • #3
      I originally tried using HTTPClient with Spring MVC to use Basic Authentication with the police API. I got it to the point where it was impossible for the code to be wrong, but I was still getting a 500 error. SO I tried the same with the Twitter API since it uses a different type of Authentication. IT STILL DIDN'T WORK... In the end Habuma gave a nice explanation in this post on why it wasn't working: http://forum.springsource.org/showth...-Spring-Social (I suggest having a read since you will most likely NOT get it to work at all without setting special permissions.)

      Basically I tried setting permissions and it still wouldn't work (FOR ME ANYWAY). I later found out that spring had its own HTTP Client factory, So I just overridded "prepareConnection()" in springs SimpleClientHttpRequestFactory class:
      Code:
          @RequestMapping(value = "/crimes", method = RequestMethod.GET)
          public @ResponseBody Object jsonCrimes(){
      
                  SimpleClientHttpRequestFactory s = new SimpleClientHttpRequestFactory() {
                  @Override
                  protected void prepareConnection(HttpURLConnection connection, String httpMethod) throws IOException {
                      super.prepareConnection(connection, httpMethod);
      
                      //Basic Authentication for Police API
                      String authorisation = "username" + ":" + "password";
                      byte[] encodedAuthorisation = Base64.encode(authorisation.getBytes());
                      connection.setRequestProperty("Authorization", "Basic " + new String(encodedAuthorisation));
                  }
              };
      
      
              RestTemplate rt = new RestTemplate(s);
      
      
              Object crime;
              crime = rt.getForObject("http://policeapi2.rkh.co.uk/api/crimes-street/all-crime?lat=52.629729&lng=-1.131592", Object.class);
      
              return crime;
      
          }
      After I had done that and removed my HTTPClient 4 jars (Yes, that's right, even though you may not have HTTPClient code, It still won't work until you get rid of the jars), Basic Authentication for the police API started working and my Spring Social TwitterTemplate naturally started working...
      Last edited by markabrucey; Aug 23rd, 2011, 09:53 AM.

      Comment


      • #4
        Solution suggested by markabrucey is not good, cause in that way authentication happens with every request. Server replies with auth cookies but they simply ignored.

        Comment


        • #5
          Any new development on this - perhaps a quick example of how to do it?
          Thanks.

          Comment

          Working...
          X