new_naweigete/app/http/middleware/AllowOriginMiddleware.php
2025-03-12 10:47:34 +08:00

56 lines
1.8 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------
namespace app\http\middleware;
use app\Request;
use crmeb\interfaces\MiddlewareInterface;
use think\facade\Config;
use think\Response;
/**
* 跨域中间件
* Class AllowOriginMiddleware
* @package app\http\middleware
*/
class AllowOriginMiddleware implements MiddlewareInterface
{
/**
* 允许跨域的域名
* @var string
*/
protected $cookieDomain;
/**
* @param Request $request
* @param \Closure $next
* @return Response
*/
public function handle(Request $request, \Closure $next)
{
$this->cookieDomain = Config::get('cookie.domain', '');
$header = Config::get('cookie.header');
$origin = $request->header('origin');
if ($origin && ('' == $this->cookieDomain || strpos($origin, $this->cookieDomain)))
$header['Access-Control-Allow-Origin'] = $origin;
if ($request->method(true) == 'OPTIONS') {
$response = Response::create('ok')->code(200)->header($header);
} else {
$response = $next($request)->header($header);
}
// $request->filter(['strip_tags', 'addslashes', 'trim']);
return $response;
}
}