douwei1128 2015-02-17 08:14
浏览 21
已采纳

如何在控制器上检查令牌(CSRF)?

There is some option on Laravel that we allow Laravel to create a token and test it on server side to pull up CSRF attacks.

I found this on Laravel website, But didn't say how to check from Controller that is an attack or from a native and real page.

How to check the token (CSRF) on controller?

  • 写回答

2条回答 默认 最新

  • dtcd27183 2015-02-17 08:19
    关注

    Assuming you use laravel 4.x:

    You don't need to check this in your controller. defining the before parameter tells laravel to check this automaticly.

    Route::post('profile', array('before' => 'csrf', function(){ 
        /* CSRF validated! */  
    }));
    

    If you want to do something when the token is incorrect, you can change the filter in app/filters.php. This one:

    Route::filter('csrf', function()
    {
        if (Session::token() != Input::get('_token'))
        {
            throw new Illuminate\Session\TokenMismatchException;
        }
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?