中间件跨域

public function handle($request, Closure $next)
{
$response = $next($request);
$origin = $request->server(‘HTTP_ORIGIN’) ? $request->server(‘HTTP_ORIGIN’) : ‘’;
$allow_origin = [
‘http://.com’,//允许访问
‘http://
.com’,//允许访问
];
if (in_array($origin, $allow_origin)) {
$response->header(‘Access-Control-Allow-Origin’, $origin);
$response->header(‘Access-Control-Allow-Headers’, ‘Origin, Content-Type, Cookie, X-CSRF-TOKEN, Accept, Authorization, X-XSRF-TOKEN’);
$response->header(‘Access-Control-Expose-Headers’, ‘Authorization, authenticated’);
$response->header(‘Access-Control-Allow-Methods’, ‘GET, POST, PATCH, PUT, OPTIONS’);
$response->header(‘Access-Control-Allow-Credentials’, ‘true’);
}
return $response;
}

protected $middleware = [
// more
App\Http\Middleware\EnableCrossRequestMiddleware::class,
];
在 App\Http\Kernel 类的 $middleware 属性添加,这里注册的中间件属于全局中间件。

接口频率限制
Route::group([‘prefix’=>’api’,’middleware’=>’throttle:5’],function(){
Route::get(‘users’,function(){
return \App\User::all();
});
});//频次上限5

《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
讨论数量: 1

......laravel7以后自带跨域中间件的

1年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!