zoukankan      html  css  js  c++  java
  • JWT使用

    JWT使用

    标签(空格分隔): php

    安装

    composer require lcobucci/jwt
    

    使用




    <?php
    namespace appcommonauth;
    
    use LcobucciJWTBuilder;
    use LcobucciJWTParser;
    use LcobucciJWTSignerHmacSha256;
    use LcobucciJWTValidationData;
    
    class JwtAuth
    {
    private static $instance;
    
    private $token;
    
    private $decodeToken;
    
    private $iis = 'web.tp5.cn';
    
    private $aud = 'web.tp5.cn';
    
    private $uid;
    
    private $configId;
    
    private $secret = 'web.tp5.cn';
    
    private function __construct() {}
    
    public static function getInstance()
    {
        if (!self::$instance) self::$instance = new self();
        return self::$instance;
    }
    
    private function __clone() {}
    
    public function setUid($uid)
    {
        $this->uid = $uid;
        return $this;
    }
    
    public function setConfigId($configId)
    {
        $this->configId = $configId;
        return $this;
    }
    
    public function getToken()
    {
        return (string)$this->token;
    }
    
    public function setToken($token)
    {
        $this->token = $token;
        return $this;
    }
    
    public function encode()
    {
        $time = time();
        $this->token = (new Builder())->setHeader('alg', 'HS256')
                ->setIssuedAt($this->iis)
                ->setAudience($this->aud)
                ->setIssuedAt($time)
                ->setExpiration($time + 120)
                ->set('uid', $this->uid)
                ->set('configId', $this->configId)
                ->sign(new Sha256(), $this->secret)
                ->getToken();
        return $this;
    }
    
    public function decode()
    {
        if (!$this->decodeToken) {
            $this->decodeToken = (new Parser())->parse((string) $this->token);
            $this->uid = $this->decodeToken->getClaim('uid');
            $this->configId = $this->decodeToken->getClaim('configId');
        }
        return $this->decodeToken;
    }
    
    public function verify()
    {
        return $this->decode()->verify(new Sha256(), $this->secret);
    }
    
    public function validate()
    {
        $data = new ValidationData();
        $data->setIssuer($this->iis);
        $data->setAudience($this->aud);
        return $this->decode()->validate($data);
    }
  • 相关阅读:
    1月19号 UIImageView
    1月18号 UILabel 加上导入.tff格式的字体
    1月18号 UIButton
    2016年 1月15号 cocoapods的导入
    1月12号 UIView
    12月30号 iOS程序准备
    12月29号 计算器(包含混合运算)
    2016.01.13 代理设计模式
    2016.01.04 视图控制器UIViewController
    2015.12.31 iOS程序准备(developer.apple.com)
  • 原文地址:https://www.cnblogs.com/yanweifeng/p/10943705.html
Copyright © 2011-2022 走看看