'RestTemplate does not follow redirects even when GET is used

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



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source