本文介紹了Node.js - Express.js JWT 總是在瀏覽器響應中返回一個無效的令牌錯誤的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我將 node.js 和 express.js 與
var expressJwt = require('express-jwt');var jwt = require('jsonwebtoken');var SECRET = 'shhhhhhared-secret';app.use('/api', expressJwt({secret: SECRET}));app.post('/authenticate', function (req, res) {//TODO 驗證 req.body.username 和 req.body.password//如果無效,返回401if (!(req.body.username === 'john.doe' && req.body.password === 'foobar')) {res.send(401, '錯誤的用戶或密碼');返回;}變量配置文件 = {first_name: '約翰',姓氏:'Doe',電子郵件:'john@doe.com',編號:123};//我們在令牌內發送配置文件var token = jwt.sign(profile, SECRET, { expiresIn: 18000 });//60*5 分鐘res.json({ token: token });});app.get('/api/protected',函數(請求,資源){res.json(req.user);});
I'm using node.js and express.js with the express-jwt module, and I have set up a simple HTTP server to test everything:
This is the node code involved:
app.set('port', process.env.PORT || 3000);
app.use(express.methodOverride());
app.use(allow_cross_domain);
app.use('/api', expressJwt({secret: '09qrjjwef923jnrge$5ndjwk'}));
app.use(express.json());
app.use(express.urlencoded());
app.use('/', express.static(__dirname + '/'));
app.use(function(err, req, res, next){
if (err.constructor.name === 'UnauthorizedError') {
res.send(401, 'Unauthorized');
}
});
app.get('login',function(req,res){
//...
jwt.sign(results.username+results.email, secret, { expiresInMinutes: 9000000000*9393939393393939393939 });
});
app.post('api/profile',function(req,res){
console.log(req.user); // this return undefined in console
res.send(req.user); // response is pending and dunno why it returns error in browser console
});
So once I open the /login
URL I get logged in and I send the session token to api/post
, which returns this response error in the browser console:
{"error":{"message":"invalid signature","code":"invalid_token","status":401,"inner":{}}}
I don't understand why this is happening, because the token stored in the front-end and the token in JWT are the same. What could be the reason for this error?
An example of headers POST
ed to the api/post
URL:
解決方案
Here is an example
http://blog.auth0.com/2014/01/07/angularjs-authentication-with-cookies-vs-token/
var expressJwt = require('express-jwt');
var jwt = require('jsonwebtoken');
var SECRET = 'shhhhhhared-secret';
app.use('/api', expressJwt({secret: SECRET}));
app.post('/authenticate', function (req, res) {
//TODO validate req.body.username and req.body.password
//if is invalid, return 401
if (!(req.body.username === 'john.doe' && req.body.password === 'foobar')) {
res.send(401, 'Wrong user or password');
return;
}
var profile = {
first_name: 'John',
last_name: 'Doe',
email: 'john@doe.com',
id: 123
};
// We are sending the profile inside the token
var token = jwt.sign(profile, SECRET, { expiresIn: 18000 }); // 60*5 minutes
res.json({ token: token });
});
app.get('/api/protected',
function(req, res) {
res.json(req.user);
});
這篇關于Node.js - Express.js JWT 總是在瀏覽器響應中返回一個無效的令牌錯誤的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!