top20ofe / two-factor-authentication
Laravel 的双因素认证 (2FA)
Requires
- php: >=7.1
- endroid/qr-code: 3.5
- paragonie/constant_time_encoding: ^2.0
- spomky-labs/otphp: ^9.0
Requires (Dev)
- orchestra/testbench: ~3.4
- phpunit/phpunit: ~7.0
This package is not auto-updated.
Last update: 2024-10-02 05:50:49 UTC
README
1. 使用 Composer 安装
$ composer require top20ofe/two-factor-authentication
注意 - 如果您使用 Laravel 5.5 或更高版本,则自动发现包会自动更新提供者,您可以跳过到 步骤 3
2. 添加服务提供者
在引入包后,将 TwoFactorAuthenticationServiceProvider::class
添加到 app.php
配置文件中的提供者数组中
[ 'providers' => [ //... Top20ofe\TwoFactorAuthentication\TwoFactorAuthenticationServiceProvider::class ] ]
3. 发布配置文件
发布配置文件
$ php artisan vendor:publish --provider="Top20ofe\TwoFactorAuthentication\TwoFactorAuthenticationServiceProvider" --tag=config
一旦配置文件发布,您就可以浏览到应用程序的配置目录,查找 2fa-config.php
文件,并根据需要更改配置。
4. 运行迁移
现在运行迁移
$ php artisan migrate
它将使用默认的用户模型并添加两个列 is_2fa_enabled
和 secret_key
。
5. 在 LoginController 中添加 AuthenticatesUserWith2FA
特性
现在配置文件已经放置好了。最后要做的就是将 AuthenticatesUsersWith2FA
特性添加到 Http/Controllers/Auth/LoginController.php
文件中,这有助于在每次登录后停止用户在验证-2FA 页面输入 TOTP 令牌。
最终的代码片段如下。
use AuthenticatesUsers, AuthenticatesUsersWith2FA { AuthenticatesUsersWith2FA::authenticated insteadof AuthenticatesUsers; }
注意:不要忘记在头部包含使用语句 use Top20ofe\TwoFactorAuthentication\AuthenticatesUsersWith2FA
。
6. 为用户设置 2FA
• 启用 2FA
现在登录到应用程序并访问 /setup-2fa/
路由,这将会显示一个二维码,可以使用上述描述的 Google Authenticator 或 Authy 移动应用程序扫描。扫描此代码并点击 启用双因素认证。
• 禁用 2FA
要禁用双因素认证,请访问 /setup-2fa
路由,现在将显示一个 禁用双因素认证 按钮。点击以禁用您账户的 2FA。
7. 测试 2FA
现在要测试 2FA,请执行登出,然后再次登录,系统将要求您输入可以从认证移动应用程序获取的令牌。输入令牌,然后您就登录成功了。
此外
如果您想同时发布视图、迁移和配置文件,请运行
$ php artisan vendor:publish --provider="Top20ofe\TwoFactorAuthentication\TwoFactorAuthenticationServiceProvider"