WebSocketAuthMiddleware.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This is my open source code, please do not use it for commercial applications.
  5. * For the full copyright and license information,
  6. * please view the LICENSE file that was distributed with this source code
  7. *
  8. * @author Yuandong<837215079@qq.com>
  9. * @link https://github.com/gzydong/hyperf-chat
  10. */
  11. namespace App\Middleware;
  12. use Hyperf\Di\Annotation\Inject;
  13. use Psr\Container\ContainerInterface;
  14. use Psr\Http\Message\ResponseInterface;
  15. use Psr\Http\Server\MiddlewareInterface;
  16. use Psr\Http\Message\ServerRequestInterface;
  17. use Psr\Http\Server\RequestHandlerInterface;
  18. /**
  19. * WebSocket token 授权验证中间件
  20. *
  21. * @package App\Middleware
  22. */
  23. class WebSocketAuthMiddleware implements MiddlewareInterface
  24. {
  25. /**
  26. * @var ContainerInterface
  27. */
  28. protected $container;
  29. /**
  30. * 授权验证守卫
  31. *
  32. * @var string
  33. */
  34. private $guard = 'jwt';
  35. public function __construct(ContainerInterface $container)
  36. {
  37. $this->container = $container;
  38. }
  39. public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
  40. {
  41. // 授权验证拦截握手请求并实现权限检查
  42. // if (auth($this->guard)->guest()) {
  43. // return $this->container->get(\Hyperf\HttpServer\Contract\ResponseInterface::class)->raw('Forbidden');
  44. // }
  45. return $handler->handle($request);
  46. }
  47. }