Announcement Announcement Module
No announcement yet.
Resttemplate + basic authentication? Page Title Module
Move Remove Collapse
Conversation Detail Module
  • 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?


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

    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(
    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.

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


    • #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: (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:
          @RequestMapping(value = "/crimes", method = RequestMethod.GET)
          public @ResponseBody Object jsonCrimes(){
                  SimpleClientHttpRequestFactory s = new SimpleClientHttpRequestFactory() {
                  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("", 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.


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


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