2

I am not sure if I have OAuth2RestTemplate configured correctly. I am getting the following error when I run the tester class.

    INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1df3248: defining beans [propertyConfigurer,dataSource,transactionManager,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0,org.springframework.transaction.interceptor.TransactionInterceptor#0,org.springframework.transaction.config.internalTransactionAdvisor,emf,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,accountRepository,questionRepository,org.springframework.data.repository.core.support.RepositoryInterfaceAwareBeanPostProcessor#0,org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor#0,org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor#0,jpaQuestionService,jpaAccountService,passwordEncoder,accountHelper,tradeConfig,org.springframework.data.repository.core.support.RepositoryInterfaceAwareBeanPostProcessor#1,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor,baseOAuth2ProtectedResourceDetails,oAuth2ProtectedResourceDetails,accessTokenRequest,oAuth2ClientContext,oAuth2RestTemplate]; root of factory hierarchy
Exception in thread "main" error="access_denied", error_description="Unable to obtain a new access token for resource 'null'. The provider manager is not configured to support it."
    at org.springframework.security.oauth2.client.token.AccessTokenProviderChain.obtainNewAccessTokenInternal(AccessTokenProviderChain.java:146)
    at org.springframework.security.oauth2.client.token.AccessTokenProviderChain.obtainAccessToken(AccessTokenProviderChain.java:118)
    at org.springframework.security.oauth2.client.OAuth2RestTemplate.acquireAccessToken(OAuth2RestTemplate.java:216)
    at org.springframework.security.oauth2.client.OAuth2RestTemplate.getAccessToken(OAuth2RestTemplate.java:168)
    at org.springframework.security.oauth2.client.OAuth2RestTemplate.createRequest(OAuth2RestTemplate.java:89)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:442)
    at org.springframework.security.oauth2.client.OAuth2RestTemplate.doExecute(OAuth2RestTemplate.java:123)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:409)
    at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:385)
    at com..main(Tester.java:44)

Classes

@Configuration
public class AppConfig {

@Bean
//@Scope(value="singleton", proxyMode=ScopedProxyMode.INTERFACES) 
public BaseOAuth2ProtectedResourceDetails baseOAuth2ProtectedResourceDetails(){
    BaseOAuth2ProtectedResourceDetails baseOAuth2ProtectedResourceDetails =  new BaseOAuth2ProtectedResourceDetails();
    baseOAuth2ProtectedResourceDetails.setClientId(clientId);
    baseOAuth2ProtectedResourceDetails.setClientSecret(clientSecret);
    return baseOAuth2ProtectedResourceDetails; 
}

@Bean
public DefaultAccessTokenRequest accessTokenRequest(){
    return new DefaultAccessTokenRequest();
}

@Bean
public OAuth2ClientContext oAuth2ClientContext(){
    return new DefaultOAuth2ClientContext(accessTokenRequest());
}

@Bean
public OAuth2RestTemplate oAuth2RestTemplate(){
    OAuth2RestTemplate restTemplate = new OAuth2RestTemplate(baseOAuth2ProtectedResourceDetails(),oAuth2ClientContext());
    return restTemplate;
}
}

Tester Class

public class Tester {

public static void main(String[] args) {

    GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
    ctx.load("classpath*:jpa-app-context.xml");
    ctx.refresh();

EntityManagerFactory emf = (EntityManagerFactory) ctx.getBean("emf");
EntityManager em = emf.createEntityManager(); 
TransactionSynchronizationManager.bindResource(emf , new EntityManagerHolder(em)); 
OAuth2RestTemplate oAuth2RestTemplate = (OAuth2RestTemplate) ctx.getBean("oAuth2RestTemplate");

//OAuth2RestTemplate oAuth2RestTemplate  = ctx.getBean(OAuth2RestTemplate.class);
String uri="https:api..";


Object obj = oAuth2RestTemplate.exchange(uri, HttpMethod.POST, null, Object.class);
System.out.println("Tester Object: "+ obj.toString());
}
}
4

1 回答 1

4

我遇到了同样的异常,但使用了另一种受保护的资源类型。

通常,只有在AccessTokenProviderChain找不到适合*AccessTokenProvider特定*ProtectedResourceDetails实例的情况下才会引发异常。这意味着,当您尝试执行以下操作时:

ClientCredentialsResourceDetails resource = new ClientCredentialsResourceDetails();

resource.setAccessTokenUri(url);
resource.setClientId(clientId);
resource.setClientSecret(secret);
resource.setGrantType("password");

return resource;

代码需要一个client_credentials授权类型,因为我们使用 a ClientCredentialsResourceDetails,但我们传递password值。

这是在我的案例中有效的代码:

private OAuth2ProtectedResourceDetails withOAuth2Authentication(final String url, final String clientId, final String secret) {
    ClientCredentialsResourceDetails resource = new ClientCredentialsResourceDetails();

    resource.setAccessTokenUri(url);
    resource.setClientId(clientId);
    resource.setClientSecret(secret);
    // here you can provide additional properties such as scope etc.

    return resource;
}

@Bean
RestTemplate callbackClientV2() {
    AccessTokenRequest atr = new DefaultAccessTokenRequest();

    return new OAuth2RestTemplate(
                withOAuth2Authentication(v2ServerUrl, v2Username, v2Password),
                new DefaultOAuth2ClientContext(atr)
    );
}
于 2015-11-04T09:44:13.727 回答