arubacao / http-basic-auth-guard
适用于 Lumen >=5.2 的 HTTP Basic Auth Guard
Requires
- php: ^7.1.3
- illuminate/database: ^5.7
- illuminate/support: ^5.7
README
HTTP Basic Auth Guard 是一个 Lumen 包,允许您将
basic作为应用程序中身份验证守卫的驱动程序。
该守卫为 Lumen >=5.2 恢复了缺失的无状态 HTTP Basic 身份验证的可能性。
说明
截至 Lumen 5.2,不再包含 会话存储。
不幸的是,要调用 Auth::onceBasic()、Auth::basic() 或类似功能,您需要 session 驱动程序,这需要 会话存储。
因此,HTTP Basic 身份验证不再为 Lumen >=5.2 默认工作。
坦白说,我不知道 Taylor Otwell 为什么从 Lumen 5.2 中移除了这个功能。
我的最好猜测是,他甚至不知道,因为我的问题在 github 上被 立即关闭 😃
幸运的是,这个包恢复了通常的功能!
要求
- Lumen
5.2或更高版本安装。 - 注意:对于 Laravel 5.* 或 Lumen 5.1,HTTP Basic Auth 仍然可以使用
session驱动程序默认工作:[链接](https://laravel.net.cn/docs/5.2/authentication#stateless-http-basic-authentication)。
测试于
- Lumen
5.2、5.3、5.4、5.5、5.6、5.7 - PHP
5.6、7.0、7.1、7.2、7.3
当前主分支适用于 Lumen >= 5.7。对于 Lumen <= 5.6,请使用版本 ^1.0。
安装
1. 拉取包
$ composer require arubacao/http-basic-auth-guard
2. 阅读并遵循官方 Lumen 文档中的身份验证说明
https://lumen.laravel.com/docs/5.7/authentication
重要:
在使用 Lumen 的身份验证功能之前,您应该在您的
bootstrap/app.php文件中取消注释注册AuthServiceProvider服务提供者的调用。
// bootstrap/app.php // Uncomment the following line $app->register(App\Providers\AuthServiceProvider::class);
当然,您希望进行身份验证的任何路由都应该分配 auth 中间件,因此您应该在您的
bootstrap/app.php文件中取消注释对 $app->routeMiddleware() 的调用。
// bootstrap/app.php // Uncomment the following lines $app->routeMiddleware([ 'auth' => App\Http\Middleware\Authenticate::class, ]);
如果您想使用
Auth::user()来访问当前已验证的用户,您应该在您的bootstrap/app.php文件中取消注释$app->withFacades()方法。
// bootstrap/app.php // Uncomment the following lines $app->withFacades(); $app->withEloquent();
3. 添加服务提供者
打开 bootstrap/app.php 并注册服务提供者
// bootstrap/app.php // Add the following line $app->register(Arubacao\BasicAuth\BasicGuardServiceProvider::class);
4. 设置守卫驱动程序
注意:在 Lumen 中,您首先需要从
vendor/laravel/lumen-framework/config/auth.php目录复制配置文件,在您的根目录中创建一个config文件夹,最后将复制的文件粘贴到那里。
$ mkdir config $ cp vendor/laravel/lumen-framework/config/auth.php config/
打开您的 config/auth.php 配置文件。
在 guards 中添加一个新的键(例如本例中的 api)。
将 basic 作为驱动程序。
确保您还设置了与数据库通信的守卫的 provider。
// config/auth.php 'guards' => [ 'api' => [ 'driver' => 'basic', 'provider' => 'users' ], // ... ], 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\User::class, ], ],
使用方法
保护路由的中间件
Route::get('api/whatever', ['middleware' => 'auth:api', 'uses' => 'NiceController@awesome']);
保护控制器的中间件
<?php namespace App\Http\Controllers; class NiceController extends Controller { public function __construct() { $this->middleware('auth:api'); } }
变更日志
请参阅变更日志了解最近发生了哪些变化。
贡献
请参阅贡献指南了解详细信息。
如有任何问题、反馈、建议或疑问,请使用问题追踪器。
安全性
如果您发现任何与安全相关的问题,请发送电子邮件至arubacao@gmail.com,而不是使用问题追踪器。
许可协议
MIT许可协议(MIT)。