Laravel跨域问题

方法一
在app>Http>Middleware添加

<?php
namespace App\Http\Middleware;
use Closure;
class EnableCrossRequestMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $response = $next($request);
        $response->header('Access-Control-Allow-Origin', '*');//允许所有资源跨域
        $response->header('Access-Control-Allow-Headers', 'Origin, Content-Type, Cookie, Accept');//允许通过的响应报头
        $response->header('Access-Control-Allow-Methods', 'GET, POST, PATCH, PUT, OPTIONS');//允许的请求方法
        $response->header('Access-Control-Allow-Credentials', 'false');//运行客户端携带证书式访问
        return $response;
        // return $next($request);
    }
}

方法二
使用fruitcake/laravel-cors:Adds CORS (Cross-Origin Resource Sharing) headers support in your Laravel application
 a global overview of CORS workflow
安装fruitcake/laravel-cors

composer require fruitcake/laravel-cors

全局使用(对于所有路由)
app/Http/Kernel.php$middleware添加

protected $middleware = [
    // ...
    \Fruitcake\Cors\HandleCors::class,
];

config/cors.php中添加

'paths' => ['api/*'],

如果要精确设置请求白名单,则必须将白名单包含进allowed_methods所对应的数组

Note: If you are explicitly whitelisting headers, you must include Origin or requests will fail to be recognized as CORS.

<?php
return [
    /*
     * You can enable CORS for 1 or multiple paths.
     * Example: ['api/*']
     */
    'paths' => [],
    /*
    * Matches the request method. `[*]` allows all methods.
    */
    'allowed_methods' => ['*'],
    /*
     * Matches the request origin. `[*]` allows all origins.
     */
    'allowed_origins' => ['*'],
    /*
     * Matches the request origin with, similar to `Request::is()`
     */
    'allowed_origins_patterns' => [],
    /*
     * Sets the Access-Control-Allow-Headers response header. `[*]` allows all headers.
     */
    'allowed_headers' => ['*'],

    /*
     * Sets the Access-Control-Expose-Headers response header.
     */
    'exposed_headers' => false,
    /*
     * Sets the Access-Control-Max-Age response header.
     */
    'max_age' => false,
    /*
     * Sets the Access-Control-Allow-Credentials header.
     */
    'supports_credentials' => false,
];

allowed_origins allowed_headersallowed_methods可以设置成[’*’] 来允许所有值

Note: When using custom headers, like X-Auth-Token or
X-Requested-With, you must set the allowed_headers to include those headers. You can also set it to [’*’] to allow all custom headers.
Note: Because of http method overriding in Laravel, allowing POST methods will also enable the API users to perform PUT and DELETE requests as well.

参考:
https://packagist.org/packages/fruitcake/laravel-cors
https://segmentfault.com/a/1190000021891566

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值