AetherUploadCORS.php 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. <?php
  2. namespace App\Http\Middleware;
  3. use Closure;
  4. use AetherUpload\ConfigMapper;
  5. class AetherUploadCORS
  6. {
  7. /**
  8. * Handle an incoming request.
  9. *
  10. * @param \Illuminate\Http\Request $request
  11. * @param \Closure $next
  12. * @return mixed
  13. */
  14. public function handle($request, Closure $next)
  15. {
  16. $response = $next($request);
  17. $origin = $request->server('HTTP_ORIGIN') ?: '';
  18. if ( in_array($origin, ConfigMapper::get('distributed_deployment_allow_origin')) ) {
  19. $response->header('Access-Control-Allow-Origin', $origin); # 允许的来源域名
  20. $response->header('Access-Control-Allow-Headers', 'X-CSRF-TOKEN'); # 允许的请求头部字段
  21. $response->header('Access-Control-Allow-Methods', 'POST, OPTIONS'); # 允许的请求方法
  22. $response->header('Access-Control-Allow-Credentials', 'true'); # 是否允许携带cookie
  23. $response->header('Access-Control-Max-Age', '3600'); # 预检请求缓存时间
  24. # 添加其它自定义内容
  25. }
  26. return $response;
  27. }
  28. }