weldon/laravel-redis-auth
Composer 安装命令:
composer require weldon/laravel-redis-auth
包简介
Laravel authorization and authentication with redis database.
README 文档
README
The Laravel Redis Auth Package is a Composer package that provides authentication functionality using a Redis database in Laravel applications. It offers a seamless integration with Laravel's authentication system while leveraging the speed and flexibility of Redis for storing user credentials.
Purpose
I wanted to build microservices and I realized that I need a authentication service which can works with all microservices. Then I had created this package. If you have three microservices, you should install this package to your all of them and you should connect services to one Redis database. When client sends request to get new access token, one of your services creates access token and stores it to Redis database. When client sends request to other service with acces token, that service can check token from Redis database.
Requirements
- PHP >= 7.4
- predis/predis
- Redis database
Installation
Install the package via Composer:
composer require weldon/laravel-redis-auth
Publish
Publish the package configuration:
php artisan vendor:publish --provider="Weldon\LaravelRedisAuth\RedisAuthServiceProvider"
Usage
Update the .env configuration to use the cache for authentication:
REDIS_CLIENT=predis
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
REDIS_PREFIX=''
You must add Weldon\LaravelRedisAuth\Traits\RedisAuthentication trait to you App/Models/User class.
use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; use Laravel\Sanctum\HasApiTokens; use Weldon\LaravelRedisAuth\Traits\RedisAuthentication; class User extends Authenticatable { use HasApiTokens, HasFactory, Notifiable, RedisAuthentication; ...
After that you can create access token by createAuthToken function of App/Models/User class.
You can create token with abilities. The packge stored all tokens with abilities in Redis database.
$user = User::query() ->where('email', $request->string('email')) ->first(); $accessToken = $user->createAuthToken([ 'users.add', 'users.show', 'users.delete', ]);
You can check token's ability with check function of App/Models/User class in your Controllers.
If user does not have a ability, check function throw Illuminate/Http/Exceptions/HttpResponseException.
Auth::user()->check('users.add', "If you want to change error message!"); OR auth()->user()->check('messages.show');
You can protect your routes by adding the auth middleware to them:
Route::middleware('redis-auth')->group(function () {
// Protected routes
});
There are configurations in redis-auth.php in config folder.
You can change them.
/** * Name of redis connection. * * Note that this name is listed in the redis section of your config/database.php */ 'connection' => env('REDIS_AUTH_CONNECTION', 'default'), /** * Name of selected hashing algorithm (i.e. "md5", "sha256", "haval160,4", etc..) * See hash_algos for a list of supported algorithms. */ 'algo' => env('REDIS_AUTH_ALGO', 'sha256'), /** * Secret key for creating a new token */ 'secret_key' => env('REDIS_AUTH_SECRET_KEY', 'laravel-redis-auth-secret-key'), /** * token_ttl represents the Token Time To Live, which defines the lifespan or expiration time of a token. */ 'token_ttl' => env('REDIS_AUTH_TOKEN_TTL', 3600 * 24), /** */ 'unauthorized_message' => env('UNAUTHORIZED_MESSAGE', 'Unauthorized...'),
Contributing
Contributions are welcome! If you find any issues or have suggestions for improvements, please open an issue or submit a pull request.
License
This package is open-source and released under the MIT License.
weldon/laravel-redis-auth 适用场景与选型建议
weldon/laravel-redis-auth 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 518 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 02 月 21 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「redis」 「auth」 「laravel」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 weldon/laravel-redis-auth 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 weldon/laravel-redis-auth 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 weldon/laravel-redis-auth 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Laravel Multiauth package
Microservice RPC through message queues.
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.
The CodeIgniter Redis package
贝嘟分布式缓存扩展
This package provides a flexible way to add Role-based Permissions to Laravel 6.x
统计信息
- 总下载量: 518
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 13
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-02-21