Spring OAuth2实现 - 需要重定向才能获得用户批准

时间:2017-03-27 23:15:42

标签: spring spring-oauth2

我知道这个问题已经被问了好几次,我已经阅读了所有的答案,但我仍然有问题。基本上,我正在尝试设置OAuth2 RestTemplate但是在尝试使用RestTemplate发出getForEntity请求时收到错误(就像许多其他人一样):

org.springframework.security.oauth2.client.resource.UserRedirectRequiredException: A redirect is required to get the users approval

希望有人能告诉我自己做错了什么。这是我的配置类:

@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);
    }
}

这是我的基础测试课程的开始:

@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();
    }
    ...
}

我在http安全配置中有这一行:

<security:custom-filter ref="oauth2ClientContextFilter" after="SECURITY_CONTEXT_FILTER" />

这是我尝试拨打电话的服务类:

@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;
    }
}

任何帮助表示赞赏!我希望这只是一种愚蠢的东西,我错过了但无论出于何种原因,我看不到它,因此需要帮助。提前谢谢。

0 个答案:

没有答案