JwtAuthorizedTrait.php 825 B

12345678910111213141516171819202122232425262728293031323334353637
  1. <?php
  2. namespace app\common\controller;
  3. use app\common\exception\CatchException;
  4. use think\facade\Log;
  5. trait JwtAuthorizedTrait
  6. {
  7. /**
  8. * $payload Jwt 载荷,stdClass
  9. *
  10. * @var \stdClass
  11. */
  12. protected $payload;
  13. public function initialize()
  14. {
  15. parent::initialize();
  16. // 允许cros通过jwt所在的header
  17. $jwt = $this->request->header('Authorization');
  18. if (!$jwt) {
  19. throw new CatchException("请登录", 600);
  20. }
  21. // 解析jwt
  22. $jwt = str_replace('Bearer ', '', $jwt);
  23. Log::debug("尝试解析Jwt: " . var_export($jwt, true));
  24. $this->payload = $this->decodeJwt($jwt);
  25. // 调用用户自定义的初始化语句
  26. if (method_exists($this, 'init')) {
  27. $this->init();
  28. }
  29. }
  30. }