PHP code example of chenbool / jwt

1. Go to this page and download the library: Download chenbool/jwt library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

chenbool / jwt example snippets



use \chenbool\JWT\JWT;

$key = "example_key";
$token = array(
    "iss" => "http://example.org", //签发者 可选
    "aud" => "http://example.com", //接收该JWT的一方 可选
    "iat" => 1356999524, //签发时间
    "nbf" => time()+30, //(Not Before):某个时间点后才能访问,比如设置time()+30,表示当前时间30秒后才能使用
    "exp" => time()=7200,  //过期时间,这里设置2个小时
);

/**
 * 加密
 * https://tools.ietf.org/html/draft-ietf-jose-json-web-algorithms-40
 */
$jwt = JWT::encode($token, $key);
$decoded = JWT::decode($jwt, $key, array('HS256'));
$decoded_array = (array) $decoded;
print_r($decoded);



/**
 * 解密/验证
 * Source: http://self-issued.info/docs/draft-ietf-oauth-json-web-token.html#nbfDef
 */
try {
    JWT::$leeway = 60; //当前时间减去60,把时间留点余地
    $decoded = JWT::decode($jwt, $key, array('HS256')); // /key要和签发一致 HS256方式要和签发一致
    $arr = (array)$decoded;
    print_r($arr);
}catch(Exception $e) {  
    echo $e->getMessage();  //错误提示
}

/**
 * 提示: 在src目录下的JWT.php中的方法 decode中,可以看到是通过多个 自定义 throw new 抛出异常的
 * 所以我们可以根据不同的throw new设置多个catch来捕获
 */
try {
    JWT::$leeway = 60; 
    $decoded = JWT::decode($jwt, $key, array('HS256')); 
} catch(\chenbool\JWT\SignatureInvalidException $e) {  
    echo $e->getMessage(); //签名不正确
}catch(\chenbool\JWT\BeforeValidException $e) {  
    echo $e->getMessage(); // 签名在某个时间点之后才能用
}catch(\chenbool\JWT\ExpiredException $e) {  
    echo $e->getMessage();  // token过期
}catch(Exception $e) {  
    //其他错误
    echo $e->getMessage();
}




use \chenbool\JWT\JWT;

$privateKey = <<<EOD
-----BEGIN RSA PRIVATE KEY-----
MIICXAIBAAKBgQC8kGa1pSjbS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QIDAQAB
-----END PUBLIC KEY-----
EOD;

$token = array(
    "iss" => "example.org", //签发者 可选
    "aud" => "example.com", //接收该JWT的一方 可选
    "iat" => 1356999524,    //签发时间
    "nbf" => 1357000000 //(Not Before):某个时间点后才能访问,比如设置time()+30,表示当前时间30秒后才能使用
);

$jwt = JWT::encode($token, $privateKey, 'RS256');
echo "加密:\n" . print_r($jwt, true) . "\n";

$decoded = JWT::decode($jwt, $publicKey, array('RS256'));
$decoded_array = (array) $decoded;
echo "解密:\n" . print_r($decoded_array, true) . "\n";