express-jwt和带有Cookie的Angular 9:未在“ Authorization”标头中设置令牌)>

问题描述 投票:0回答:1

你好,

我正在努力在我的后端和Angular 9前端之间实现JWT。

我已经决定要使用Cookie

(稍后我将确保安全)。

使用express-jwt,我可以使用以下代码创建cookie并将其发送到我的angular应用程序:

// The middleware function
const checkIfAuthenticated = expressJwt({
   secret: RSA_PUBLIC_KEY
})

[...]

// The part where I send the cookie to Angular
res.status(200)
   .cookie('SESSIONID', jwtBearerToken, {httpOnly:true, secure:false})
   .send('OK');

[...]

// My testing route, to be tried after the browser got the cookie
app.get('/test', checkIfAuthenticated, (req, res) => {
    console.log(req.cookies)
    res.status(200).json("Authorized!")
})

我可以使用开发人员的工具看到Cookie(在开发过程中请注意secure:false,但是如果我激活checkIfAuthenticated

,则会收到以下消息:
UnauthorizedError: No authorization token was found

然后,由Angular发出的请求包含令牌,但不在教程描述的Authorization标头中(类似于一个:https://blog.angular-university.io/angular-jwt-authentication/]

,而是令牌位于常规cookie中。

这令人惊讶,因为有关express-jwt文档:

该模块的默认行为是从Authorization标头

使用Wireshark,我在调用后端时观察到,JWT令牌以常规cookie的形式传递,express-jwt期望将其找到到Authorization标头中。

我从几个小时开始潜入网络,但我不认为应该如何在[授权]标头中发送通过.cookie('SESSIONID', jwtBearerToken, {httpOnly:true, secure:false})发送的经典Cookie。我原以为cookie-parser将负责此工作,但现在我对此表示怀疑。

欢迎任何帮助,在此先谢谢您!

[你好,我正在我的后端和Angular 9前端之间实现JWT。我已决定使用Cookie(稍后我将予以保护)。使用express-jwt,我可以...

angular cookies jwt authorization express-jwt
1个回答
0
投票

我不完全确定该系统的功能以及JWT的cookie中间件如何工作,从angular-universisty教程的注释部分中,我可以看到有人对authorizaiton标头也没有评论,那

© www.soinside.com 2019 - 2024. All rights reserved.