vkoori/laravel-stateless-auth
Composer 安装命令:
composer require vkoori/laravel-stateless-auth
包简介
Stateless authentication for Laravel using JWT (JSON Web Tokens). This package enables secure, token-based authentication for API endpoints.
README 文档
README
A fully stateless JWT authentication guard and provider for Laravel.
Supports access/refresh tokens, automatic cookie injection, multi-source token parsing (header, query, cookie), and token revocation.
✨ Features
- 🔐 Custom Laravel guard + provider (fully stateless)
- ♻️ Refresh token support
- 🍪 Cookie-based tokens (HttpOnly, optional)
- 📩 Header / Query string token support
- 🔄 Token revocation
- 💡 Simple trait-based token issuing
- ⚡️ Octane-ready
📦 Installation
composer require vkoori/laravel-stateless-auth
⚙️ Configuration
Publish the config file:
php artisan vendor:publish --provider="Vkoori\JwtAuth\AuthServiceProvider"
This will publish config/jwt-guard.php.
also read optionally
🛡 Register the Guard & Provider
In your config/auth.php:
'guards' => [ 'api' => [ 'driver' => 'jwt-auth', 'provider' => 'jwt-users', ], ], 'providers' => [ 'jwt-users' => [ 'driver' => 'jwt-auth-provider', 'model' => App\Models\User::class, ], ],
👤 Token Support on User Model
Your User model must use the provided trait:
use Vkoori\JwtAuth\Auth\Traits\HasApiTokens; class User extends Authenticatable { use HasApiTokens; }
🧩 Custom Cache Driver for JWT Token Storage
To prevent JWT tokens from being removed during global cache clears (php artisan cache:clear), you can isolate token storage using a custom cache store.
🔧 Configuration
Use one of the existing cache stores, or define a dedicated store for JWT.
- Add a new cache store in
config/cache.php:
'stores' => [ // Other cache stores... 'redis_jwt' => [ 'driver' => 'redis', 'connection' => env('REDIS_JWT_CONNECTION', 'jwt'), 'lock_connection' => env('REDIS_CACHE_LOCK_CONNECTION', 'default'), ], ],
- Define a dedicated Redis connection in
config/database.php:
'redis' => [ // Other connections... 'jwt' => [ 'url' => env('REDIS_URL'), 'host' => env('REDIS_HOST', '127.0.0.1'), 'username' => env('REDIS_USERNAME'), 'password' => env('REDIS_PASSWORD'), 'port' => env('REDIS_PORT', '6379'), 'database' => env('REDIS_JWT_DB', '2'), ], ],
- Set the custom driver on your
Authenticatablemodel using theHasApiTokenstrait:
use Vkoori\JwtAuth\Auth\Traits\HasApiTokens; class User extends Authenticatable { use HasApiTokens; public ?string $jwtCacheDriver = 'redis_jwt'; }
💡 Why This Matters
By using a dedicated Redis cache store for JWT tokens:
- php artisan cache:clear won’t wipe out active tokens
- You can still manually clear tokens when needed:
php artisan cache:clear redis_jwt
This is especially useful when enable_revoke is set to true in your config, ensuring users are logged out securely while preserving system-wide cache stability.
🔐 JWT Scope Middleware
This package includes a built-in JwtScopeMiddleware to restrict route access based on scopes defined in the JWT token payload.
🔧 Middleware Registration
✅ Laravel 12+
In Laravel 12+, middleware is registered using the bootstrap/app.php:
->withMiddleware(function (Middleware $middleware): void { $middleware ->alias([ 'jwt.scope' => \Vkoori\JwtAuth\Middlewares\JwtScopeMiddleware::class, ]); })
🧱 Laravel 11 and below
If you're using Laravel 11 or older, register the middleware in app/Http/Kernel.php:
protected $routeMiddleware = [ // ... 'jwt.scope' => \Vkoori\JwtAuth\Middlewares\JwtScopeMiddleware::class, ];
✅ Defining Scopes in Your JWT
Make sure you include the scope claim when generating your access tokens. Example:
$user->accessToken(scopes: ['admin', 'manager'])
🔒 Protect Routes Using Scope Middleware
You can pass multiple allowed scopes or single scope, and access will be granted if at least one matches:
Route::middleware(['jwt.scope:admin'])->group(function () { Route::get('/admin/dashboard', [AdminController::class, 'index']); }); Route::middleware(['jwt.scope:admin,other'])->group(function () { Route::get('/admin/dashboard', [AdminController::class, 'index']); });
⚠️ Error Handling
- If the user is unauthenticated, a 401 Unauthorized will be thrown.
- If the token lacks the required scopes, a 403 Forbidden (ScopeException) will be raised.
vkoori/laravel-stateless-auth 适用场景与选型建议
vkoori/laravel-stateless-auth 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 546 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 07 月 18 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「auth」 「jwt」 「guard」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 vkoori/laravel-stateless-auth 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 vkoori/laravel-stateless-auth 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 vkoori/laravel-stateless-auth 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Laravel Multiauth package
A lightweight and powerful OAuth 2.0 authorization and resource server library with support for all the core specification grants. This library will allow you to secure your API with OAuth and allow your applications users to approve apps that want to access their data from your API.
A simple library to decode and parse Apple Sign In client tokens.
This package provides a flexible way to add Role-based Permissions to Laravel 6.x
JWT API authentication driver (Guard) for Laravel.
Email Toolkit Plugin for CakePHP
统计信息
- 总下载量: 546
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 34
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Unknown
- 更新时间: 2025-07-18