添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I want to consume a REST webservice from a server which protects his resources using oauth2.

I use Spring boot (JHipster).

To do this i have in SecurityConfiguration class this :

@Value("${oauth.resource:http://sercverUsingOAuth2}")
private String baseUrl;
@Value("${oauth.authorize:http://sercverUsingOAuth2/rest/oauth/token}")
private String authorizeUrl;
@Value("${oauth.token:http://sercverUsingOAuth2/rest/oauth/token}")
private String tokenUrl;
@Bean
public OAuth2RestOperations oauth2RestTemplate() {
    AccessTokenRequest atr = new DefaultAccessTokenRequest();
    return new OAuth2RestTemplate(resource(),
            new DefaultOAuth2ClientContext(atr));
@Bean
protected OAuth2ProtectedResourceDetails resource() {
    AuthorizationCodeResourceDetails resource = new AuthorizationCodeResourceDetails();
    resource.setAccessTokenUri(tokenUrl);
    resource.setUserAuthorizationUri(authorizeUrl);
    resource.setClientId("client_id");
    resource.setClientSecret("client_secret");
    resource.setGrantType("grant_type");
    return resource;

This class (SecurityConfiguration) is annoted using :

@Configuration
@EnableWebSecurity
@EnableOAuth2Client

And this is my controller (Spring MVC) :

@RestController
@RequestMapping("/consume")
public class MyContrtoller {
@Inject
private OAuth2RestOperations oauth2RestTemplate;
@RequestMapping(value = "/oauth2", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public List<DataModel> getProducts() {
    ResponseEntity<MyModel> forEntity = oauth2RestTemplate
            .getForEntity("http://sercverUsingOAuth2/rest/resourceToConsume",
                    MyModel.class);
    return forEntity.getBody().getData();

However when i want to consume my webservice (http://myHost/consume/oauth2) i get this Exception :

    org.springframework.security.oauth2.client.resource.OAuth2AccessDeniedException:
 Unable to obtain a new access token for resource 'null'. The provider manager
 is not configured to support it.

I have googled and i found this :

  • github
  • stackoverflow
  • But it doesn't help me.

    Thanks.

    I do not need user name and user password to consume the web service, because the server where this ws is hosted allow me to consume this ws using: client_id,client_secret and grant_type. PS:When i use a rest client apps i get the token form server but using my java code i can't. – LBOSS Jul 22, 2015 at 9:09

    You are using the same URL for the authorization url and the token url. That was my first clue, then I saw your comments.

    Even though you are changing the grant type, you are still using "AuthorizationCodeResourceDetails" when you should be using "ClientCredentialsResourceDetails" instead. This type of ResourceDetails is meant to be used for the case you are explaining.

    ClientCredentialsResourceDetails resource = new ClientCredentialsResourceDetails();
    resource.setAccessTokenUri(TOKEN_URL);
    resource.setClientId(CLIENT_ID);
    resource.setClientSecret(CLIENT_SECRET);
    resource.setClientAuthenticationScheme(AuthenticationScheme.form); //This line isn't always needed
    return resource;
            

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.