【发布时间】:2015-01-27 02:49:57
【问题描述】:
我正在关注这篇文章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() {
....
【问题讨论】:
-
伙计们,为什么所有的反对票都投了?
-
状态码是个愚人节玩笑...
-
我知道。问题是为什么它适用于本地而不适用于生产?
-
你被否决了,因为你没有提供你的代码,没有想要的结果,并且向我们提供了一个虚假的错误......
-
@Richard:对不起,刚刚编辑并添加了我的代码。
标签: php angularjs laravel-4 csrf csrf-protection