我有一个laravel后端和一个关于react / redux的网站。我试图使用superagent-bluebird-promis e从我的前端传递我的JWT令牌,但我一直在
否'访问控制 - 允许 - 来源'标头出现在请求的资源上
错误。我看过每一篇文章,但无法弄清楚。 这是我前端的代码:
import request from 'superagent-bluebird-promise'
......
requestPromise= request.get(API_URL + action.url);
if(action.token)
requestPromise.set("Authorization","Bearer "+ action.token).withCredentials();
requestPromise.query({ // add query (if get)
...action.data,
});
在我的laravel上,我有这个.htaccess
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule .* - [e=HTTP_AUTHORIZATION:%1]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
我还有一个CORS中间件允许这个标题,它从来没有到这里运行此代码:
public function handle($request, Closure $next)
{
$origin = 'https://mywebsite.com';
return $next($request)
->header('Access-Control-Allow-Origin', $origin)
->header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS')
->header('Access-Control-Allow-Headers','Authorization')
->header('Access-Control-Allow-Credentials','true');
;
}
我还在docker / nginx设置中运行我的laravel如果有帮助
答案 0 :(得分:0)
我通过将我的cors中间件添加到kernel.php
中的全局中间件来修复此问题 protected $middleware = [
\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
\App\Http\Middleware\Cors::class,
];