Giter Club home page Giter Club logo

Comments (5)

cjstehno avatar cjstehno commented on September 26, 2024

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.

from spring-rest-service-oauth.

mariubog avatar mariubog commented on September 26, 2024

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

from spring-rest-service-oauth.

cjstehno avatar cjstehno commented on September 26, 2024

@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. :-)

from spring-rest-service-oauth.

royclarkson avatar royclarkson commented on September 26, 2024

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

from spring-rest-service-oauth.

SayedTCS avatar SayedTCS commented on September 26, 2024

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());

from spring-rest-service-oauth.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.