【问题标题】:Add recaptcha to default Laravel Password Reset将验证码添加到默认 Laravel 密码重置
【发布时间】:2018-03-13 13:51:29
【问题描述】:

我想要求我的 Laravel 5.1 应用程序的用户完成 Google Recaptcha 流程,但我不知道如何安全地修改发送重置密码链接的代码。

为我执行此操作的代码是继承特征“ResetsPassword”中的“postEmail()”函数。这是我的整个 PasswordController:

use App\Http\Controllers\Controller;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Contracts\Auth\PasswordBroker;
use Illuminate\Foundation\Auth\ResetsPasswords;

class PasswordController extends Controller {

use ResetsPasswords;

/**
 * Create a new password controller instance.
 *
 * @param  \Illuminate\Contracts\Auth\Guard  $auth
 * @param  \Illuminate\Contracts\Auth\PasswordBroker  $passwords
 * @return void
 */
public function __construct(Guard $auth, PasswordBroker $passwords)
{
    $this->auth = $auth;
    $this->passwords = $passwords;

    $this->middleware('guest');
}

}

如您所见,所有真正的方法都在供应商文件中的“ResetsPasswords”特征中,所以我不想直接修改它。如何在我的 PasswordsController 中安全地修改继承特征中的“postEmail()”函数?

【问题讨论】:

  • 扩展vendor Class怎么样?只是一个想法,还没准备好回答。

标签: laravel laravel-5 laravel-5.1 recaptcha change-password


【解决方案1】:

在你的ForgotPasswordController 添加这个方法:

protected function validateEmail(Request $request)
{
    $this->validate($request, [
        'email' => 'required|email',
        'g-recaptcha-response' => 'recaptcha',
    ]);
}

并在此处遵循我的 reCAPTCHA 实施指南:Laravel reCaptcha integration

【讨论】:

  • 完美,就像我喜欢的答案一样:小巧、精确且有效!我看到 laravel 6 可能发生了变化,但现在在 SendsPasswordResetEmails.php 特征中它运行 $request->validate([...]) 而不是 $this->validate($request,[...]) 但我认为这只是一个很小的变化,可能会双向工作。
【解决方案2】:

将代码添加到 Auth/ForgotPasswordController.php

<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Foundation\Auth\SendsPasswordResetEmails;

class ForgotPasswordController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Password Reset Controller
    |--------------------------------------------------------------------------
    |
    | This controller is responsible for handling password reset emails and
    | includes a trait which assists in sending these notifications from
    | your application to your users. Feel free to explore this trait.
    |
    */

    use SendsPasswordResetEmails;

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest');
    }

    protected function validateEmail(Request $request)
    {
        $this->validate($request, [
            'email' => ['required', 'string', 'email', 'max:255'],
            'g-recaptcha-response' => 'required|recaptcha',
        ]);
    }
}

【讨论】:

    猜你喜欢
    • 2017-07-26
    • 1970-01-01
    • 2018-06-18
    • 1970-01-01
    • 2017-12-09
    • 2022-01-16
    • 2017-12-31
    • 1970-01-01
    • 2017-07-06
    相关资源
    最近更新 更多