smskin/laravel-jwt-auth
Composer 安装命令:
composer require smskin/laravel-jwt-auth
包简介
JWT auth support module for laravel projects
README 文档
README
This module allows you to:
- generate a JWT for a user;
- authorize users via JWT (auth gateway).
Installation
Run the following commands:
composer require smskin/laravel-jwt-auth
php artisan vendor:publish --provider="SMSkin\JwtAuth\Providers\ServiceProvider"
Generate a random (brute-force resistant) string and store it in the JWT_SECRET_KEY variable in the .env file.
Add the following to the \App\Models\User model:
- the interface
\SMSkin\JwtAuth\Contracts\IJwtUser; - the trait
\SMSkin\JwtAuth\Traits\JwtTrait.
Example after editing:
class User extends Authenticatable implements IJwtUser
{
/** @use HasFactory<\Database\Factories\UserFactory> */
use HasFactory;
use Notifiable;
use JwtTrait;
protected $table = 'users';
...
Add a new guard to the auth.php configuration file:
'jwt' => [
'driver' => 'jwt',
'provider' => 'users',
]
Example after editing:
...
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'jwt' => [
'driver' => 'jwt',
'provider' => 'users',
]
],
...
Configuration
The jwt.php config file contains the following variables:
core.secret_key— secret key used for signing the JWT. Can be a string of any length;access_token.lifetime— access token lifetime (in minutes);refresh_token.lifetime— refresh token lifetime (in minutes).
Usage
How It Works
- In exchange for login and password, the user receives 3 pieces of data:
accessToken— a key that allows the service to identify the user;expiresAt— the timestamp when the key expires;refreshToken— a key that allows the user to obtain a new access token. - The user sends requests to the service. The middleware
auth:jwtis used to identify the user. - When the access token expires (either by time or receiving a 401 response), the user calls the refresh API method to exchange the
refreshTokenfor a newaccessToken.
Generating an Access Token (JWT)
The JwtTrait includes the generateJwt method, which returns a Request instance consisting of:
accessToken(string) — the access token;expiresAt(Carbon) — the expiration timestamp of the access token;refreshToken(string) — the refresh token.
Obtaining a New Access Token via Refresh
The IAuthService interface provides the refreshAccessToken method to exchange a refresh token for a new access token.
Example AuthController
<?php
namespace App\Http\Controllers;
use App\Http\Requests\RAuthRefresh;
use App\Http\Responses\RSAccessToken;
use App\Http\Requests\RAuthLogin;
use App\Models\User;
use Auth;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Validation\ValidationException;
use SMSkin\JwtAuth\Contracts\IAuthService;
use SMSkin\JwtAuth\Exceptions\InvalidRefreshToken;
class AuthController extends Controller
{
public function __construct(private readonly IAuthService $authService)
{
$this->middleware('auth:jwt')->except([
'login',
'refresh'
]);
}
public function check(): Response
{
return response()->noContent();
}
/**
* @throws ValidationException
*/
public function login(Request $request): JsonResponse
{
$this->validate($request, [
'email' => [
'required',
'email'
],
'password' => [
'required',
'string'
]
]);
$check = Auth::validate([
'email' => $request->input('email'),
'password' => $request->input('password')
]);
if (!$check) {
throw ValidationException::withMessages([
'password' => [
'Invalid password'
]
]);
}
/**
* @var $user User
*/
$user = User::where('email', $request->input('email'))->firstOrFail();
$jwt = $user->generateJwt();
return response()->json(new RSAccessToken(
$jwt->accessToken,
$jwt->expiresAt,
$jwt->refreshToken
));
}
/**
* @throws ValidationException
*/
public function refresh(Request $request): JsonResponse
{
$this->validate($request, [
'refreshToken' => [
'required',
'string'
]
]);
try {
$jwt = $this->authService->refreshAccessToken($request->input('refreshToken'));
} catch (InvalidRefreshToken) {
throw ValidationException::withMessages([
'refreshToken' => [
'Invalid refresh token'
]
]);
}
return response()->json(new RSAccessToken(
$jwt->accessToken,
$jwt->expiresAt,
$jwt->refreshToken
));
}
}
smskin/laravel-jwt-auth 适用场景与选型建议
smskin/laravel-jwt-auth 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 56 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 04 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「auth」 「laravel」 「jwt」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 smskin/laravel-jwt-auth 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 smskin/laravel-jwt-auth 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 smskin/laravel-jwt-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
统计信息
- 总下载量: 56
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 6
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-04-24