我正在关注本文http://blog.neoxia.com/laravel4-and-angularjs/来设置和过滤CSRF令牌。 我能够使它在本地服务器上工作,但在我部署实时并测试它之后,我不断获得“状态代码418”。有什么想法吗?
以下是我的代码:
Route.php:
// Route to filter CSRF
Route::filter('serviceCSRF',function(){
if (Session::token() != Request::header('csrf_token')) {
return Response::json([
'message' => 'Security token doesn\'t match, possible CSRF attack.'
], 418);
}
});
// Route for authentication
Route::group(['prefix' => 'api/auth', 'after' => 'allowOrigin'], function() {
Route::get('check', [
'as'=>'check_auth_path',
'uses'=>'SessionsController@check'
]);
Route::post('login', [
'as'=>'login_path',
'uses'=>'SessionsController@login'
]);
Route::get('sentryLogout', [
'as'=>'logout_path',
'uses'=>'SessionsController@logout'
]);
});
会话控制器:
class SessionsController extends \BaseController {
public function __construct() {
$this->beforeFilter('serviceCSRF');
}
...
app.js:
var xhReq = new XMLHttpRequest();
xhReq.open("GET", "//" + window.location.hostname + "/api/csrf", false);
xhReq.send(null);
app.constant("CSRF_TOKEN", xhReq.responseText);
app.run(function ($window, $couchPotato, $rootScope, $state, $stateParams, $http, CSRF_TOKEN) {
app.lazy = $couchPotato;
$rootScope.$state = $state;
$rootScope.$stateParams = $stateParams;
// editableOptions.theme = 'bs3';
$http.defaults.headers.common['csrf_token'] = CSRF_TOKEN;
// watch for location path change
$rootScope.$on("$locationChangeStart",function() {
....
答案 0 :(得分:0)
解决。原因是Request :: header();无法用下划线解释参数(csrf_token)。所以在我改为
之后$http.defaults.headers.common['csrfToken'] = CSRF_TOKEN;
它有效!
感谢来自(https://github.com/laravel/framework/issues/1655#issuecomment-20595277)
的rvs1977