安装
1.作曲家安装
bash
$ composer require thecodework/two-factor-authentication
2。添加服务提供商
要求包后将TwoFactorAuthenticationServiceProvider::class添加到app.phpconf文件中的providors数组中
php
[
'providers' => [
//...
Thecodework\TwoFactorAuthentication\TwoFactorAuthenticationServiceProvider::class
]
]
3.运行迁移
现在运行迁移
bash
$ php artisan migrate
它将使用默认的 User 模型并添加两列 is_2fa_enabled 和 secret_key。
4.在 LoginController 中添加 AuthenticatesUserWith2FA trait
现在配置文件已放置。最后要做的是在Http/Controllers/Auth/LoginController.php 文件中添加AuthenticatesUsersWith2FA trait,这有助于阻止用户在每次登录后在verify-2fa 页面输入TOTP 令牌。
最终的 sn-p 将如下所示。
php
use AuthenticatesUsers, AuthenticatesUsersWith2FA {
AuthenticatesUsersWith2FA::authenticated insteadof AuthenticatesUsers;
}
注意:不要忘记在标题中包含使用语句use Thecodework\TwoFactorAuthentication\AuthenticatesUsersWith2FA。
5.发布配置文件
终于发布配置文件
$ php artisan vendor:publish --provider="Thecodework\TwoFactorAuthentication\TwoFactorAuthenticationServiceProvider" --tag=config
配置文件发布后,您可以导航到应用程序的配置目录并查找 2fa-config.php 文件并根据需要更改配置。
6.为用户设置 2FA
• 启用 2FA
现在登录应用程序并访问/setup-2fa/ route,该路径将显示可以使用 Google Authenticator 或 Authy 移动应用程序扫描的条形码,如上所述。
扫描该代码并点击启用两因素身份验证。
• 禁用 2FA
要禁用双重身份验证,请访问/setup-2fa 路由,该路由现在将显示禁用双重身份验证按钮。点击为您的帐户禁用 2FA。
7.测试 2FA
现在要测试 2FA,执行注销并重新登录,它会要求您输入可以从验证器移动应用程序获取的 Token。输入令牌即可登录。