我有一个 Spring Boot 后端,它针对数据库对用户进行身份验证并为用户生成一个 JWT 令牌,现在我正在尝试使用 Angular 2 设置前端,我有一个登录功能应该从后端请求身份验证,然后保存localstroge 中的 JWT 令牌,并将用户重定向到 /home 。
login(event, username, password) {
event.preventDefault();
let body = JSON.stringify({ username, password });
this.http.post('/authenticate', body, { headers: contentHeaders })
.subscribe(
response => {
localStorage.setItem('id_token', response.json().id_token);
this.router.navigate(['home']);
},
error => {
alert(error.text());
console.log(error.text());
}
);
当发送请求对用户进行身份验证时,用户已通过身份验证,并返回带有有效 jwt 令牌的 Authorization 标头,但不会重定向到 /home,这是因为 .subscribe() 中的代码从未调用过。什么可能导致这种情况?该代码是本教程的一部分https://auth0.com/blog/creating-your-first-real-world-angular-2-app-from-authentication-to-calling-an-api-and-everything-in -之间/