JwtAuth.php 4.3 KB

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