Laravel 当 *not* 以什么开头时路由过滤器?
Laravel route filter when *not* starts with?
现在我有
Route::when('*', 'csrf', ['post']);
在我的 routes.php
文件中保护我免受 CSRF 攻击。在 filters.php
我有:
Route::filter('csrf', function()
{
if (Session::token() !== Input::get('_token'))
{
throw new Illuminate\Session\TokenMismatchException;
}
});
我现在遇到的问题是我正在与第 3 方服务(Google 日历 API)集成,它会 POST 回我的网站,这当然会触发 CSRF保护。
如何为特定路径前缀禁用该过滤器,比如 /gapi
?
您需要定义 2 个路由组 并应用 csrf 并应用 CSRF 过滤器其中之一。通过这种方式,您可以将需要启用 CSRF 保护的路由与不需要的路由分开。
这对你有用:
Route::filter('csrf', function() {
if (Request::method() == 'POST' && Session::token() !== Input::get('_token')) {
throw new Illuminate\Session\TokenMismatchException;
}
});
Route::group(array('before' => 'csrf'), function() {
// here define all routes that need CSRF protection enabled
});
Route::group(array(), function() {
// here define all routes that do not need CSRF protection
});
您可以在 Laravel 4.2 中详细了解路由分组的工作原理:http://laravel.com/docs/4.2/routing#route-groups
您还可以排除以特定 URL:
开头的路线
Route::whenRegex('#(?!gapi/)#A', 'csrf', ['post']);
现在我有
Route::when('*', 'csrf', ['post']);
在我的 routes.php
文件中保护我免受 CSRF 攻击。在 filters.php
我有:
Route::filter('csrf', function()
{
if (Session::token() !== Input::get('_token'))
{
throw new Illuminate\Session\TokenMismatchException;
}
});
我现在遇到的问题是我正在与第 3 方服务(Google 日历 API)集成,它会 POST 回我的网站,这当然会触发 CSRF保护。
如何为特定路径前缀禁用该过滤器,比如 /gapi
?
您需要定义 2 个路由组 并应用 csrf 并应用 CSRF 过滤器其中之一。通过这种方式,您可以将需要启用 CSRF 保护的路由与不需要的路由分开。
这对你有用:
Route::filter('csrf', function() {
if (Request::method() == 'POST' && Session::token() !== Input::get('_token')) {
throw new Illuminate\Session\TokenMismatchException;
}
});
Route::group(array('before' => 'csrf'), function() {
// here define all routes that need CSRF protection enabled
});
Route::group(array(), function() {
// here define all routes that do not need CSRF protection
});
您可以在 Laravel 4.2 中详细了解路由分组的工作原理:http://laravel.com/docs/4.2/routing#route-groups
您还可以排除以特定 URL:
开头的路线Route::whenRegex('#(?!gapi/)#A', 'csrf', ['post']);