2017-07-31 102 views

回答

0

作为一种解决方案,我从控制器向节点服务器提供令牌,并且在客户端连接到节点服务器之后,客户端被请求发送持有者令牌 - 并使用jsonwebtoken包从中提取jti,该包用于与令牌从控制器收到。

这里去代码:

控制器

$userTokenReceiver = $pm->receiver->tokens()->where('revoked', 'false') 
    ->select('id')->get()->sortBy('created_at', true)->first(); 
if ($userTokenReceiver && $userTokenReceiver->toArray()['id']) 
    $dataRedis->receiver_token = $userTokenReceiver->toArray()['id']; 

节点服务器

var jwt = require('jsonwebtoken'); 
... 
io.on('connection', function(socket) { 
    socket.waitingAuthorize = true; 
    // E> authentication_request: after connection asking client to provide token 
    socket.emit('authentication_request'); 

    // O> authorize: waiting for client's response to auth request 
    socket.on('authorize', function(token) { 
     var decoded = jwt.decode(token); 

     if (!decoded || !decoded.jti) { 
      socket.disconnect(true); 
      return; 
     } 

     socket.waitingAuthorize = false; 
     socket.token = decoded.jti; // this is compared to token received from controller 
    }); 
... 
相关问题