【发布时间】:2016-06-03 19:54:53
【问题描述】:
我正在尝试设置两个身份验证防护:internal(用于正常浏览器请求)和api 用于 AJAX 请求。 api 是默认的守卫,但我现在专注于让internal-guard 工作。
这是我的配置/auth.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,
],
],
];
这是我的 routes.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');
});
这是 InternalAuthController:
<?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 时,我最终会进入重定向循环。 我错过了一些东西......有什么想法吗?
【问题讨论】:
-
从你的公共路由中移除 auth 中间件,比如'/','login'等。只对那些需要授权用户的路由实施。
-
在
/上的Auth 中间件旨在。/login上没有 auth 中间件。但我玩了一圈发现,当/不受保护时,/login重定向到/。所以这导致了重定向循环。但是为什么/login在我没有通过身份验证的情况下这样做呢? -
检查中间件中的 auth.php 或 Authorization.php。
-
没什么可疑的...
auth.php在上面的问题中。app/Http/Middleware/Authenticate.php根本没有被修改。
标签: php redirect laravel-routing laravel-5.2