CSRF & CORS with AngularJS + Laravel

回眸只為那壹抹淺笑 提交于 2019-12-13 05:23:45

问题


I'm working on an AngularJS webapp with a Laravel backend.

I want to enable CSRF protection with cross-domain requests. Is it possible?

$http reference in "Cross Site Request Forgery" says "The header will not be set for cross-domain requests"

Looking the Developer Tools logs I see that after the $http.post call the preflight request is sent (OPTION verb) and it has the XSRF-TOKEN cookies set, but the POST request has no cookies so I can't do:

$http.defaults.headers.post['X-CSRFToken'] = $cookies['XSRF-TOKEN'];

Any idea?

UPDATE:

@zeroflagL: I tried with

$http.defaults.headers.common.xsrfCookieName = 'XSRF-TOKEN';
$http.defaults.headers.common.xsrfHeaderName = 'X-XSRF-TOKEN';

And now in the Request headers of the POST I have:

xsrfCookieName:XSRF-TOKEN
xsrfHeaderName:X-XSRF-TOKEN

But the CSRF check is not passed (TokenMismatchException on the server). I suppose that in the Request headers there should be the XSRF-TOKEN to work...


回答1:


As zeroflagL said CSRF protection can't be applied to cross domain requests.

To reply to my question: no, it's not possible.



来源:https://stackoverflow.com/questions/33676726/csrf-cors-with-angularjs-laravel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!