ociomercado/laravel-jwt

一个简单的Laravel包,使用lcobucci/jwt库实现JWT的Provider、Middleware和Facade,用于生成和验证令牌。

v0.4.2 2017-02-23 01:10 UTC

This package is not auto-updated.

Last update: 2024-09-28 20:25:25 UTC


README

一个简单的Laravel包,实现一个ProviderMiddlewareFacade用于JWT,使用lcobucci/jwt库生成和验证令牌。

依赖关系

此库需要

  • PHP 5.5+
  • OpenSSL扩展

安装

使用composer

composer require ociomercado/laravel-jwt

配置

Provider

您需要更新您的config/app.php文件,并在providers部分添加以下代码

  'providers' => [
    // Other providers

      OcioMercado\LaravelJWT\JWTServiceProvider::class,

    // Other providers
  ]

别名

此外,您还需要在aliases部分添加以下内容

  'aliases' => [
    // Other aliases

      'JWT' => OcioMercado\LaravelJWT\Facades\JWTFacade::class,

    // Other aliases
  ]

配置文件

然后您需要发布配置文件以自定义选项

php artisan vendor:publish

这将创建位于/config文件夹中的配置文件jwt.php。请务必查看并按需更改选项。

使用库

保护路由

现在您可以使用JWT中间件来保护您的路由

  Route::get('/user', function (Request $request) {
    return 'Route secured!';
  })->middleware('JWT');

中间件检查请求是否有Authorization头或通过GET或POST发送的参数token

JWT类

  /**
   * Creates and signs a new JWT.
   *
   * It signs the token with the configured type of key in the jwt.php file.
   *
   * @param string $jti A unique identifier for the token.
   * @param mixed[] $customClaims Optional data to append to the token.
   *
   * @return Lcobucci\JWT\Token
   */
  public function createToken($jti = null, $customClaims = null)
  /**
   * Validates and verifies a JWT.
   *
   * It verfies the token with the configured type of key in the jwt.php file.
   *
   * @return Lcobucci\JWT\Token Returns the token.
   *
   * @throws TokenNotFoundException When the token is not found.
   * @throws InvalidTokenException When the token is not valid.
   * @throws InvalidTokenSignException When the token sign is not valid.
   */
  public function verifyToken($token)
  /**
   * Gets the JWT string from the request headers or from the GET parameter.
   *
   * @return string Returns the token string.
   *
   * @throws TokenNotFoundException When the token is not found.
   */
  public function getTokenString()
  /**
   * Parses the JWT string.
   *
   * @return Lcobucci\JWT\Token Returns the token.
   *
   * @throws TokenNotFoundException When the token is not found.
   * @throws InvalidTokenException When the token is not valid.
   */
  public function parseTokenString()
  /**
   * Checks if the JWT has expired.
   *
   * @throws TokenNotFoundException When the token is not found.
   * @throws InvalidTokenException When the token is not valid.
   * @throws TokenExpiredException When the token has expired.
   */
  public function tokenExpired()
  /**
   * Checks if the JWT can be refreshed.
   *
   * @return boolean Returns true is the token can be refreshed, otherwise it returns false.
   *
   * @throws TokenNotFoundException When the token is not found.
   * @throws InvalidTokenException When the token is not valid.
   */
  public function isRefreshableToken()
  /**
   * Gets the JWT object.
   *
   * @return Lcobucci\JWT\Token Returns the token.
   *
   * @throws TokenNotFoundException When the token is not found.
   * @throws InvalidTokenException When the token is not valid.
   */
  public function getToken()