Laravel棘轮插座Auth

问题描述 投票:2回答:3

我开始学习Ratchet(reactPHP)我正在使用laravel。但我谈到了安全问题。如何拒绝基于用户的websocket连接登录与否

public function onOpen(ConnectionInterface $conn)
    {
        $this->clients->attach($conn);
        $this->users[$conn->resourceId] = $conn;
        if(Auth::check()){
            echo 'user logged in';
        }else{
            echo "New connection! ({$conn->resourceId})\n";
        }

    }

我使用了类似的东西,但是它通过了Auth :: check,并且控制台总是显示New Connection。

sockets laravel websocket laravel-5 ratchet
3个回答
0
投票

好的玩找到解决方案似乎没问题:我正在使用Sentinel

$session = (new SessionManager(App::getInstance()))->driver();
$cookies = $conn->WebSocket->request->getCookies();
$laravelCookie = urldecode($cookies['timeline_auth']);
$idSession = Crypt::decrypt($laravelCookie);
$user = Sentinel::findByPersistenceCode($idSession);

如果有更好的解决方案,请发表评论


0
投票

你不能再使用Auth :: user()和WebSocket了。 WebSocket服务器正在处理多个连接(因此Auth :: user()dosent有任何意义)。但您可以访问用户会话。

这里有更多细节

https://laravel.io/forum/01-16-2015-loading-laravels-session-using-ratchet


0
投票

使用laravel-ratchet包。

它将为您处理与auth转换和laravel会话的连接。

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