我有一個使用'spring-cloud-oauth2'和'spring-cloud-security'進行安全保護的oauth-server,並且該應用程序是一個spring-boot應用程序。我試圖從角度2應用獲取access_token,即我想從角度2應用登錄。在我的oauth-server中,我有一個client-id = microservice和client-secret = microservicesecret。 我已經檢查了郵遞員oauth-server,在授權部分中,用戶名和密碼分別使用客戶端ID和客戶端密碼。在body部分,我使用grant_type = password,client_id = microservice,[email protected],password = passw0rd,並且一切都很好。如何使用spring-oauth2服務器連接角度2應用程序?
但是,我在使用angular 2應用程序中的配置時遇到了問題。我已經使用了下面的代碼片段。
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);
}
});
}
此處userEmail和userPassword由用戶插入。 令牌網址是
loginUrl:string="http://localhost:9000/services/oauth/token";
但是,我gettng 401錯誤。我需要解決方案。