更新
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
namespace app\service;
|
||||
|
||||
final class HttpMiddleware
|
||||
{
|
||||
public function handle($request, \Closure $next)
|
||||
{
|
||||
$headers = ['Cache-Control' => 'no-store', 'X-Content-Type-Options' => 'nosniff',
|
||||
'Referrer-Policy' => 'no-referrer', 'Vary' => 'Origin'];
|
||||
$origin = (string) $request->header('origin', '');
|
||||
$allowed = array_filter(array_map('trim', explode(',', Settings::get('IAM_CORS_ORIGINS', 'http://localhost:5173,http://127.0.0.1:5173'))));
|
||||
if (\app\service\group\GroupService::enabled()) {
|
||||
$hospitalOrigin = \app\service\group\GroupService::hospitalOrigin();
|
||||
if ($hospitalOrigin !== '') { $allowed[] = $hospitalOrigin; }
|
||||
}
|
||||
if ($origin !== '') {
|
||||
$sameOrigin = rtrim((string) $request->domain(), '/') === $origin;
|
||||
if (!$sameOrigin && !in_array($origin, $allowed, true)) {
|
||||
return json(['code' => 0, 'msg' => '请求来源不允许', 'data' => (object) []], 403)->header($headers);
|
||||
}
|
||||
$headers += ['Access-Control-Allow-Origin' => $origin,
|
||||
'Access-Control-Allow-Headers' => 'Authorization,Content-Type,X-Company-Id',
|
||||
'Access-Control-Allow-Methods' => 'GET,POST,OPTIONS', 'Access-Control-Max-Age' => '600'];
|
||||
}
|
||||
// Send before routing too, so the global exception handler preserves CORS on errors.
|
||||
foreach ($headers as $key => $value) { header($key . ': ' . $value); }
|
||||
if ($request->isOptions()) { return response('', 204)->header($headers); }
|
||||
return $next($request)->header($headers);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user