【问题标题】:Laravel 5.8 : Email verification link leads to a 403 errorLaravel 5.8:电子邮件验证链接导致 403 错误
【发布时间】:2020-01-03 20:31:46
【问题描述】:

我正在使用最新版本的 Laravel(通常)并尝试通过电子邮件验证实现身份验证。但是,我在用户注册后收到的电子邮件中的链接会导致 403 错误页面。不过链接似乎是正确的。

我正在与 Homestead 和 Vagrant 在 'localhost' 中工作。 注册用户正在工作,我在函数结束时获得了创建的用户,并且数据库填充得很好。

我不得不编辑 RegisterController 创建方法,因为没有发送邮件。

首先,点击链接时,我只有一个空白页面。经过一番阅读,我了解到用户需要在验证之前进行身份验证,并且注册用户后我的 RegisterController 没有对他进行身份验证。 在创建用户后,我添加了一些代码来对用户进行身份验证。 现在,当我点击链接时,我得到一个 403 错误页面。

注意:我的不是默认表的表包含 email_verified_at 字段。

这是我在 RegisterController 中的创建方法:

protected function create(Request $request)
    {
        //Create user in database
        $user = User::create([
            'Username' => $request['pc2f448466c4bad46f466f025e6cf88f9'],
            'email' => $request['p07d5d4eb055cc69dbba790bb9d39a3dc'],
            'Password' => md5($request['pb80ee032d334d60f5bc555af500fcf87']),
            'SecretQuestion' => $request['secret_question'],
            'SecretAnswer' => $request['secret_answer'],
            'Nickname' => $request['p9c969905e63e2824c45b8712e6d29585']
        ]);

        $user->sendEmailVerificationNotification();


        Auth::login($user);
    }

这是我的验证控制器:

<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\VerifiesEmails;

class VerificationController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Email Verification Controller
    |--------------------------------------------------------------------------
    |
    | This controller is responsible for handling email verification for any
    | user that recently registered with the application. Emails may also
    | be re-sent if the user didn't receive the original email message.
    |
    */

    use VerifiesEmails;

    /**
     * Where to redirect users after verification.
     *
     * @var string
     */
    protected $redirectTo = '/';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
        $this->middleware('signed')->only('verify');
        $this->middleware('throttle:6,1')->only('verify', 'resend');
    }

    protected function redirectTo()
    {
        return route('home');
    }
}

我尝试在 TrustProxies 中间件中设置 protected $proxies = '*';

.env 文件中我的应用程序常量:

APP_NAME=Furya
APP_ENV=local
APP_KEY=base64:xxxxxxxxxxxxxxxxxxxx
APP_DEBUG=true
APP_URL=http://localhost

在我的网络路由文件中,我的第一条路由是:

Auth::routes(['verify' => true]);

Route::get('/', function () {
    return view('main/home');
})->name('home');

使用php artisan route:list 时,我看到Auth::routes(['verify' =&gt; true]); 路由的正确中间件。

在我的用户表 (MySQL) 中,email_verified_at 字段具有日期时间类型。

在应用程序配置中,我的应用程序 URL 是这样设置的:

'url' => env('APP_URL', 'http://sunshine.local'),

'asset_url' => env('ASSET_URL', null),

我实际收到的链接是这样的:http://sunshine.local/email/verify/177?expires=1567179404&amp;signature=0a81e1370934c95282975c9fb7cfa379fd1945814e45894ffec5728e04ad4f7a

我不知道发生了什么,遗憾的是直到现在互联网上都没有找到对我有帮助的解决方案。

感谢您的帮助。

【问题讨论】:

  • 你有控制器app/Http/Controllers/Auth/VerificationController.php吗?如果是这样,你能把它包括在你的问题中吗?
  • @UdoE。是的,我有它,但没有编辑它。我正在用它的代码编辑我的帖子。
  • 好的。在您点击验证链接后,您的用户是否得到验证?我的意思是,当您检查用户表中的email_verified_at 字段时,它是null 还是已更新(即使出现403 错误)?另外,您能否在web.php 中提供您的家乡路线定义。似乎 url '/' 以某种方式受到保护,必须通过定义的路由方法访问。
  • @UdoE。该字段保持为空。我用我的家乡路线定义更新我的帖子。
  • @TomGiru 看到我的回答。希望对您有所帮助。

标签: php laravel


【解决方案1】:

从您的web.php 文件中,当发出 http 网络请求时,您的应用程序根 url '/' 只能通过命名路由访问:route('home'),它返回一个自定义视图。

所以,像这样在VerificationController.php 中包含一个redirectTo() 方法

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
// other imports

class VerificationController extends Controller
{
    use VerifiesEmails;

    /**
     * Where to redirect users after verification.
     *
     * @var string
     */
    protected $redirectTo = '/';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
        $this->middleware('signed')->only('verify');
        $this->middleware('throttle:6,1')->only('verify', 'resend');
    }

    protected function redirectTo()
    {
        return route('home');
    }
}

现在您应该在电子邮件验证后被重定向到有效路由。

【讨论】:

  • 很遗憾,点击链接后我仍然收到 403 错误页面。
  • 重新注册一个新用户并再次检查新链接。另外,您只包含了VerificationController 的一部分。包括整个
  • 我编辑了我的帖子,这是我得到的链接:sunshine.local/email/verify/…
  • 尝试通过在控制台上依次运行php artisan route:cachephp artisan cache:clearcomposer dump auto-load 来清除路由和应用程序缓存并刷新自动加载。重新注册看看是否有帮助。
  • 我试过了,但还是不行。请注意,我无法执行php artisan route:cache,因为我的路线使用了闭包。
【解决方案2】:
Auth::routes(['verify' => true]);`

我注意到您的路线中有一个反引号。就在验证路由分号之后。尝试删除它,看看会发生什么

【讨论】:

  • 只是复制粘贴错误。我的路由文件中没有它。
  • @TomGirou 好的,您检查过日志文件吗? 403 表示禁止请求。这意味着您无权查看该资源。请验证用户是否已登录。粘贴记录的错误
【解决方案3】:

这是由于用户表中字段名称的问题。

请参阅我的另一篇帖子以获取答案:Laravel 5.8 : Remember me token not being saved in database

【讨论】:

    【解决方案4】:

    有同样的问题; 检查你的 .env 你的

    APP_URL=http://...

    是正确的并且正在匹配。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-07
      • 2019-07-19
      • 2019-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-15
      • 1970-01-01
      相关资源
      最近更新 更多