Laravel 5.5 多重身份验证路由问题

2024-05-02

尝试使用 Doctrine 而不是 Eloquent 让 Laravel 进行多重身份验证。我已经尝试了很多事情,但总是陷入困境。我目前定义了两个守卫、两个模型、两个登录控制器等。如果我启用其中之一,它们就会起作用。如果我同时尝试两者,则似乎只有默认防护起作用。当我尝试访问其他守卫时,我被重定向到错误的登录页面。

如果我转到 /login - 按预期工作

如果我转到 /home (未登录) - 按预期重定向到 /login

如果我去 /register- 按预期工作

如果我转到 /admin/login - 按预期工作

如果我转到 /admin/register - 按预期工作

如果我转到 /admin (未登录) - 失败 - 应该重定向到 /admin/login 但反而重定向到 /login

我确信我错过了一些简单的事情。一切都是单独运作的。它只是获取 /admin 路由来使用正确的中间件......我想......也许?

我的路线文件:

Auth::routes();

// staff authentication routes
Route::group( [ 'middleware' => [ 'web' ] ], function() {
  Route::get( 'admin/login', 'Auth\StaffLoginController@showLoginForm' );
  Route::post( 'admin/login', [ 'as' => 'staff.login', 'uses' => 'Auth\StaffLoginController@login' ] );
  Route::get( 'admin/register', 'Auth\StaffRegisterController@showRegistrationForm' );
  Route::post( 'admin/register', [ 'as' => 'staff.register', 'uses' => 'Auth\StaffRegisterController@register' ] );

  Route::group( [ 'middleware' => [ 'staff' ] ], function() {
    Route::get( '/admin', 'AdminController@index' )->name( 'admin' );
  });
});

Route::get('/home', 'HomeController@index')->name('home');

我尝试过各种路线定义,但这是最新的迭代。之前的迭代也没有成功。

我的授权文件:

'defaults' => [
    'guard' => 'web',
    'passwords' => 'users',
],

'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],

    'api' => [
        'driver' => 'token',
        'provider' => 'users',
    ],

    'staff' => [
        'driver' => 'session',
        'provider' => 'adminusers',
    ]
],

'providers' => [
    'users' => [
        'driver' => 'doctrine',
        'model' => App\Users\Customer::class,
    ],
    'adminusers' => [
        'driver' => 'doctrine',
        'model' => App\Users\Staff::class,
    ]
],

'passwords' => [
    'users' => [
        'provider' => 'users',
        'table' => 'password_resets',
        'expire' => 60,
    ],
    'adminusers' => [
        'provider' => 'adminusers',
        'table' => 'password_resets',
        'expire' => 30,
    ],
],

我的 LoginController (与开箱即用的基本相同):

class LoginController extends Controller {
  use AuthenticatesUsers;

  protected $redirectTo = '/home';

  public function __construct() {
    $this->middleware('guest')->except('logout');
  }
}

我的员工登录控制器:

<?php

class StaffLoginController extends Controller {
  use AuthenticatesUsers;

  protected $redirectTo = '/admin';

  protected $guard = 'staff';

  public function __construct() {
    $this->middleware( 'guest' )->except( 'logout' );
  }

  public function showLoginForm() {
    return view( 'auth.staff.login' );
  }

  public function login( Request $request ) {
    $this->validate( $request, [
      'email' => 'required|email',
      'password' => 'required',
    ]);

    if( auth()->guard( 'staff' )->attempt( [
      'email' => $request->input( 'email' ),
      'password' => $request->input( 'password' ),
    ])) {
      return view( 'staff' );
    } else {
      return view( 'auth.staff.login' )->withErrors( [ 'email' => 'Authentication failed' ] );
    }
  }

  protected function guard() {
    return \Auth::guard( 'staff' );
  }
}

我的管理员控制器:

class AdminController extends Controller {
  public function __construct() {
    $this->middleware( 'auth:staff' );
  }

  public function index() {
    return view( 'staff' );
  }
}

我的 RedirectIfStaffUnauthenticated 中间件(在 Http\Kernel.php routeMiddleware 中注册为 'staff' => \SNJ\Http\Middleware\RedirectIfStaffUnauthenticated::class, ):

class RedirectIfStaffUnauthenticated {
  public function handle( $request, Closure $next, $guard = 'staff' ) {
    if ( !Auth::guard( $guard )->check() ) {
      return view( 'auth.staff.login' );
    }

    return $next( $request );
  }
}

UPDATE:

将 web.php 中的路由更改为如下(从 admin/login 和 admin/register 路由中删除了中间件:

Auth::routes();

// staff authentication routes
Route::get( 'admin/login', 'Auth\StaffLoginController@showLoginForm' );
Route::post( 'admin/login', [ 'as' => 'staff.login', 'uses' => 'Auth\StaffLoginController@login' ] );
Route::get( 'admin/register', 'Auth\StaffRegisterController@showRegistrationForm' );
Route::post( 'admin/register', [ 'as' => 'staff.register', 'uses' => 'Auth\StaffRegisterController@register' ] );

Route::group( [ 'middleware' => [ 'staff' ] ], function() {
  Route::get( '/admin', 'AdminController@index' )->name( 'admin' );
});

Route::get('/home', 'HomeController@index')->name('home');

不用找了。还是不行。

尝试更改路由(将所有管理路由放入“staff”中间件中: 授权::路线();

// staff authentication routes

Route::group( [ 'middleware' => [ 'staff' ] ], function() {
  Route::get( 'admin/login', 'Auth\StaffLoginController@showLoginForm' );
  Route::post( 'admin/login', [ 'as' => 'staff.login', 'uses' => 'Auth\StaffLoginController@login' ] );
  Route::get( 'admin/register', 'Auth\StaffRegisterController@showRegistrationForm' );
  Route::post( 'admin/register', [ 'as' => 'staff.register', 'uses' => 'Auth\StaffRegisterController@register' ] );
  Route::get( '/admin', 'AdminController@index' )->name( 'admin' );
});

Route::get('/home', 'HomeController@index')->name('home');

彼此彼此。还是不行。


改变你的StaffLoginController对此,

    <?php

class StaffLoginController extends Controller {
  use AuthenticatesUsers;

  public function showLoginForm() {
    return view( 'auth.staff.login' );
  }

  public function login( Request $request ) {
    $this->validate( $request, [
      'email' => 'required|email',
      'password' => 'required',
    ]);

    if( auth()->guard( 'staff' )->attempt( [
      'email' => $request->input( 'email' ),
      'password' => $request->input( 'password' ),
    ])) {
      return view( 'staff' );
    } else {
      return view( 'auth.staff.login' )->withErrors( [ 'email' => 'Authentication failed' ] );
    }
  }
}

从 AdminController 中删除构造函数,稍后我们将在路由上调用此中间件。

class AdminController extends Controller {

  public function index() {
    return view( 'staff' );
  }
}

您不需要将保护值传递给身份验证中间件,因为您已经定义为用于身份验证的第二个中间件admin routes.

像这样更新你的员工中间件。

class RedirectIfStaffUnauthenticated {
  public function handle( $request, Closure $next, $guard = null ) {
    if ( Auth::guard( $guard )->check() ) {

if(! $guard == 'staff')
                {
                    return redirect()->route( 'staff.login.form' ); 
                }
     }else return redirect()->route( 'staff.login.form' ); 

    return $next( $request );
  }
}

现在更新您的路线。

Route::get( 'admin/login', 'Auth\StaffLoginController@showLoginForm' );
Route::post( 'admin/login', [ 'as' => 'staff.login.submit', 'uses' => 'Auth\StaffLoginController@login' ] );
Route::get( 'admin/register', 'Auth\StaffRegisterController@showRegistrationForm' );
Route::post( 'admin/register', [ 'as' => 'staff.register.submit', 'uses' => 'Auth\StaffRegisterController@register' ] );
Route::group( [ 'middleware' => [ 'staff' ] ], function() {
  Route::get( '/admin', 'AdminController@index' )->name( 'admin' );
});

还将以下行添加到您的unauthenticated函数于Handler.php文件输入app\Exceptions

$guard = array_get($exception->guards(), 0);
        switch ($guard) {
      case 'staff':
        $login = 'admin/login';
        break;
    case 'users':
        $login = 'login';
        break;
      default:
        $login = 'login';
        break;
    }
    return redirect()->guest(route('login'));

请检查您的app/Kernel.php,你可以看到guest是一个别名app/Http/Middleware/RedirectIfAuthenticated.php中间件。

您不需要在两者上调用构造函数web.php文件和控制器构造函数。

这里的RedirectIfStaffUnauthenticated中间件检查根/admin已通过身份验证,并且其保护值为staff,如果没有,它将重定向到/admin/login route.

请阅读Laravel 文档 https://laravel.com/docs/5.5/authentication#authenticating-users为了澄清

希望这可以帮助。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Laravel 5.5 多重身份验证路由问题 的相关文章

  • 如何为 Lumen 的封闭路线指定路线名称?

    您好 我有以下流明路线 router gt get end function Illuminate Http Request request use router controller router gt app gt make App H
  • 如何在Web服务中传递URL

    我想将此 URL 作为网址中的值传递http localhost h2orn php verify php email emails hash hash但是 我只能在 符号之前传递 我想传递所有 URL 我正在使用java网络服务 代码在这
  • ASP.NET MVC - 重写 FormMethod.Get 查询字符串?

    我有一个简单的表单 只有一个文本框和一个提交按钮 该表单基本上将文本框中的值作为查询字符串发送到不同的页面 当我单击提交按钮时 查询字符串采用以下格式 例如 mysite com TargetCode Test1 我希望它以这种格式显示 m
  • 配置 .htaccess 以在 PHP 框架 (Silex) 上工作

    我的 Apache2 本地主机 linux 上有一个工作路径 http localhost lab silex web index php hello name 我想成为 http localhost lab silex hello nam
  • 当用户信息属于公司设备时Rails设备

    我为公司创建了一个设计 我创建了一个用户表 希望我想要存储用户信息密码等 当用户注册时 我希望它创建一个新用户并与公司关联 我公司型号 has one 用户 我的用户模型 所属 公司 我应该如何覆盖注册控制器 此致 Rails 初学者 如果
  • Firebase Cloud Messaging 是否需要身份验证?

    我有一个带有自定义用户身份验证的 php 服务器 我正在尝试与 Firebase 创建聊天 在阅读文档后 我仍然很困惑 我需要为 Firebase 验证我的用户 如果是这样 我应该使用 signInWithCustomToken 来完成吗
  • 使用 php 将 HLS Segment (ts) 视频转换并加入到 mp4

    你好我正在使用这个工具 https github com Ejz HLSDownloader https github com Ejz HLSDownloader将 HLS 视频片段从 m3u8 播放列表下载到 ts 文件中 不 我不知道如
  • SimpleSAMLPHP 重定向循环

    我们正在尝试使用自定义 mysql 数据库设置 sso 但它在以下两个请求之间进入无限循环 POST http 192 168 0 15 simplesaml module php core loginuserpass php 设置Cook
  • PHP header() 和 jquery mobile

    我想使用 php header Location newpage php 进行重定向 我没有收到错误 但 Jquery mobile 似乎无法加载目标页面 并且地址栏仍保留旧地址 请问您有什么建议吗 Thanks 尝试添加data ajax
  • Propel Query 中的动态表名称

    我想知道您是否可以使 propel 查询的表名称动态化 有点像变量 一个例子类似于 DynamicVar Query create 我让它在 ifs 中工作 就像下面的例子一样 但如果更动态地制作 可以删除相当多的行 这些表的设置都是相同的
  • Yii2 DropDownList Onchange 更改自动完成小部件“源”属性?

    我已经尝试过这个 yii2 依赖的自动完成小部件 https stackoverflow com questions 27025791 yii2 dependent autocomplete widget 但我不知道为什么它不起作用 这是我
  • cURL '格式错误的网址'

    This url 在浏览器中工作得很好 但 cURL 返回错误 3 格式错误的 url 关于解决方法有什么想法吗 EDIT 卷曲代码 function get web page url options array CURLOPT RETUR
  • 将秒转换为天、小时、分钟和秒

    我想转换一个变量 uptime这是秒 分为天 小时 分钟和秒 Example uptime 1640467 结果应该是 18 days 23 hours 41 minutes 这可以通过以下方式实现DateTime http php net
  • 避免 SQLite3 中的 SQL 注入

    我正在尝试找出一种避免 SQL 注入的好简单方法 到目前为止我只能提出两个想法 对用户输入进行 Base64 编码 其实不想这样做 使用正则表达式删除不需要的字符 目前正在使用这个 不确定是否100 安全 这是我当前的代码
  • Laravel Socialite:项目中尚未使用 Legacy People API

    我在我的 Web 应用程序上使用 Laravel 5 4 和 Socialite 3 0 进行社交登录 但现在我遇到了一个错误旧版 People API 尚未在项目 xxx 中使用 然后我对socialite包的核心文件做了一些更改 ven
  • 使 div 的大小与其内部图像的大小相同

    我有一个带有以下代码的div HTML div img src img logo png div CSS div imgContainer width 250px height 250px padding 13px 问题是用户可以编辑图像大
  • WordPress 事件按元生效日期排序

    我在获取参数数组以按 Wordpress 中的日期对事件列表进行排序时遇到一些问题 我在 Stack Overflow 和其他地方找到了几个建议的解决方案 但经过大量的试验和错误后 这些解决方案似乎都不起作用 这没什么花哨的 而且应该比这容
  • sqlsrv_num_rows 不返回任何值

    我正在尝试获取查询中返回的行数 while 循环遍历结果有效 但由于某种原因 sqlsrv num rows 不返回任何值 result SELECT from dtable WHERE id2 apple query sqlsrv que
  • PHP 除法浮点值问题

    当我尝试获取余数时 它给出了无效值 我试图获得两位小数的余数 我得到 3 4694469519536E 18 我的价值观是 x 0 1 y 0 005 我尝试了以下方法 echo ed fmod 0 1 0 005 OutPut 3 469
  • 哪个 PHP 5 版本最常用?

    当我开发将在不同配置的客户端 Web 服务 通常使用共享托管 上使用的应用程序时 我应该假设大多数 Web 服务器都具有哪个 PHP 5 版本 例如 5 2 x 5 3 x 等 在所有使用 PHP 版本 5 的网站中 有 84 9 使用版本

随机推荐