【发布时间】:2018-05-07 19:07:12
【问题描述】:
使用来自 Angular 应用程序的护照 jwt 进行身份验证 我将 Angular 4 中的请求标头设置为 我在服务器端使用护照 jwt,但出现未经授权的错误
`let strategy = new JwtStrategy(jwtOpts, (jwt_payload, cb)=>{
let stmt = "select * from user where id = ?";
let id = jwt_payload.id;
connection.query(stmt, id, (error, result) => {
if(error){
throw error;
}
if(result){
var userinfo = {
email: result[0].email,
id: result[0].id
}
cb(null, userinfo);
}
else{
cb(null, false);
}
})
})`
//this is my route
`app.use('/tournament',passport.authenticate('jwt', {session: false}), tour_route);`
//This is my angular call
createTournament(name: string){
this.token = 'JWT ' + this.authService.getToken();
const body = {
name
}
this.http.post('http://localhost:8000/tournament',
{
headers: new HttpHeaders().set('Authorization', this.token)
})
.subscribe(
response => {}
);
}
But i am getting an unauthorised access even if i am sending the right token
【问题讨论】:
-
您必须添加代码的相关部分,否则没有人可以帮助您
-
我猜出于某种原因您的令牌没有被输入“授权”。当您从 authService 获取令牌时,我也不明白为什么您的客户端将“JWT”添加到您的令牌中?当您从 localStorage 获取令牌时,根本不应更改从后端发送到本地存储的令牌。您能否发布您的后端代码,例如 app.post() 或 app.put() 等。
-
在发出请求之前,您是否检查过您是否从 this.authService.getToken() 获取令牌?如果您可以添加您编写的代码来验证您的令牌,那将会很有帮助。
标签: angular express nodes passport.js