简体   繁体   English

Laravel 的 CSRF 令牌和负载均衡

[英]Laravel's CSRF token and load balancing

Consider this scenario:考虑这个场景:

An application has a login route that is protected by Laravel's CSRF filter:应用程序有一个受 Laravel 的 CSRF 过滤器保护的登录路由:

Route::group(array('before' => 'csrf'), function() {

    Route::post('/doLogin', array('as' => 'doLogin', 'uses' => 'MainController@doLogin'));

});

The application sits behind a load balancer, where each request is doled out randomly to either server01 or server02 .该应用程序位于负载平衡器之后,其中每个请求随机server01server01server02 Laravel is configured to persist sessions in a database, which is shared by both server01 and server02 . Laravel 被配置为在数据库中持久化会话,该数据库由server01server02共享。 The standard path to follow is: a user accesses / , enters their credentials into a login form, and submits those credentials to /doLogin , which checks the token, processes the credentials, and returns the user to / on error, or /home on success.要遵循的标准路径是:用户访问/ ,在登录表单中输入他们的凭据,然后将这些凭据提交到/doLogin ,后者检查令牌,处理凭据,并将用户返回到/ on error 或/home on成功。

My question is this: since there's no guarantee that a user who accesses / on server01 will post to /doLogin on server01 , will Laravel's built-in CSRF tokens work?我的问题是:由于不能保证访问/server01上的server01将发布到/doLoginserver01 ,Laravel 的内置 CSRF 令牌是否有效? Or since the token is stored in Session , will it work regardless of which server ends up being assigned by the LB?或者由于令牌存储在Session ,无论 LB 最终分配了哪个服务器,它都可以工作吗?

CSRF will work regardless of which server it hits if session is shared between these servers.如果会话在这些服务器之间共享,则无论它击中哪个服务器,CSRF 都将工作。

Database, Cookie and memcached/redis session drivers are good.数据库、Cookie 和 memcached/redis 会话驱动程序都很好。

File session driver should not work ususally.文件会话驱动程序不应该正常工作。

CSRF token from client is compared with the one in the session.来自客户端的 CSRF 令牌与会话中的令牌进行比较。

You should use cookie or database driver for session handling .您应该使用cookie数据库驱动程序进行会话处理。

for more info read laravel session doc .有关更多信息,请阅读laravel session doc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM