swoft-cloud / swoft

🚀 PHP Microservice Full Coroutine Framework
https://swoft.org
Apache License 2.0
5.58k stars 788 forks source link

中间件方式解决cors的问题。 #168

Closed rondaful closed 6 years ago

rondaful commented 6 years ago

base.php 有配,下面是middleWare.类。 为什么请求没有带上 Access-Control-Allow-Origin Access-Control-Allow-Methods 这些header

<?php
/**
 * This file is part of Swoft.
 *
 * @link https://swoft.org
 * @document https://doc.swoft.org
 * @contact group@swoft.org
 * @license https://github.com/swoft-cloud/swoft/blob/master/LICENSE
 */

namespace App\Middlewares;

use Psr\Http\Server\RequestHandlerInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Swoft\Bean\Annotation\Bean;
use Swoft\Core\RequestContext;
use Swoft\Http\Message\Middleware\MiddlewareInterface;
use Swoft\Http\Message\Server\Response;
use Swoft\Http\Message\Stream\SwooleStream;

/**
 * @Bean()
 * @uses      ActionTestMiddleware
 * @version   2017年11月16日
 * @author    huangzhhui <huangzhwork@gmail.com>
 * @copyright Copyright 2010-2017 Swoft software
 * @license   PHP Version 7.x {@link http://www.php.net/license/3_0.txt}
 */
class CorsMiddleware implements MiddlewareInterface
{

    /**
     * Process an incoming server request and return a response, optionally delegating
     * response creation to a handler.
     *
     * @param \Psr\Http\Message\ServerRequestInterface $request
     * @param \Psr\Http\Server\RequestHandlerInterface $handler
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \InvalidArgumentException
     */
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
    {
        switch (strtoupper($request->getMethod())){
            case 'OPTIONS':
                $response = RequestContext::getResponse();
                $response->withBody(new SwooleStream("HTTP/1.1 204 No Content"));
                $response->withStatus( 203);
                var_dump("CorsMiddleware");
                break;
            default:
                $response = $handler->handle($request);
        }
        $response->withHeader('Access-Control-Allow-Origin','*');
        $response->withHeader('Access-Control-Allow-Credentials','true');
        $response->withAddedHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS, HEAD,PATCH');
        $response->withAddedHeader('Access-Control-Allow-Headers', 'Authorization,Content-Type,Accept,Origin,User-Agent,DNT,Cache-Control,X-Mx-ReqToken,Keep-Alive,X-Requested-With,If-Modified-Since,Captcha,Lang');
        return $response;
    }
}
rondaful commented 6 years ago

我想直接修改response的header值,让它后面一直有效下去。

rondaful commented 6 years ago
$response = $response->withAddedHeader('Access-Control-Allow-Origin','*');
        $response = $response->withAddedHeader('Access-Control-Allow-Credentials','true');
        $response = $response->withAddedHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS, HEAD,PATCH');
        $response = $response->withAddedHeader('Access-Control-Allow-Headers', 'Authorization,Content-Type,Accept,Origin,User-Agent,DNT,Cache-Control,X-Mx-ReqToken,Keep-Alive,X-Requested-With,If-Modified-Since,Captcha,Lang');

这样子解决了,但合适吗???

inhere commented 6 years ago

可以写链式调用

$response = $response->withHeader('Access-Control-Allow-Origin','*')
        ->withHeader('Access-Control-Allow-Credentials','true')
        ->withAddedHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS, HEAD,PATCH')
        ->withAddedHeader('Access-Control-Allow-Headers', 'Authorization,Content-Type,Accept,Origin,User-Agent,DNT,Cache-Control,X-Mx-ReqToken,Keep-Alive,X-Requested-With,If-Modified-Since,Captcha,Lang');

return $response;