jwt
composer require firebase/php-jwt
use Firebase\JWT\JWT;
public function hello($name = 'ThinkPHP5')
{
$secret_key = "YOUR_SECRET_KEY";
$issuer_claim = "THE_ISSUER";
$audience_claim = "THE_AUDIENCE";
$issuedat_claim = time(); // issued at
$notbefore_claim = $issuedat_claim + 10; //not before in seconds
$expire_claim = $issuedat_claim + 60; // expire time in seconds
$token = array(
"iss" => $issuer_claim,
"aud" => $audience_claim,
"iat" => $issuedat_claim,
"nbf" => $notbefore_claim,
"exp" => $expire_claim,
"data" => array(
"user_id" => "1",
"user_name" => "John Doe",
)
);
$jwt = JWT::encode($token, $secret_key);
echo $jwt;
// return 'hello,' . $name;
}
上面的
$jwt = JWT::encode($token, $secret_key);
$jwt = JWT::encode($token, $secret_key, "HS256");
use Firebase\JWT\JWT;
use Firebase\JWT\Key;
$jwt = JWT::encode($payload, $key, 'HS256');
//解析
$decoded = JWT::decode($jwt, new Key($key, 'HS256'));