// 判断当前用户是否登录 public function check(); // 判断当前用户是否是游客(未登录) public function guest(); // 获取当前认证的用户 public function user(); // 获取当前认证用户的 id,严格来说不一定是 id,应该是上个模型中定义的唯一的字段名 public function id(); // 根据提供的消息认证用户 public function validate(array $credentials = []); // 设置当前用户 public function setUser(Authenticatable $user);
StatefulGuard 接口
Illuminate\Contracts\Auth\StatefulGuard
StatefulGuard 接口继承自 Guard 接口,除了 Guard 里面定义的一些基本接口外,还增加了更进一步、有状态的 Guard.
新添加的接口有这些:
// 尝试根据提供的凭证验证用户是否合法 public function attempt(array $credentials = [], $remember = false); // 一次性登录,不记录session or cookie public function once(array $credentials = []); // 登录用户,通常在验证成功后记录 session 和 cookie public function login(Authenticatable $user, $remember = false); // 使用用户 id 登录 public function loginUsingId($id, $remember = false); // 使用用户 ID 登录,但是不记录 session 和 cookie public function onceUsingId($id); // 通过 cookie 中的 remember token 自动登录 public function viaRemember(); // 登出 public function logout();
Laravel 中默认提供了 3 中 guard :RequestGuard,TokenGuard,SessionGuard.
RequestGuard
Illuminate\Auth\RequestGuard
RequestGuard 是一个非常简单的 guard. RequestGuard 是通过传入一个闭包来认证的。可以通过调用 Auth::viaRequest 添加一个自定义的 RequestGuard.
SessionGuard
Illuminate\Auth\SessionGuard
SessionGuard 是 Laravel web 认证默认的 guard.
TokenGuard
Illuminate\Auth\TokenGuard
TokenGuard 适用于无状态 api 认证,通过 token 认证.
实现自定义 Guard
App\Auth\UserGuard.php
<?php namespace App\Auth; use Illuminate\Http\Request; use Illuminate\Auth\GuardHelpers; use Illuminate\Contracts\Auth\Guard; use Illuminate\Contracts\Auth\UserProvider; class UserGuard implements Guard { use GuardHelpers; protected $user = null; protected $request; protected $provider; /** * The name of the query string item from the request containing the API token. * * @var string */ protected $inputKey; /** * The name of the token "column" in persistent storage. * * @var string */ protected $storageKey; /** * The user we last attempted to retrieve * @var */ protected $lastAttempted; /** * UserGuard constructor. * @param UserProvider $provider * @param Request $request * @return void */ public function __construct(UserProvider $provider, Request $request = null) { $this->request = $request; $this->provider = $provider; $this->inputKey = 'Authorization'; $this->storageKey = 'api_token'; } /** * Get the currently authenticated user. * @return \Illuminate\Contracts\Auth\Authenticatable|null */ public function user() { if(!is_null($this->user)) { return $this->user; } $user = null; $token = $this->getTokenForRequest(); if(!empty($token)) { $user = $this->provider->retrieveByCredentials( [$this->storageKey => $token] ); } return $this->user = $user; } /** * Rules a user's credentials. * @param array $credentials * @return bool */ public function validate(array $credentials = []) { if (empty($credentials[$this->inputKey])) { return false; } $credentials = [$this->storageKey => $credentials[$this->inputKey]]; $this->lastAttempted = $user = $this->provider->retrieveByCredentials($credentials); return $this->hasValidCredentials($user, $credentials); } /** * Determine if the user matches the credentials. * @param mixed $user * @param array $credentials * @return bool */ protected function hasValidCredentials($user, $credentials) { return !is_null($user) && $this->provider->validateCredentials($user, $credentials); } /** * Get the token for the current request. * @return string */ public function getTokenForRequest() { $token = $this->request->header($this->inputKey); return $token; } /** * Set the current request instance. * * @param \Illuminate\Http\Request $request * @return $this */ public function setRequest(Request $request) { $this->request = $request; return $this; } }
在 AppServiceProvider 的 boot 方法添加如下代码:
App\Providers\AuthServiceProvider.php