-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 8c1c441
Showing
8 changed files
with
519 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
/.idea | ||
/vendor | ||
/composer.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Think-Cors | ||
|
||
## 代码引用 | ||
- [asm89/stack-cors](https://github.com/asm89/stack-cors) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
{ | ||
"name": "nhzex/think-cors", | ||
"type": "library", | ||
"description": "", | ||
"keywords": [ | ||
"thinkphp6", | ||
"thinkphp", | ||
"cors" | ||
], | ||
"license": "Apache-2.0", | ||
"authors": [ | ||
{ | ||
"name": "auooru", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"require": { | ||
"php": "^7.1", | ||
"topthink/framework": "^6.0" | ||
}, | ||
"require-dev": { | ||
"phpunit/phpunit": "^8|^7|^9", | ||
"symfony/var-dumper": "^5.0|^4.0" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"HZEX\\Think\\Cors\\": "src" | ||
}, | ||
"files": [ | ||
"src/helper.php" | ||
] | ||
}, | ||
"autoload-dev": { | ||
"psr-4": { | ||
"Test\\Think\\": "tests" | ||
} | ||
}, | ||
"extra": { | ||
"think": { | ||
"services": [ | ||
], | ||
"config": { | ||
"cors": "config/cors.php" | ||
} | ||
} | ||
}, | ||
"config": { | ||
"sort-packages": true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
return [ | ||
'allowed_origins' => ['*'], | ||
|
||
'allowed_origins_patterns' => [], | ||
|
||
'allowed_methods' => ['*'], | ||
|
||
'allowed_headers' => ['*'], | ||
|
||
'exposed_headers' => [], | ||
|
||
'max_age' => 0, | ||
|
||
'supports_credentials' => false, | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace app\Service\Cors; | ||
|
||
use Closure; | ||
use think\Config; | ||
use think\Request; | ||
use think\Response; | ||
|
||
class CorsMiddleware | ||
{ | ||
/** | ||
* @var CorsService | ||
*/ | ||
protected $cors; | ||
|
||
public function __construct(Config $config) | ||
{ | ||
$conf = $config->get('cros', []); | ||
|
||
$this->cors = new CorsService( | ||
$conf['allowed_origins'] ?? [], | ||
$conf['allowed_origins_patterns'] ?? [], | ||
$conf['allowed_methods'] ?? [], | ||
$conf['allowed_headers'] ?? [], | ||
$conf['exposed_headers'] ?? [], | ||
$conf['supports_credentials'] ?? false, | ||
$conf['max_age'] ?? 0 | ||
); | ||
} | ||
|
||
/** | ||
* 允许跨域请求 | ||
* @access public | ||
* @param Request $request | ||
* @param Closure $next | ||
* @return Response | ||
*/ | ||
public function handle(Request $request, Closure $next): Response | ||
{ | ||
if ($this->cors->isPreflightRequest($request)) { | ||
return $this->cors->handlePreflightRequest($request); | ||
} | ||
|
||
if (!$this->cors->isRequestAllowed($request)) { | ||
return Response::create('Not allowed in CORS policy.', 'html', 403); | ||
} | ||
|
||
/** @var Response $response */ | ||
$response = $next($request); | ||
|
||
if ($this->cors->isCorsRequest($request)) { | ||
$this->cors->addRequestHeaders($response, $request); | ||
} | ||
|
||
return $response; | ||
} | ||
} |
Oops, something went wrong.