feat : Proxy payment
This commit is contained in:
53
app/Aspect/Test/Proxy/SecurityAspect.php
Normal file
53
app/Aspect/Test/Proxy/SecurityAspect.php
Normal file
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
namespace App\Aspect\Test\Proxy;
|
||||
|
||||
use App\Service\Test\Proxy\Payment\PaymentService;
|
||||
use Hyperf\Di\Annotation\Aspect;
|
||||
use Hyperf\Di\Annotation\Inject;
|
||||
use Hyperf\Di\Aop\AbstractAspect;
|
||||
use Hyperf\Di\Aop\ProceedingJoinPoint;
|
||||
use Hyperf\Di\Exception\Exception;
|
||||
use Hyperf\HttpServer\Request;
|
||||
use RuntimeException;
|
||||
|
||||
#[Aspect]
|
||||
class SecurityAspect extends AbstractAspect
|
||||
{
|
||||
/**
|
||||
* @var Request
|
||||
*/
|
||||
#[Inject]
|
||||
protected Request $request;
|
||||
|
||||
/**
|
||||
* @var array|string[]
|
||||
*/
|
||||
public array $classes = [
|
||||
PaymentService::class , '::pay',
|
||||
];
|
||||
|
||||
/**
|
||||
* 安全验证切面
|
||||
* @param ProceedingJoinPoint $proceedingJoinPoint
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
public function process(ProceedingJoinPoint $proceedingJoinPoint): mixed
|
||||
{
|
||||
$token = $this->request->header('Authorization');
|
||||
|
||||
if (!$this->validateToken($token)) throw new RuntimeException('Unauthorized');
|
||||
|
||||
return $proceedingJoinPoint->process();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string|null $token
|
||||
* @return bool
|
||||
*/
|
||||
private function validateToken(?string $token): bool
|
||||
{
|
||||
return $token !== 'valid-token';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user