6

I am using the RestTemplate class to get to some page, which will require redirect ( redirect to oauth 2 provider, but that probably doesnt matter ).

I am doing:

ResponseEntity<String> forEntity = oAuth2RestTemplate.getForEntity(url, String.class, Collections.emptyMap());

with standard OAuth2RestTemplate, I haven't done any custom creation of this bean, so it is just injected from Spring.

As a response I get a ResponseEntity with location header and 302 status, but it is not followed. Judging by many other sources RestTemplate should follow redirects by default if all I do is GET - similar issue here: Follow 302 redirect using Spring restTemplate?

This is not a case, no redirect happens, am I missing something? Do I have to manually grab this location and redirect to it manually?

update:

OK, OAuth2RestTemplate is using OAuth2AccessTokenSupport as a request factory. This class configures factory to NOT follow redirects. Too bad it is so hard to find.

But using = new RestTemplate() doesnt help either ( note new keyword, if you are going to autowire it, then Spring will autowire oauth2RestTemplate ).

Ortomala Lokni
  • 56,620
  • 24
  • 188
  • 240
hi_my_name_is
  • 4,894
  • 3
  • 34
  • 50
  • 3
    Please post your update as answer and accept it. It may save somebody's time someday. – jannis Dec 16 '16 at 11:27
  • Whatever RestTemplate you use, use request factory with Apache HTTP Client. It is much more capable then default request factory: http://stackoverflow.com/questions/37024709/spring-resttemplate-follow-redirect-with-cookie/37054521#37054521 – Michal Foksa Dec 20 '16 at 09:05

0 Answers0