mpociot / reauthenticate
通过让用户重新输入密码来重新验证应用程序特定部分的用户。
1.4.0
2019-10-08 08:25 UTC
Requires
- php: >=5.5.9
- illuminate/support: ~5.5|^6.0
Requires (Dev)
- mockery/mockery: ^1.0
- orchestra/testbench: ^3.5
This package is auto-updated.
Last update: 2024-09-08 18:58:09 UTC
README
因为有时候,你希望有额外的安全层
通过让用户重新输入应用程序特定部分的密码来重新验证用户(适用于Laravel 5)。
Route::group(['middleware' => ['auth','reauthenticate']], function () { Route::get('user/payment', function () { // Needs to re-enter password to see this }); });
内容
安装
为了将重新验证添加到您的项目,只需在您的 composer.json 中添加
"mpociot/reauthenticate": "~1.0"
。然后运行 composer install
或 composer update
。
或者如果您喜欢,可以运行 composer require mpociot/reauthenticate
。
使用
将中间件添加到您的 Kernel 中
在您的 app\Http\Kernel.php
文件中,将重新验证中间件添加到 $routeMiddleware
数组中。
protected $routeMiddleware = [ // ... 'reauthenticate' => \Mpociot\Reauthenticate\Middleware\Reauthenticate::class, // ... ];
添加路由和视图
默认情况下,reauthanticate 正在寻找一个路由 auth/reauthenticate
和一个视图 auth.reauthenticate
,其中将包含一个密码字段。
示例视图可以从 这里 复制。请注意,此文件需要手动复制,因为我不想将服务提供者添加到这个包中。
可以使用 Reauthenticates
特性使用 HTTP 控制器方法,所以您的 AuthController 看起来像这样
<?php namespace App\Http\Controllers\Auth; use App\User; use Validator; use App\Http\Controllers\Controller; use Mpociot\Reauthenticate\Reauthenticates; use Illuminate\Foundation\Auth\ThrottlesLogins; use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers; class AuthController extends Controller { /* |-------------------------------------------------------------------------- | Registration & Login Controller |-------------------------------------------------------------------------- | | This controller handles the registration of new users, as well as the | authentication of existing users. By default, this controller uses | a simple trait to add these behaviors. Why don't you explore it? | */ use AuthenticatesAndRegistersUsers, ThrottlesLogins, Reauthenticates { AuthenticatesAndRegistersUsers::getFailedLoginMessage insteadof Reauthenticates; }
确保从 guest
中间件中排除重新验证路由。
/** * Create a new authentication controller instance. * * @return void */ public function __construct() { $this->middleware('guest', ['except' => ['logout','getReauthenticate','postReauthenticate'] ]); }
要开始,将这些路由添加到您的 routes.php
文件中
// Reauthentication routes Route::get('auth/reauthenticate', 'Auth\AuthController@getReauthenticate'); Route::post('auth/reauthenticate', 'Auth\AuthController@postReauthenticate');
就这样。一旦用户成功重新验证,有效的登录将被存储30分钟。
用户被重定向到的URL可以通过在您的 config/app.php
文件中添加一个 reauthenticate_url
键来配置
return [ // ... 'reauthenticate_url' => '/custom-url', ];
许可证
重新验证是免费软件,根据MIT许可证的条款分发。