I know this question has been asked more than a few times and I have read over all of the answers but am still having an issue. Basically, I am trying to setup an OAuth2 RestTemplate but am receiving the error (like so many others) when attempting to use the RestTemplate to make a getForEntity request:
org.springframework.security.oauth2.client.resource.UserRedirectRequiredException: A redirect is required to get the users approval
Hoping someone can tell me what I am 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;
/**
* Returns an OAuth2 protected resource connection to test
*
* @return the oauth2 protected resource
*/
@Bean
public OAuth2ProtectedResourceDetails test()
{
// Create a new authorization code resource details object
AuthorizationCodeResourceDetails details = new AuthorizationCodeResourceDetails();
// Set the information on the authorization code resource details object
details.setId("java_server");
details.setClientId(key);
details.setClientSecret(secret);
details.setUserAuthorizationUri(authUrl);
details.setAccessTokenUri(tokenUrl);
// Return the authorization code resource details object
return details;
}
/**
* Returns a rest template for test
*
* @return the rest template
*/
@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;
// Define protected class variables
protected MockMvc mockMvc;
/**
* Ensure the mock mvc framework has the spring security filter chain in it
*/
@Before
public void setup()
{
// Initialize the mock mvc
this.mockMvc = MockMvcBuilders.webAppContextSetup(context)
.addFilters(
new RequestContextFilter(),
new DelegatingFilterProxy(clientContextFilter),
new DelegatingFilterProxy(springSecurityFilterChain))
.build();
}
...
}
And I have this line in my http security config:
<security:custom-filter ref="oauth2ClientContextFilter" after="SECURITY_CONTEXT_FILTER" />
Here is my service class that attempts to make the call:
@Service("testService")
public class TestServiceImpl implements TestService
{
@Autowired
@Qualifier("testRestTemplate")
private OAuth2RestTemplate restTemplate;
/**
* ${@inheritDoc}
*/
@Override
public List<Person> listPeople() throws Exception
{
ResponseEntity<String> sample = restTemplate.getForEntity(
myEndpoint, String.class);
return null;
}
}
Any help is appreciated! I am hoping it is just something dumb that I am missing but for whatever reason, I can't see it and therefore need help. Thanks in advance.