55 lines
1.4 KiB
PHP
55 lines
1.4 KiB
PHP
<?php
|
|
/**
|
|
* This is my open source code, please do not use it for commercial applications.
|
|
* For the full copyright and license information,
|
|
* please view the LICENSE file that was distributed with this source code
|
|
*
|
|
* @author Yuandong<837215079@qq.com>
|
|
* @link https://github.com/gzydong/hyperf-chat
|
|
*/
|
|
|
|
namespace App\Middleware;
|
|
|
|
use Hyperf\HttpServer\Contract\RequestInterface;
|
|
use Hyperf\HttpServer\Contract\ResponseInterface as HttpResponse;
|
|
use Psr\Http\Message\ResponseInterface;
|
|
use Psr\Http\Message\ServerRequestInterface;
|
|
use Psr\Http\Server\MiddlewareInterface;
|
|
use Psr\Http\Server\RequestHandlerInterface;
|
|
|
|
/**
|
|
* Http Token 授权验证中间件
|
|
*
|
|
* @package App\Middleware
|
|
*/
|
|
class JWTAuthMiddleware implements MiddlewareInterface
|
|
{
|
|
/**
|
|
* @var RequestInterface
|
|
*/
|
|
protected $request;
|
|
|
|
/**
|
|
* @var HttpResponse
|
|
*/
|
|
protected $response;
|
|
|
|
public function __construct(HttpResponse $response, RequestInterface $request)
|
|
{
|
|
$this->response = $response;
|
|
$this->request = $request;
|
|
}
|
|
|
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
|
|
{
|
|
if (auth('jwt')->guest()) {
|
|
return $this->response->withStatus(401)->json([
|
|
'code' => 401,
|
|
'message' => 'Token authentication does not pass !',
|
|
]);
|
|
}
|
|
|
|
return $handler->handle($request);
|
|
}
|
|
}
|