spie/laravel-jwt 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

spie/laravel-jwt

Composer 安装命令:

composer require spie/laravel-jwt

包简介

JWT package for Laravel and Lumen.

README 文档

README

Build Coverage Status StyleCI

This package provides a Laravel Guard for JWT authentication.

This package provides a access and refresh token workflow. You need to create an access token first. With the access token you can issue a refresh token. Then this refresh token can be used to create access tokens if required.

Requirements

Installation

Just pull the package with composer

composer require spie/laravel-jwt

Laravel

Add the SPie\LaravelJWT\Providers\LaravelServiceProvider to the providers array in config/app.php.

'providers' => [
    ...
    SPie\LaravelJWT\Providers\LaravelServiceProvider::class
],

Lumen

In bootstrap/app.php add Illuminate\Auth\AuthServiceProvider and SPie\LaravelJWT\Providers\LumenServiceProvider.

...
    
$app->register(Illuminate\Auth\AuthServiceProvider::class);
    
$app->register(SPie\LaravelJWT\Providers\LumenServiceProvider::class);
    
...

Configuration

JWT

You can configure the JWT package in your .env file. You can find the available config options in the .env.example file.

JWT_SECRET=
JWT_ISSUER=App
JWT_SIGNER=Lcobucci\JWT\Signer\Hmac\Sha256
JWT_ACCESSS_TOKEN_PROVIDER=SPie\LaravelJWT\TokenProvider\HeaderTokenProvider
JWT_ACCESS_TOKEN_TTL=10
JWT_ACCESS_TOKEN_KEY=Authorization
JWT_BLACKLIST=SPie\LaravelJWT\Blacklist\CacheTokenBlacklist
JWT_REFRESH_TOKEN_PROVIDER=SPie\LaravelJWT\TokenProvider\CookieTokenProvider
JWT_REFRESH_TOKEN_TTL=
JWT_REFRESH_TOKEN_KEY=refresh-token
JWT_REFRESH_TOKEN_REPOSITORY=
JWT_IP_CHECK_ENABLED=

You can also copy the config/jwt.php file from the repo to your projects config directory to configure JWT without an .env file.

It is required to add a value for JWT_SECRET and JWT_ISSUER. For every other config a default value exists.

Auth

You need to add an entry for the JWTGuard in your config/auth.php file.

'guards' => [

    ...
    
    'jwt' => [
        'driver' => 'jwt',
    ],
],

Usage

You can use the SPie\LaravelJWT\Auth\JWTGuard by using dependency injection and depend on Illuminate\Contracts\Auth\Guard, but you have to bind the JWTGuard to SPie\LaravelJWT\Auth\JWTGuard to Illuminate\Contracts\Auth\Guard in your ServiceProvider. You can also get the JWTGuard by Illuminate\Auth\AuthManager::guard($name), using the guard name configured in config/auth.php.

User

To use your user model for authentication, it has to implement the SPie\LaravelJWT\Contracts\JWTAuthenticatable interface.

Login

To Login use the login method provided by Illuminate\Contracts\Auth\StatefulGuard. After that you can get the Access and Refresh token by the getAccessToken and getRefreshToken methods.

Logout

The JWTGuard::logout() method will unset the $jwt and $user property. If a TokenBlacklist is configured, the token will be revoked. If a refresh token was used, it will get revoked.

TokenProvider

You have to specify a TokenProvider to be able to extract a token from request. This package includes two TokenProvider already: the SPie\LaravelJWT\TokenProvider\HeaderTokenProvider and the SPie\LaravelJWT\TokenProvider\CookieTokenProvider. Of course, you can create a custom TokenProvider, implementing the SPie\LaravelJWT\Contracts\TokenProvider interface and specify it in the JWT config. You have to specify a TokenProvider for refresh tokens too.

JWTHandler

To create or validate JWTs, you can use the SPie\LaravelJWT\JWTHandler.

Create JWT

To create a JWT, you have to provide a subject and an optional payload and TTl.

/** @var SPie\LaravelJWT\JWT $jwt */
$jwt = $jwtHandler->createJWT('SUBJECT', ['claim1' => 'value1'], );

Get valid JWT

To validate a JWT, you have to provide the token as string. You will get a SPie\LaravelJWT\JWT object, if the token is valid, or a specific JWTException.

$token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJUZXN0IiwiaWF0IjoxNTQyOTc0NzM3LCJleHAiOjE1NzQ1OTcxMzcsImF1ZCI6IiIsInN1YiI6IlRlc3QifQ.XdS6BiYD02I_1AAFeCxuO3LdeNBXLjE9TWd-G89ePOk';
 
/** @var SPie\LaravelJWT\JWT $jwt */
$jwt = $jwtHandler->getValidJWT($token);

Possible exceptions are:

  • SPie\LaravelJWT\Exceptions\InvalidTokenException
  • SPie\LaravelJWT\Exceptions\InvalidSignatureException
  • SPie\LaravelJWT\Exceptions\TokenExpiredException
  • SPie\LaravelJWT\Exceptions\BeforeValidException

If the setting JWT_IP_CHECK_ENABLED is set, the IP address will be compared with the one. If they don't match, the user is not authenticated.

JWT Object

The SPie\LaravelJWT\JWT object is just a wrapper for Lcobucci\JWT\Token. To get the string representation of the JWT, you have to call the JWT::getJWT() method.

TokenBlacklist

The JWTGuard can use a token blacklist. The token blacklist has to implement the SPie\LaravelJWT\Contracts\TokenBlacklist interface. The interface provide two methods: revoke(SPie\LaravelJWT\JWT $jwt) and isRevoked(string $jwt). The revoke method will store the JWT until it would expire, or forever if no expiration date is set. The isRevoked method will check for a revoked token.

RefreshTokenRepository

You have to implement the SPie\LaravelJWT\RefreshTokenRepository if you want to use refresh tokens. The RefreshTokenRepository will store and revoke the refresh tokens if needed and also checks if a refresh token is already revoked.

spie/laravel-jwt 适用场景与选型建议

spie/laravel-jwt 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 141 次下载、GitHub Stars 达 4, 最近一次更新时间为 2018 年 11 月 25 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「Authentication」 「laravel」 「jwt」 「lumen」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 spie/laravel-jwt 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 spie/laravel-jwt 我们能提供哪些服务?
定制开发 / 二次开发

基于 spie/laravel-jwt 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 141
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 4
  • 点击次数: 0
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 4
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-11-25