Monday, February 13, 2017

Basic Authentication of spring with Restangular

Leave a Comment

I am using Restangular with Spring's oauth security and in the client side i am using Restangular for login request.

Code in OAuth2ServerConfiguration:

   @Override     public void configure(ClientDetailsServiceConfigurer clients) throws Exception {         clients             .inMemory()                  .withClient("clientapp")                     .authorizedGrantTypes("password", "refresh_token")                     .authorities("USER")                     .scopes("read", "write")                     .secret("abc");         } 

Login with postman needs these configurations:

1-Set Authorization as "Basic Auth". 2-Set username,password as {"username":"clientapp","password":"abc"}//credentials to access server side 3-In request body through "x-www-form-urlencoded" three parameters are sent.   {"username":"abc@gmail.com","password":"abc123","grant_type":"password"}//credentials to login which are checked from database. 

This will do a successful login.but i cannot understand how to use these configurations in Angular JS Restangular call.

currently m trying with this.

In Config:

    RestangularProvider.withConfig(function (RestangularConfigurer) {    return RestangularConfigurer.setDefaultHeaders({ "Authorization": "Basic Y2xpZW50YXBwOkxNUw==",         "username":"clientapp",         "password":"abc",         "Content-type": "application/x-www-form-urlencoded; charset=utf-8"             }); 

In Controller:

     Restangualar.all("oauth/login").post({username;$scope.user.username,      password:"$scope.user.password","grant_type":"password"}).then(function(){  console.log(res);   }); 

But I am getting this error:

  error:"unauthorized",error_description:"Full authentication is required to access this resource"   

in browser.

Note:This resource is not secured.

Any Solution???

Update: I forgot to added a main information that my frontend with angular is running independently on localhost(through xampp) while spring login backend is on localhost:8080..

2 Answers

Answers 1

The advantage of using restangular is the ability to manage resources in a more semantic way and the ability to get nested resources. All these advantages don't really apply for a call just to retrieve a token from an oauth2 provider.

I would recommend to forget about using restangular for this specific call (you still can use it for everything else in your application) and convert this call to a simple $http.post.

$http.post('oauth/login',             {  username;$scope.user.username,               password:"$scope.user.password",               "grant_type":"password"            },             {                 headers: { "Authorization": "Basic Y2xpZW50YXBwOkxNUw==",                            "username":"clientapp",                            "password":"abc",                            "Content-type": "application/x-www-form-urlencoded; charset=utf-8"                          }             })             .then(function(response) {                 Restangular.setDefaultHeaders({                      "Authorization": "Bearer " + response.token                 });             }); 

So, you just use $http.post, and on its response set the default headers in angular to use the retrieved token.

Cheers,

Answers 2

You can use Restangular custom post. See documentation.

Example:

Restangular.service("/oauth/login").one().customPOST(   {},   '',   {     // Params...     grant_type: 'password',     client_id: 'clientapp',     client_secret: 'abc',     username: 'abc@gmail.com',     password: 'abc123',     scope: 'read, write'   },   {     // headers...   }).then(     function (response) {       // Manage successfull response     },     function () {       // Manage error response     } ); 

Hope it helps

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment