I know this question has been asked more than a few times and I have read all the answers, but I still have a problem. Basically, I am trying to configure OAuth2 RestTemplate, but I get an error (like many others) when I try to use RestTemplate for getForEntity request:
org.springframework.security.oauth2.client.resource.UserRedirectRequiredException: A redirect is required to get the users approval
In the hope that someone will tell me what I'm doing wrong. Here is my configuration class:
@Configuration
@EnableOAuth2Client
public class OAuth2Configuration
{
@Autowired
private OAuth2ClientContext oauth2Context;
@Value("${test.oauth.key}")
private String key;
@Value("${test.oauth.secret}")
private String secret;
@Value("${test.auth.url}")
private String authUrl;
@Value("${test.token.url}")
private String tokenUrl;
@Bean
public OAuth2ProtectedResourceDetails test()
{
AuthorizationCodeResourceDetails details = new AuthorizationCodeResourceDetails();
details.setId("java_server");
details.setClientId(key);
details.setClientSecret(secret);
details.setUserAuthorizationUri(authUrl);
details.setAccessTokenUri(tokenUrl);
return details;
}
@Bean(name = "testRestTemplate")
public OAuth2RestTemplate testRestTemplate()
{
return new OAuth2RestTemplate(test(), oauth2Context);
}
}
This is the beginning of my base test class:
@WebAppConfiguration
public abstract class AbstractJUnitTest extends AbstractJUnit4SpringContextTests
{
@Autowired
protected WebApplicationContext context;
@Autowired
private OAuth2ClientContextFilter clientContextFilter;
@Autowired
private FilterChainProxy springSecurityFilterChain;
protected MockMvc mockMvc;
@Before
public void setup()
{
this.mockMvc = MockMvcBuilders.webAppContextSetup(context)
.addFilters(
new RequestContextFilter(),
new DelegatingFilterProxy(clientContextFilter),
new DelegatingFilterProxy(springSecurityFilterChain))
.build();
}
...
}
And I have this line in my http security configuration:
<security:custom-filter ref="oauth2ClientContextFilter" after="SECURITY_CONTEXT_FILTER" />
Here is my service class that is trying to make a call:
@Service("testService")
public class TestServiceImpl implements TestService
{
@Autowired
@Qualifier("testRestTemplate")
private OAuth2RestTemplate restTemplate;
@Override
public List<Person> listPeople() throws Exception
{
ResponseEntity<String> sample = restTemplate.getForEntity(
myEndpoint, String.class);
return null;
}
}
Any help is appreciated! I hope itβs just something dumb that Iβm missing, but for some reason I donβt see it and therefore need help. Thanks in advance.