【问题标题】:Laravel unauthenticated no triggeringLaravel 未经身份验证无触发
【发布时间】:2018-04-26 19:14:40
【问题描述】:

我正在使用 rappasoft/laravel-5-boilerplate。

有一个奇怪的问题,我在路由中使用“auth”中间件。测试对需要登录的页面的访问。未登录时,它会显示未经身份验证的异常错误,并且不会重定向到登录屏幕。在 Handler.php 文件中未经身份验证存在并且有代码重定向到登录屏幕,但这个函数永远不会被调用!

为什么不会在 Illuminate\Auth\AuthenticationException 上调用未经身份验证的函数?没有意义。

路线:

Route::group(['middleware' => 'auth'], function () {
    Route::get('project/add', 'Project\ProjectController@create')-
>name('project.add');
});

异常/Handler.php

protected function unauthenticated( $request, AuthenticationException $exception )
{
    if ( $request->expectsJson() ) {
        return response()->json( [ 'error' => 'Unauthenticated.' ], 401 );
    }

    return redirect()->guest( route( 'frontend.auth.login' ) );
}

然后进行测试,我只是访问:http://px.app/project/add

【问题讨论】:

  • 您能否在edit 中包含MVCE 的问题?如果没有看到您的代码,我们很难找到任何关于您遗漏了什么的线索。
  • 添加代码sn-ps

标签: php authentication laravel-5.4


【解决方案1】:

为了解决这个问题,我将检查身份验证的代码放入渲染方法中,因此现在它在异常触发时正确调用未经身份验证的代码。

很奇怪,以前默认不能这样做。

【讨论】:

    【解决方案2】:

    确保您已将 AuthenticationException::class 包含在您的 Exceptions/Handler.php$dontReport 数组中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-10
      • 2019-06-14
      • 1970-01-01
      • 2015-03-17
      • 2020-12-14
      • 2017-01-06
      • 2019-02-28
      • 2018-01-05
      相关资源
      最近更新 更多