fix 跨域问题
This commit is contained in:
parent
bc889b918a
commit
62d6c58f85
26
app/middleware/Cros.php
Normal file
26
app/middleware/Cros.php
Normal file
@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace app\middleware;
|
||||
|
||||
use Webman\MiddlewareInterface;
|
||||
use Webman\Http\Response;
|
||||
use Webman\Http\Request;
|
||||
|
||||
class Cros implements MiddlewareInterface
|
||||
{
|
||||
public function process(Request $request, callable $handler) : Response
|
||||
{
|
||||
// 如果是options请求则返回一个空响应,否则继续向洋葱芯穿越,并得到一个响应
|
||||
$response = $request->method() == 'OPTIONS' ? response('') : $handler($request);
|
||||
|
||||
// 给响应添加跨域相关的http头
|
||||
$response->withHeaders([
|
||||
'Access-Control-Allow-Credentials' => 'true',
|
||||
'Access-Control-Allow-Origin' => $request->header('origin', '*'),
|
||||
'Access-Control-Allow-Methods' => $request->header('access-control-request-method', '*'),
|
||||
'Access-Control-Allow-Headers' => $request->header('access-control-request-headers', '*'),
|
||||
]);
|
||||
|
||||
return $response;
|
||||
}
|
||||
}
|
@ -6,10 +6,18 @@
|
||||
* For full copyright and license information, please see the MIT-LICENSE.txt
|
||||
* Redistributions of files must retain the above copyright notice.
|
||||
*
|
||||
* @author walkor<walkor@workerman.net>
|
||||
* @author walkor<walkor@workerman.net>p
|
||||
* @copyright walkor<walkor@workerman.net>
|
||||
* @link http://www.workerman.net/
|
||||
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
||||
*/
|
||||
|
||||
return [];
|
||||
|
||||
return [
|
||||
// 全局中间件
|
||||
'' => [
|
||||
// ... 这里省略其它中间件
|
||||
// app\middleware\Cros::class,
|
||||
]
|
||||
|
||||
];
|
@ -16,6 +16,7 @@
|
||||
use app\controller\AdController;
|
||||
use app\controller\OAuthController;
|
||||
use app\controller\GoogleAdsController;
|
||||
use support\Request;
|
||||
use Webman\Route;
|
||||
|
||||
|
||||
@ -115,6 +116,17 @@ Route::group('/googleads', function () {
|
||||
});
|
||||
});
|
||||
|
||||
Route::fallback(function (Request $request) {
|
||||
$response = strtoupper($request->method()) === 'OPTIONS' ? response('', 204) : response('', 404);
|
||||
$response->withHeaders([
|
||||
'Access-Control-Allow-Credentials' => 'true',
|
||||
'Access-Control-Allow-Origin' => "*",
|
||||
'Access-Control-Allow-Methods' => '*',
|
||||
'Access-Control-Allow-Headers' => '*',
|
||||
]);
|
||||
return $response;
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user