Consume an OAuth-secured REST webservice using Spring oauth2
NickName:LBOSS Ask DateTime:2015-07-16T19:20:23

Consume an OAuth-secured REST webservice using Spring oauth2

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 :

But it doesn't help me.

Thanks.

Copyright Notice:Content Author:「LBOSS」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/31452729/consume-an-oauth-secured-rest-webservice-using-spring-oauth2

More about “Consume an OAuth-secured REST webservice using Spring oauth2” related questions

Consume an OAuth-secured REST webservice using Spring oauth2

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.

Show Detail

How to consume a Spring Rest service that receives an array of multipart objects

I have zero knowledge about Spring and I need to upload files to a published webservice with the following payload: public ResponseEntity &lt;FileMessage&gt; uploadFiles(@RequestParam(&quot;files&q...

Show Detail

Consume webservice service in SPRING-WS using wsdl

I have WSDL with me .eg: /sample/hello?wsdl . I want to invoke the service the webservice by configuring in Spring-ws. I passed this wsdl as parameter to tags in springconfig.xml. Can anyone please

Show Detail

How can I consume a rest webservice with oauth 1.0a using spring?

I have a Spring MVC WebApp and a wordpress site with wp api configured alongside oauth 1.0a authentication. I have the following values to authenticate: oauth_consumer_key oauth_consumer_secret

Show Detail

Consume SOAP XML in Rest WebService

I am able to consume xml based request in rest webservice. Below is the sample code how I am consuming XML request in my rest webservice. Now, I want to consume soap request XML and I know SOAP is a

Show Detail

How to authenticate with rest webservice using oauth2

Hello I have a rest web service which implement oauth2.0 . Now I have my web application with a login which currently using spring security. I want to use the same login to authenticate with the ...

Show Detail

Calling Rest API Java- Spring Boot with spring batch batch

We are using Spring batch with Spring Boot. We have to read data from a CSV file, transform it and call Rest API to send data to different system. We are able to read CSV file but do not know how c...

Show Detail

consume Oauth2 (authorization code) rest api with spring rest template

I'm trying to consume a rest web service in spring integration project. This web service is secured with oauth2 (authorization code).Any idea how to achieve this? I tried using OAuth2RestTemplate ...

Show Detail

How to consume REST URLs using Spring MVC?

I have developed few RESTful methods and exposed them via Apache Cxf I'm developing the client side application using Spring MVC and I'm looking for a simple example to demonstrate how to call/con...

Show Detail

Securing a Spring Boot REST service using Oauth2 and Keycloak

I've successfully been able to secure a Spring Boot REST service using the keycloak Spring Boot adaptors and a Keycloak identity provider. However we now wish to attempt the same thing thing without

Show Detail