<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                ### 安裝擴展庫 composer require lindelius/php-jwt ### 添加中間件 就叫 Jwt.php 吧 代碼大致如下 ~~~ <?php declare(strict_types=1); namespace App\Middleware; use Hyperf\HttpServer\Contract\RequestInterface; use Hyperf\HttpServer\Contract\ResponseInterface as HttpResponse; use Hyperf\Utils\Context; use Lindelius\JWT\StandardJWT; use Psr\Container\ContainerInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\MiddlewareInterface; use Psr\Http\Server\RequestHandlerInterface; class Jwt implements MiddlewareInterface { /** * @var ContainerInterface */ protected $container; protected $response; protected $request; public function __construct(ContainerInterface $container, HttpResponse $response, RequestInterface $request) { $this->container = $container; $this->response = $response; $this->request = $request; } public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface { $headers = $request->getHeaders(); if (empty($headers['token'])) { return $this->response->json( [ 'code' => 0, 'data' => [ 'error' => 'token不能為空', ], ] )->withStatus(401); } // 驗證token $token = $headers['token'][0]; $jwt = new StandardJWT(); try { $decodedJwt = $jwt::decode($token); $payload = $decodedJwt->getPayload(); $userId = $payload['sub']; $request = $request->withAttribute('userid', $userId); // 添加屬性 Context::set(ServerRequestInterface::class, $request); // 設置屬性 } catch (\Exception $exception) { return $this->response->json( [ 'code' => $exception->getCode(), 'data' => [ 'error' => $exception->getMessage() ], ] )->withStatus(401); } return $handler->handle($request); } } ~~~ ### 控制器中注入jwt中間件 ~~~ <?php namespace App\Controller; use Hyperf\HttpServer\Annotation\AutoController; use Hyperf\HttpServer\Annotation\Middleware; use Lindelius\JWT\StandardJWT; use App\Middleware\Jwt; /** * @AutoController() * @Middleware(Jwt::class) */ class Home extends AbstractController { protected $userid; public function index() { $arr = $this->request->getAttributes(); // 獲取屬性 return $arr['userid']; } } ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看