在 Laravel 5 中使用多个身份验证防护时重定向循环


Redirect loop when using multiple auth guards in Laravel 5

我正在尝试设置两个身份验证保护:internal(用于普通浏览器请求)和api用于 AJAX 请求。 api是默认的守卫,但我现在专注于让internal守卫工作。

这是我的配置/身份验证.php:

<?php
return [
    'defaults' => [
        'guard' => 'api',
        'passwords' => 'clients',
    ],
    'guards' => [
        'internal' => [
            'driver' => 'session',
            'provider' => 'users',
        ],
        'api' => [
            'driver' => 'token',
            'provider' => 'clients',
        ],
    ],
    'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App'User::class,
        ],
        'clients' => [
             'driver' => 'eloquent',
             'model' => App'Client::class,
        ],
    ],
    'passwords' => [
        'users' => [
            'provider' => 'users',
            'email' => 'auth.emails.password',
            'table' => 'password_resets',
            'expire' => 60,
        ],
    ],
];

这是我的路线.php:

<?php
Route::group([
    'domain' => 'internal.example.com',
    'middleware' => ['web', 'auth:internal']
], function () {
    Route::get('/', function () {
        return view('welcome');
    });
    Route::get('/home', 'HomeController@index');
});
Route::group([
    'domain' => 'internal.example.com',
    'middleware' => [ 'web']
], function () {
    Route::match(['get', 'post'], '/login', 'InternalAuth'InternalAuthController@login');
    Route::get('/logout', 'InternalAuth'InternalAuthController@logout');
});

这是内部身份验证控制器:

<?php
namespace App'Http'Controllers'InternalAuth;
use App'User;
use Validator;
use App'Http'Controllers'Controller;
use Illuminate'Foundation'Auth'ThrottlesLogins;
use Illuminate'Foundation'Auth'AuthenticatesAndRegistersUsers;
class InternalAuthController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Registration & Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users, as well as the
    | authentication of existing users. By default, this controller uses
    | a simple trait to add these behaviors. Why don't you explore it?
    |
    */
    use AuthenticatesAndRegistersUsers, ThrottlesLogins;
    /**
     * Where to redirect users after login / registration.
     *
     * @var string
     */
    protected $redirectTo = '/';
    protected $guard = 'internal';
    /**
     * Create a new authentication controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest', ['except' => 'logout']);
    }
    /**
     * Get a validator for an incoming registration request.
     *
     * @param  array  $data
     * @return 'Illuminate'Contracts'Validation'Validator
     */
    protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => 'required|max:255',
            'email' => 'required|email|max:255|unique:users',
            'password' => 'required|confirmed|min:6',
        ]);
    }
    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return User
     */
    protected function create(array $data)
    {
        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
        ]);
    }
}

对我来说似乎很好。但是当我在浏览器中转到/、/home 或/login 时,我最终会陷入重定向循环。我错过了一些东西...有什么想法吗?

/login指向InternalAuth'InternalAuthController@loginlogin()是一种Illuminate'Foundation'Auth'AuthenticatesAndRegistersUsers的方法(InternalAuth use d),但它不负责返回视图或响应帖子请求。这是一份getLoginpostLogin的工作.

所以,我需要在routes.php中更改它:

Route::match(['get', 'post'], '/login', 'InternalAuth'InternalAuthController@login');

对此:

Route::get('/login', 'InternalAuth'InternalAuthController@getLogin');
Route::post('/login', 'InternalAuth'InternalAuthController@postLogin');