I have an oauth-server secured with 'spring-cloud-oauth2' and 'spring-cloud-security' and the application is a spring-boot application. I am trying to get access_token from an angular 2 app, i.e. I want to login from angular 2 app. In my oauth-server I have a client-id=microservice and client-secret=microservicesecret. I have checked the oauth-server from postman where, in the authorization section, for username and password, I use the client-id and client-secret respectively. And in the body section, I use grant_type=password, client_id=microservice, username=m@email.com, password=passw0rd and everything works great.
But, I am having trouble using the configuration in angular 2 app. I have used the following code-snippets.
userLogin(){
console.log("In the userLogin()");
var username:string='microservice';
var password:string='microservicesecret';
let headers = new Headers();
headers.append('Content-Type', 'application/json');
headers.append('grant_type','password');
headers.append('client_id','microservice');
headers.append('client_secret','microservicesecret');
headers.append('username',this.user.userEmail);
headers.append('password',this.userPassword);
console.log("User");
console.log(this.user);
console.log(headers);
console.log( JSON.stringify({username:username,password:password}));
var data = "username=" + username + "&password=" + password ;
this.http
.post(this.loginUrl,
JSON.stringify({user,password}),
{headers}
)
.map(res=>res.json())
.map((res)=>{
if(res.success){
console.log("Success");
localStorage.setItem('access_token',res.access_token);
console.log("Access token");
console.log(res.access_token);
}
});
}
Here userEmail and userPassword is inserted by the user. The token url is
loginUrl:string="http://localhost:9000/services/oauth/token";
But, I am gettng 401 error. I need solution.