public static function create($claimsKey, $claimsValue = null) { $claims = Claims::set($claimsKey, $claimsValue); // 权利声明 $key = config('jwt.key'); // 密钥 $alg = config('jwt.alg') ? config('jwt.alg') : 'HS256'; // 算法 $token = JWT::encode($claims, $key, $alg); // 编码 return $token; }
public static function get($token = null) { $token = is_null($token) ? Token::find() : $token; $key = config('jwt.key'); // 密钥 $alg = config('jwt.alg') ? config('jwt.alg') : 'HS256'; // 算法 if ($token) { try { if ($claims = JWT::decode($token, $key, array($alg))) { return $claims; } } catch (\InvalidArgumentException $e) { data()->setErr('token', '服务器设置错误: ' . $e->getMessage())->status(500); } catch (\Exception $e) { data()->setErr('token', '无效的TOKEN: ' . $e->getMessage())->status(403); } } return null; }
/** * Encode a PHP object into a JSON string. * * @param object|array $input A PHP object or array * * @return string JSON representation of the PHP object or array * * @throws DomainException Provided object could not be encoded to valid JSON */ public static function jsonEncode($input) { $json = json_encode($input); if (function_exists('json_last_error') && ($errno = json_last_error())) { JWT::handleJsonError($errno); } elseif ($json === 'null' && $input !== null) { throw new DomainException('Null result with non-null input'); } return $json; }