JsonWebToken | 获取请求validation

我目前正在尝试构build一个基于令牌的简单login系统。 我有一个简单的login表单,将凭据发送到服务器。 创build一个令牌也适用于我。

如果用户login并手动打开用户仪表板链接,则服务器将收到请求。 我怎样才能validation令牌呢? 我无法find一个方法来访问它的get事件。

令牌创build:

var token = jwt.sign({ Username: 'test' }, app.get('tokenPass')); // This token is either send as response or via emitting. 

在客户端,我将其存储为

 $window.sessionStorage = token; 

我的问题:

 app.get('/dashboard', function(req, res) { // req is not including the token // I cant deliver the dashboard without // validating that the requesting user // is logged in. }); 

我想阻止使用cookie(这就是为什么我select了令牌authentication)。 此外,我希望能够从简单的发射访问令牌(这是可能的,如果它被存储在SessionStorage中)。

这是一个私人项目,所以我不介意在需要的时候获得替代方法的想法。 我感谢在这里的每一个帮助。

非常感谢!

根据评论编辑:

服务器部分

 // Simply tried sending a token for testing purposes // After a page-refresh I expected being able accessing it router.get('/', auth, function(req, res) { // Sending the index.html res.sendFile(exp.get('views') + '/index.html'); // Token is never delivered console.log('BODY: ' + req.body); console.log('BODY: ' + req.query); console.log('BODY: ' + req.query.token); console.log('BODY: ' + req.headers['x-access-token']); // New token is send here var token = jwt.sign({ username: 'testUser' }, 'SECRET'); req.io.emit('token', { token: token }); }); 

客户端部分

 <script> $(function (){ var socket = io(); // Incoming token get sets to sessionStorage socket.on('token', function(data) { // Is raising and showing token. alert('token: ' + data.token); $window.sessionStorage.accessToken = data.token; $window.sessionStorage = data.token; }); $('#logout').click(function (err){ sessionStorage.token = null; }); }); </script> 

主体的控制台输出是:

 BODY: undefined BODY: [object Object] BODY: undefined BODY: undefined 

我对node.js和webcoding本身很陌生。 所以请耐心等待我

将json web令牌从客户端发送到服务器,而不使用会话当然最熟知的技术是发送请求标头。

当你在客户端进行ajax调用时,添加如下标题:

 $.ajax({ ... beforeSend: function(request) { request.setRequestHeader("Access-Token", jsonWebToken); } ... }); 

然后,您可以validation中间件中的json Web令牌,如下所示:

 const verifyMiddleware = function(req, res, next){ const jsonWebToken = req.headers['Access-Token']; jwt.verify(jsonWebToken, function(err, user){ if(err) { return res.status(401).send('Bad Auth'); } else { req.user = user; next(); } } } 

您可以从应用程序中调用中间件,如:

 app.get('/dashboard', verifyMiddleware, function(req, res) { // req is not including the token // I cant deliver the dashboard without // validating that the requesting user // is logged in. }); 

缺less的是将令牌发送到服务器并获取authentication的方法。 既然你使用HTTP,你有很多可能的方法来做到这一点。
最常见的两个是cookies和本地存储:

cookies

Cookies通过浏览器自动发送到服务器,几乎所有的HTTP工具都可以发送。 因此,您将令牌存储在cookie中,并在服务器端检查cookie的存在并validation其中的JWT。

优点:

  • 使用一个已知的技术,所以你可能有更多的文件如何做到这一点
  • 应该使用所有的HTTP工具

localStorage的

由ardilgulez更详细的逻辑是,服务器有两个端点。 一个不需要authentication,并提供一个网页来检查JWT的LocalStorage,并在一个AJAX调用中使用它来对第二个服务器端点进行authentication并返回数据。 一旦页面有数据,它就编辑HTML来打印它们。 这样做的另一个好处是您提供的端点只服务于数据,因此当有一天您想要创build另一个客户端时,无论您的网站是“v2”,都不需要触摸服务器端。

优点:

  • 你有一个像端点一样的REST,只有相关的数据,任何未来的客户端可能会使用。
  • 根据您如何有效地发送令牌,configurationHTTP工具比使用cookie更容易

还请查看我们的聊天讨论