如何利用 Laravel 提供的密码确认功能来保护用户信息安全?

Laravel 简介

密码确认中间件是在 laravel v6.2.0 中引入的,它内置于 Illuminate/Auth 中,而且你可以在 Illuminate/Auth/Middleware/RequirePassword.php 文件中找到它

如何工作

它通过查看传入请求的 password_confirmed_at session 来工作,并根据结果使用 ResponseFactory 构建响应,以使用 JSON 响应或重定向到 password.confirm 路由。如果成功,则它将重定向到原始路由,或者将 $redirectTo 变量设置为失败

默认情况下,如果成功,token 有效期将设置为 3 小时,但是你也可以使用 auth.password_timeout 配置项进行更改

「密码确认」的用法

让我们来做一些假设:首先,假设您有一些用户,这些用户可以更改一些敏感设置,例如,他们可能会更改密码或禁用双向身份验证。

这是你可能想要保护的东西,尤其是如果你让你的用户使用 「记住我」 功能,比如说小红已经设置了 「记住我」 功能,然后她关闭浏览器后走开了,后来小明来了。

小明在您的站点上也有一个帐户,所以他进入您的站点只是为了以小红的身份登录,现在小明出于恶作剧的目的想更改一些敏感设置,但是当他来到 /settings/security/ 路由时,他将被要求输入小红的密码,他不知道,现在小红的敏感设置已经从小明的恶作剧中挽救了回来。

我知道这种方式比较滑稽,但它依旧比较重要,有时你会希望你的用户给你一个确认,已确定访问他们帐户的是他本人

那么我们为什么不创造一个可爱的小例子呢?让我们启动一个新的本地 Laravel 应用程序。

laravel new password-confirmation --auth

提示: 你可以像我一样,使用 Laravel Valet 和 PHP 7.3。

创建迁移文件

为了尽可能简单,让我们在 users 表中添加一列。

php artisan make:migration create_user_settings --table users
public function up()
{
    Schema::table('users', function (Blueprint $table) {
        $table->string('secret')->nullable();
    });
}

public function down()
{
    Schema::table('users', function (Blueprint $table) {
        $table->dropColumn('secret');
    });
}

当然,不要忘了迁移它并将 secret 添加到 User上的 fillable 数组中。

新路由和控制器。

现在让我们添加一些路由,为简单起见,我们将使用组继承和新的 SecretController

Route::group(['middleware' => ['auth']], function () {
    Route::post('/secret', 'SecretController@store')->name('secret.store');

    Route::group(['middleware' => ['password.confirm']], function () {
        Route::get('/secret/edit', 'SecretController@edit')->name('secret.edit');
    });
});

我们的控制器也很简单,只需要一些基本的验证。

class SecretController extends Controller
{
    public function edit()
    {
        return view('secret.edit');
    }

    public function store(Request $request)
    {
        $validated = $request->validate([
            'secret' => 'required|string'
        ]);

        auth()->user()->update($request->all());

        return redirect()->route('home')->with('status', 'Secret added!');
    }
}

添加视图

剩下要做的就是创建我们的视图,因此我们在 resources/views 目录下创建一个名字为 secret 的目录,然后创建一个 edit.blade.php 文件。

此文件将包含我们更新密码的表单。

<form action="{{ route('secret.store') }}"  method="POST">
    @csrf

    <div class="form-group is-invalid">
        <label for="secret">Your secret</label>
        <input id="secret" type="text" name="secret" class="form-control @error('secret') is-invalid @enderror" value="{{ old('secret') }}" aria-describedby="secret-help" required>
        <div class="d-flex justify-content-between w-100">
            <small id="secret-help" class="form-text text-muted @error('secret') is-invalid @enderror">We'll never share your secret with anyone else.</small>
            @error('secret')<div class="invalid-feedback w-auto">{{ $message }}</div>@enderror
        </div>
    </div>

    <button type="submit" class="btn btn-primary">Save</button>
</form>

我们还将编辑 home.blade.php 文件显示我们的密码,所以在您的 仪表板卡片 下面添加以下内容。

<div class="card mb-5">
    <div class="card-body text-center">
        @if (auth()->user()->secret)
            {{ auth()->user()->secret }}
        @else
            You don't have any secret yet, <a href="{{ route('secret.edit') }}" title="Edit secret">add one.</a>
        @endif
    </div>
</div

现在,如果您前往 secret/edit ,将提示您确认密码,然后继续编辑您的密码。

confirm password view

edit secret view


结论。

在 Laravel 中添加密码确认很简单,但有一些限制,密码确认不能在POST请求中使用,因此您无法开箱即用地将其添加到 secret.store 本身。

我还没有看过,但几乎可以肯定有一个程序包可以为您提供该功能,如果我找到一个程序包,我稍后会将其添加到此处以供您使用。

现在我是在说我使其可以投入生产的演示了吗?不,与往常一样,在更新域或模型时,您应该检查的不仅仅是简单的验证要求,但希望这能让您了解在项目中的哪些地方可以使用此功能。

与往常一样,感谢您的阅读,您可以在我的 GitHub 上找到此演示的完整源代码 (thinkverse/password-confirmation).

本文中的所有译文仅用于学习和交流目的,转载请务必注明文章译者、出处、和本文链接
我们的翻译工作遵照 CC 协议,如果我们的工作有侵犯到您的权益,请及时联系我们。

原文地址:https://dev.to/thinkverse/how-laravel-s-...

译文地址:https://learnku.com/laravel/t/50042

本文为协同翻译文章,如您发现瑕疵请点击「改进」按钮提交优化建议
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!