JwtAuth.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. /**
  3. *
  4. *User:Administrator
  5. *Date:2021/9/29
  6. */
  7. namespace app\common\lib\auth;
  8. use Lcobucci\JWT\Parser;
  9. use Lcobucci\JWT\Builder;
  10. use Lcobucci\JWT\Signer\Hmac\Sha256;
  11. use Lcobucci\JWT\Token;
  12. use Lcobucci\JWT\ValidationData;
  13. /**
  14. * * 单例 一次请求中所有出现使用jwt的地方都是一个用户
  15. */
  16. class JwtAuth
  17. {
  18. private $qydm;//企业代码
  19. private $rybh;//人员编号
  20. private $rymc;//人员名称
  21. private $groupname;//组名
  22. private $uid;//t_ry表的id
  23. private $token;
  24. private $decodeToken;
  25. /**
  26. * 单例模式 jwtAuth句柄
  27. * @var
  28. */
  29. private static $instance;
  30. /**
  31. * 获取jwtAuth的句柄
  32. * @return JwtAuth
  33. */
  34. public static function getInstance()
  35. {
  36. if (is_null(self::$instance)) {
  37. self::$instance = new self();
  38. }
  39. return self::$instance;
  40. }
  41. /**
  42. * 私有化构造函数 防止new
  43. */
  44. private function __construct(){}
  45. /**
  46. * 私有化 clone函数 防止clone
  47. */
  48. private function __clone(){}
  49. //Token
  50. public function getToken()
  51. {
  52. return (string)$this->token;
  53. }
  54. public function setToken($token)
  55. {
  56. $this->token=$token;
  57. return $this;
  58. }
  59. //Uid
  60. public function getUid()
  61. {
  62. return (string)$this->uid;
  63. }
  64. public function setUid($uid)
  65. {
  66. $this->uid=$uid;
  67. return $this;
  68. }
  69. //人员编号
  70. public function setRybh($rybh)
  71. {
  72. $this->rybh=$rybh;
  73. return $this;
  74. }
  75. public function getRybh()
  76. {
  77. return $this->rybh;
  78. }
  79. //$groupname
  80. public function setGroupname($groupname)
  81. {
  82. $this->groupname=$groupname;
  83. return $this;
  84. }
  85. public function getGroupname()
  86. {
  87. return $this->groupname;
  88. }
  89. //name
  90. public function setName($name)
  91. {
  92. $this->name=$name;
  93. return $this;
  94. }
  95. public function getName()
  96. {
  97. return $this->name;
  98. }
  99. //人员名称
  100. public function setRymc($rymc)
  101. {
  102. $this->rymc=$rymc;
  103. return $this;
  104. }
  105. public function getRymc()
  106. {
  107. return $this->rymc;
  108. }
  109. //企业代码
  110. public function setQydm($qydm)
  111. {
  112. $this->qydm=$qydm;
  113. return $this;
  114. }
  115. public function getQydm()
  116. {
  117. return $this->qydm;
  118. }
  119. /**
  120. * @return $this
  121. */
  122. public function encode(): JwtAuth
  123. {
  124. //设置签发人、接收人、唯一标识、签发时间、立即生效、过期时间、用户id、签名
  125. $time=time();
  126. $this->token = (new Builder())
  127. ->setHeader('alg', 'HS256')
  128. ->setIssuer(config('jwt.issuer'))
  129. ->setAudience(config('jwt.audience'))
  130. ->setExpiration($time + config('jwt.expiration'))
  131. ->set('uid',$this->uid)
  132. ->set('name', $this->name)
  133. ->set('rymc', $this->rymc)
  134. ->set('groupname', $this->groupname)
  135. ->set('rybh',$this->rybh)
  136. ->set('qydm',$this->qydm)
  137. ->sign(new Sha256(),config('jwt.secret'))
  138. ->getToken();
  139. return $this;
  140. }
  141. /**
  142. * @return Token|void
  143. */
  144. public function decode()
  145. {
  146. if (!$this->decodeToken) {
  147. $this->decodeToken = (new Parser())->parse((string)$this->token);
  148. $this->uid = $this->decodeToken->getClaim('uid');
  149. $this->rybh = $this->decodeToken->getClaim('rybh');
  150. $this->qydm = $this->decodeToken->getClaim('qydm');
  151. $this->name = $this->decodeToken->getClaim('name');
  152. $this->rymc = $this->decodeToken->getClaim('rymc');
  153. $this->groupname = $this->decodeToken->getClaim('groupname');
  154. }
  155. return $this->decodeToken;
  156. }
  157. /**
  158. * verify token
  159. * @return bool
  160. */
  161. public function verify(): bool
  162. {
  163. $result = $this->decode()->verify(new Sha256(), config('jwt.secret'));
  164. return $result;
  165. }
  166. /**
  167. * validate
  168. * @return bool
  169. */
  170. public function validate(): bool
  171. {
  172. $data = new ValidationData();
  173. $data->setIssuer(config('jwt.issuer'));
  174. $data->setAudience(config('jwt.audience'));
  175. return $this->decode()->validate($data);
  176. }
  177. }