Skip to content

Commit

Permalink
init commit
Browse files Browse the repository at this point in the history
  • Loading branch information
NHZEX committed Apr 9, 2020
0 parents commit 8c1c441
Show file tree
Hide file tree
Showing 8 changed files with 519 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/.idea
/vendor
/composer.lock
4 changes: 4 additions & 0 deletions README.md
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)
50 changes: 50 additions & 0 deletions composer.json
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
}
}
17 changes: 17 additions & 0 deletions config/cros.php
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,
];
59 changes: 59 additions & 0 deletions src/CorsMiddleware.php
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;
}
}
Loading

0 comments on commit 8c1c441

Please sign in to comment.