gpt4 book ai didi

angularjs - Angular CORS 请求无法发送到 Laravel 后端,但预检看起来不错

转载 作者:行者123 更新时间:2023-12-05 01:02:27 24 4
gpt4 key购买 nike

简而言之,我使用 Angular 将数据发布到 Laravel 后端。 OPTIONS/preflight 请求看起来不错,但随后的 POST 失败,指出请求的资源中缺少 Access-Control-Allow-Origin header 。

我使用的是 Laravel 5 和 Angular 1.2.26。可以在此处找到有关后端中间件的更多文档:https://laracasts.com/discuss/channels/requests/laravel-5-cors-headers-with-filters .

Laravel 中间件:

public function handle($request, Closure $next)
{
return $next($request)->header('Access-Control-Allow-Origin' , 'http://laravel.app:8001')
->header('Access-Control-Allow-Credentials', 'true')
->header('Access-Control-Allow-Methods', 'POST, GET, OPTIONS, PUT, DELETE')
->header('Access-Control-Allow-Headers', 'Content-Type, Accept, Authorization, X-Requested-With')
->header('Access-Control-Max-Age', '28800');
}

Angular 配置 - 我已经尝试了注释代码的各种组合,每次都得到相同的结果:

$httpProvider.defaults.useXDomain = true;
//$httpProvider.defaults.withCredentials = true;
//delete $httpProvider.defaults.headers.common["X-Requested-With"];
//$httpProvider.defaults.headers.common["Accept"] = "application/json";
//$httpProvider.defaults.headers.common["Content-Type"] = "application/json";

预检/选项:

Remote Address:127.0.0.1:8000
Request URL:http://laravel.app:8000/api/v1/authentication/login
Request Method:OPTIONS
Status Code:200 OK

Request Headers
Accept:*/*
Accept-Encoding:gzip, deflate, sdch
Accept-Language:en-US,en;q=0.8
Access-Control-Request-Headers:accept, content-type
Access-Control-Request-Method:POST
Connection:keep-alive
Host:laravel.app:8000
Origin:http://laravel.app:8001
Referer:http://laravel.app:8001/
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.65 Safari/537.36

Response Headers
Access-Control-Allow-Credentials:true
Access-Control-Allow-Headers:Content-Type, Accept, Authorization, X-Requested-With
Access-Control-Allow-Methods:POST, GET, OPTIONS, PUT, DELETE
Access-Control-Allow-Origin:http://laravel.app:8001
Access-Control-Max-Age:28800
Allow:GET,HEAD,POST
Cache-Control:no-cache
Connection:keep-alive
Content-Encoding:gzip
Content-Type:text/html; charset=UTF-8
Date:Mon, 24 Nov 2014 16:01:57 GMT
Server:nginx/1.6.2
Set-Cookie:laravel_session=blahblah; expires=Mon, 24-Nov-2014 18:01:57 GMT; Max-Age=7200; path=/; httponly
Set-Cookie:XSRF-TOKEN=blahblah; expires=Thu, 01-Jan-1970 00:02:00 GMT; Max-Age=-1416844797; path=/; httponly
Transfer-Encoding:chunked

发布:

Remote Address:127.0.0.1:8000
Request URL:http://laravel.app:8000/api/v1/authentication/login
Request Method:POST
Status Code:500 Internal Server Error

Request Headers
Accept:application/json, text/plain, */*
Accept-Encoding:gzip, deflate
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Length:47
Content-Type:application/json;charset=UTF-8
Host:laravel.app:8000
Origin:http://laravel.app:8001
Referer:http://laravel.app:8001/
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.65 Safari/537.36

Request Payload
{email: "x", password: "x", rememberMe: false}
email: "x"
password: "x"
rememberMe: false

Response Headers
Cache-Control:no-cache
Connection:keep-alive
Content-Type:text/html; charset=UTF-8
Date:Mon, 24 Nov 2014 16:01:57 GMT
Server:nginx/1.6.2
Transfer-Encoding:chunked

最佳答案

在单步执行 VerifyCsrfToken 中间件后,我确定它确实是 token 不匹配。

原因是 Angular 没有通过 header 或 post 中的参数提供 CSRF token 。它适用于 GET 和 OPTIONS 请求,因为这些请求不会针对 token 进行验证。

所以,我研究了 Angular,那里有关于 XSRF 保护的文档(请参阅 https://docs.angularjs.org/api/ng/service/ $http),还有很多关于如何添加适当 header 的讨论(例如 https://github.com/angular/angular.js/issues/5122#issuecomment-36157820)。

我还没有机会跟进这一切,因为我必须继续推进我的项目,而我的特定用例允许我不用禁用 VerifyCsrfToken,因为我在开发过程中只需要 CORS。

但希望这会给其他人一个更全面地解决这个问题的起点。

关于angularjs - Angular CORS 请求无法发送到 Laravel 后端,但预检看起来不错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27109226/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com