Laravel的用户认证只需一行代码即可部署完成。
php artisan make:auth
然后,我们就可以访问指定的路由
http:://www.域名/register
http:://www.域名/login
等其他
我们参看一下 php artisan make:auth 代码执行时添加的 ...
- 添加了路由(我们能访问/login /register 等)
//> 在 routes/web.php 中添加如下代码
//> 这里注册了所有 用户认证需要路由
Auth::routes();
//> 用户登录成功跳转路由
Route::get('/home', 'HomeController@index');
我们参看 Auth::auth(); 添加的路由
/**
* Register the typical authentication routes for an application.
*
* @return void
*/
public function auth()
{
// Authentication Routes...
//> 登录页面 GET 加载
$this->get('login', 'Auth\LoginController@showLoginForm')->name('login');
//> 登录数据提交路由 POST
$this->post('login', 'Auth\LoginController@login');
//> 登出数据清除路由
$this->post('logout', 'Auth\LoginController@logout')->name('logout');
// Registration Routes...
//> 注册页面 GET 路由
$this->get('register', 'Auth\RegisterController@showRegistrationForm')->name('register');
//> 注册数据提交路由 POST
$this->post('register', 'Auth\RegisterController@register');
// Password Reset Routes...
//> 下面是密码重置相关路由
$this->get('password/reset', 'Auth\ForgotPasswordController@showLinkRequestForm');
$this->post('password/email', 'Auth\ForgotPasswordController@sendResetLinkEmail');
$this->get('password/reset/{token}', 'Auth\ResetPasswordController@showResetForm');
$this->post('password/reset', 'Auth\ResetPasswordController@reset');
}
auth()方法注册的路由都是Auth开始的,这是由于Route路由控制都被指定了App\Http\Controller\ 这里
![](https://img.haomeiwen.com/i3240400/91b3b808a47e441c.png)
[=========]
- App\Http\Controllers\Auth\LoginController 控制器 处理登录相关逻辑
//> LoginController控制器支持一下属性
$this->maxAttempts = 3; //> 最大登录失败次数
$this->decayMinutes = 1; //> 登录maxAttempts次失败等待指定分钟数
//> 源码参考 trait
/**
* Get the maximum number of attempts to allow.
*
* @return int
*/
public function maxAttempts()
{
//> property_exists() 判断类或对象指定属性是否存在
return property_exists($this, 'maxAttempts') ? $this->maxAttempts : 5;
}
/**
* Get the number of minutes to throttle for.
*
* @return int
*/
public function decayMinutes()
{
return property_exists($this, 'decayMinutes') ? $this->decayMinutes : 1;
}
//> 判断当前用户登录是否同意用户标志 用户名+ip
/**
* Get the throttle key for the given request.
*
* @param \Illuminate\Http\Request $request
* @return string
*/
protected function throttleKey(Request $request)
{
return Str::lower($request->input($this->username())).'|'.$request->ip();
}
//> 判断当前用户登录是否超出次数 源码
/**
* Determine if the given key has been "accessed" too many times.
*
* @param string $key
* @param int $maxAttempts
* @param float|int $decayMinutes
* @return bool
*/
public function tooManyAttempts($key, $maxAttempts, $decayMinutes = 1)
{
//> 用户名+ip+:lockout 表示当前用户已锁住
if ($this->cache->has($key.':lockout')) {
return true;
}
//> $this->attempts($key)从缓存里取之前失败次数
if ($this->attempts($key) >= $maxAttempts) {
//> 超过最大登录次数,锁住登录
$this->lockout($key, $decayMinutes);
//> 清除 用户名+ip 为key的缓存数据
$this->resetAttempts($key);
return true;
}
return false;
}
网友评论