Home> PHP Framework> ThinkPHP> body text

ThinkPHP5 uses JWT for encryption

藏色散人
Release: 2019-08-19 14:18:06
forward
5881 people have browsed it

ThinkPHP5 uses JWT for encryption

Using Github'sfirebase\JWT

- Install this extension usingComposer- Code Example

_setJwtToken($string); throw new \Exception($jwt, 200); } catch (\Exception $e) { return json(array("code"=>$e->getCode(), "msg"=>$e->getMessage())); } } //签发token private function _setJwtToken($string=""){ $key = $this->key; $time = time(); $token = array( "iss" => "http://ml.cn", "aud" => "http://ml.cn", 'iat' => $time, //签发时间 'nbf' => $time + 10, //在什么时间之后该jwt才可用 'exp' => $time + 10, //过期时间 "string" => $string ); $jwt = JWT::encode($token, $key); return $jwt; } //解析token protected function _readJwtToken($jwt){ $key = $this->key; try { JWT::$leeway = 60;//当前时间减去60,把时间留点余地 $decoded = JWT::decode($jwt, $key, ['HS256']); //HS256方式,这里要和签发的时候对应 $arr = (array)$decoded; return json_msg(200, "success", $arr); } catch(\Firebase\JWT\SignatureInvalidException $e) { //签名不正确 return json_msg(-101, $e->getMessage()); }catch(\Firebase\JWT\BeforeValidException $e) { // 签名在某个时间点之后才能用 return json_msg(-102, $e->getMessage()); }catch(\Firebase\JWT\ExpiredException $e) { // token过期 return json_msg(-103, $e->getMessage()); }catch(Exception $e) { //其他错误 return json_msg(-104, $e->getMessage()); } } //测试解析 public function _writeJwtToken($token){ halt($this->_readJwtToken($token)); } } ?>
Copy after login

This article comes from the ThinkPHP framework technical article column:

//m.sbmmt.com/phpkj/thinkphp/

The above is the detailed content of ThinkPHP5 uses JWT for encryption. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:learnku.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!