2020-11-03 17:12:57 +08:00
|
|
|
<?php
|
|
|
|
declare(strict_types=1);
|
2021-09-04 20:18:20 +08:00
|
|
|
|
2020-12-26 21:33:40 +08:00
|
|
|
/**
|
|
|
|
* This is my open source code, please do not use it for commercial applications.
|
|
|
|
* For the full copyright and license information,
|
|
|
|
* please view the LICENSE file that was distributed with this source code
|
|
|
|
*
|
|
|
|
* @author Yuandong<837215079@qq.com>
|
|
|
|
* @link https://github.com/gzydong/hyperf-chat
|
|
|
|
*/
|
2020-11-03 17:12:57 +08:00
|
|
|
|
|
|
|
namespace App\Middleware;
|
|
|
|
|
|
|
|
use Hyperf\Utils\Context;
|
|
|
|
use Psr\Http\Message\ResponseInterface;
|
|
|
|
use Psr\Http\Message\ServerRequestInterface;
|
|
|
|
use Psr\Http\Server\MiddlewareInterface;
|
|
|
|
use Psr\Http\Server\RequestHandlerInterface;
|
|
|
|
|
|
|
|
class CorsMiddleware implements MiddlewareInterface
|
|
|
|
{
|
|
|
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
|
|
|
|
{
|
|
|
|
$response = Context::get(ResponseInterface::class);
|
|
|
|
$response = $response->withHeader('Access-Control-Allow-Origin', '*')
|
|
|
|
->withHeader('Access-Control-Allow-Credentials', 'true')
|
|
|
|
->withHeader('Access-Control-Allow-Headers', 'DNT,Keep-Alive,User-Agent,Cache-Control,Content-Type,Authorization');
|
|
|
|
|
|
|
|
Context::set(ResponseInterface::class, $response);
|
|
|
|
|
2021-06-10 22:49:38 +08:00
|
|
|
if ($request->getMethod() == 'OPTIONS' || $request->getUri()->getPath() == '/favicon.ico') {
|
2020-11-03 17:12:57 +08:00
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $handler->handle($request);
|
|
|
|
}
|
|
|
|
}
|