Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Oauth2RestTemplate example #13

Open
cjstehno opened this issue Jan 13, 2015 · 5 comments
Open

Oauth2RestTemplate example #13

cjstehno opened this issue Jan 13, 2015 · 5 comments

Comments

@cjstehno
Copy link

This is a great example project, but it would be nice to have an example using the OAuth2RestTemplate along with the provided curl example.

@cjstehno
Copy link
Author

I was able to get this figured out and have created a Gist of what I did - it's basically just an integration test as example. You are welcome to use it, or I can fork your project and provide a pull request if you'd like me to.

@mariubog
Copy link
Contributor

cjstenho In your example unless you're authorized on the client side I thing that connection would throw an exception for not being authorized, and It wont work for non browser clients or when you try to fully automate the process
here I created an example of what I mean
https://github.com/mariubog/oauth-client-sample

It works with Roy Clarkson's example

@cjstehno
Copy link
Author

@mariubog - thanks, the integration test works; however, I see what you are saying about requiring a username and password. I will take a look at your example. I am new to OAuth so I am still trying to figure out how all the pieces come together. :-)

@royclarkson
Copy link
Owner

This is good stuff. An OAuth2RestTemplate client example would be a very nice addition. Thanks for the suggestion!

@SayedTCS
Copy link

SayedTCS commented Sep 12, 2017

Very nice and simple example to understand the Oauth02 security implementation.
It would be further good , if we can get a Sample Service(greeting) as a separate component to understand the necessary configuration to protect the service .

Below sample code to get the token can be used:

final String uri = "http://localhost:8080/uaa/oauth/token";

	    RestTemplate restTemplate = new RestTemplate();
	    
	    
	 // Create a multimap to hold the named parameters
	    MultiValueMap<String,String> parameters = new LinkedMultiValueMap<String,String>();
	    
	
	    String plainCreds = "clientapp:123456";
	    byte[] plainCredsBytes = plainCreds.getBytes();
	    byte[] base64CredsBytes = Base64.encode(plainCredsBytes);
	    String base64Creds = new String(base64CredsBytes);

	    HttpHeaders headers = new HttpHeaders();
	    headers.add("Authorization", "Basic " + base64Creds);
	    //parameters.add("clientapp", "123456");
	    parameters.add("password", "spring");
	    parameters.add("username", "roy");
	    parameters.add("grant_type", "password");
	    parameters.add("scope", "read write");
	    parameters.add("client_secret", "123456");
	    parameters.add("client_id", "clientapp");

	    headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));//Accept: application/json
	  
	    // Create the http entity for the request
	    HttpEntity<MultiValueMap<String,String>> entity =
	                new HttpEntity<MultiValueMap<String, String>>(parameters, headers);
	  
	    ResponseEntity<String> result = restTemplate.exchange(uri, HttpMethod.POST, entity, String.class);
	  		    System.out.println("result"+result.getBody().toString());

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants