[英]JWT is not working as expected with Node.JS
我很难在Node JS
上使用JWT
让事情正常工作。 我先说我的目标是(登录后)访问我的 API 的privateRoute路由。
我总是打一个:
authHeader == null
在authenticateToken中间件 function 中,虽然我已经尝试了很多东西。
所以我无法通过authenticateToken的级别让我进入。作为引入header内部所需内容并能够通过的解决方案,我曾考虑创建一种进入路径以进入,但它仍然是不工作。
这是相关的代码。
app.get('/privateRoute', authenticateToken, function(req, res) {
// Useful work to do, after logging in.
.......
});
function authenticateToken(req, res, next) {
// Gather the jwt access token from the request header
const authHeader = req.headers['authorization'],
token = authHeader && authHeader.split(' ')[1]
if (token == null) {
console.log('authenticateToken-401')
return res.sendStatus(401) // There isn't any token.
}
// It never reaches this point !!!
jwt.verify(token, 'myBigSecret', (err, user) => {
console.log(err)
if (err) {
console.log('authenticateToken-403')
return res.sendStatus(403)
}
req.user = user
})
}
app.get('/entryRoute', function(req, res) {
res.set('authorization', 'Bearer ' + myJWT_Token);
res.redirect('/privateRoute');
});
有人可以告诉我我需要更改代码以使我的(可能不是那么好)解决方案工作吗? 或者告诉我一个更好的方法?
以下是来自浏览器的一些额外信息,以备不时之需。
在 FireFox 菜单中,Tools,Web Developer,Web Console; 网络选项卡。 我可以看到以下内容:
对于响应标头 (/entryRoute):
Authorization : Bearer eyiwhfehihinR…CuwfihvihiL_hfgSt_J8D
Connection :keep-alive
Content-Length : 56
Content-Type : text/html; charset=utf-8
Date : Mon, 13 Apr 2020 09:46:55 GMT
Location : /privateRoute
Server : Xoibuy
Vary : Accept
Via : 1.1 vegur
X-Powered-By : Express
对于请求标头 (/privateRoute):
Accept : text/html,application/xhtml+xm…ml;q=0.9,image/webp,*/ *;q=0.8
Accept-Encoding : gzip, deflate, br
Accept-Language : en-US,en;q=0.5
Connection : keep-alive
Host : myapp.herokuapp.com
Upgrade-Insecure-Requests : 1
User-Agent : Mozilla/5.0 (Macintosh; Intel …) Gecko/20100101 Firefox/75.0
我的声誉仍然很低,所以我无法发表评论。 我认为您的代码没有问题。 我认为您执行 Http 请求的方式错误,因为授权 header 未传递给请求。 我不知道您在前端使用的是什么语言,但我假设它是 JavaScript。 你可以这样做。
const consumeApi() = async () => {
const response = await fetch('https://example.domain/endpoint', {
method: 'POST' // could be any other methods (GET, POST, PUT, PATCH, DELETE, etc)
headers: {
'Authorization': 'Token some_token_value'
}
});
// Response status other than 200
if (response.status !== 200) return alert('Something wrong happened.');
// Response status 200
// do someting ...
}
使用上面的代码,您应该能够将授权 header 添加到您的 Http 请求中。 希望这可以帮助。
-- 编辑您可以尝试创建一个新的 HTML 文件。 假设 example.html 看起来像这样
// example.html
<!DOCTYPE html>
<html>
<script>
const consumeApi() = async () => {
const response = await fetch('https://example.domain/endpoint', {
method: 'POST' // could be any other methods (GET, POST, PUT, PATCH, DELETE, etc)
headers: {
'Authorization': 'Token some_token_value'
}
});
// Response status other than 200
if (response.status !== 200) return alert('Something wrong happened.');
// Response status 200
// do someting ...
}
// call the function directly after loading the HTML page
consumeApi().then();
</script>
</html>
然后尝试通过 web 浏览器打开 example.html 文件,它应该直接调用您的 api。 这样,您应该能够使用授权 header 向您的 node.js 服务器发出请求。 希望这可以帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.