类 yii\filters\auth\HttpHeaderAuth
HttpHeaderAuth 是一个动作过滤器,它支持通过 HTTP 标头进行 HTTP 身份验证。
您可以通过将其作为行为附加到控制器或模块来使用 HttpHeaderAuth,如下所示
public function behaviors()
{
return [
'basicAuth' => [
'class' => \yii\filters\auth\HttpHeaderAuth::class,
],
];
}
HttpHeaderAuth 的默认实现使用 user 应用程序组件的 loginByAccessToken() 方法,并传递 X-Api-Key 标头的值。此实现用于对 API 客户端进行身份验证。
公共属性
| 属性 | 类型 | 描述 | 定义于 |
|---|---|---|---|
| $except | array | 此过滤器不应应用于的动作 ID 列表。 | yii\base\ActionFilter |
| $header | string | HTTP 标头名称 | yii\filters\auth\HttpHeaderAuth |
| $only | array | 此过滤器应应用于的动作 ID 列表。 | yii\base\ActionFilter |
| $optional | array | 此过滤器将应用于的动作 ID 列表,但身份验证失败不会导致错误。 | yii\filters\auth\AuthMethod |
| $owner | yii\base\Component|null | 此行为的所有者 | yii\base\Behavior |
| $pattern | string | 用于提取 HTTP 身份验证值的模式 | yii\filters\auth\HttpHeaderAuth |
| $request | yii\web\Request|null | 当前请求。 | yii\filters\auth\AuthMethod |
| $response | yii\web\Response|null | 要发送的响应。 | yii\filters\auth\AuthMethod |
| $user | yii\web\User|null | 表示用户身份验证状态的用户对象。 | yii\filters\auth\AuthMethod |
公共方法
受保护的方法
| 方法 | 描述 | 定义于 |
|---|---|---|
| getActionId() | 通过将 yii\base\Action::$uniqueId 转换为相对于模块的 ID 来返回动作 ID。 | yii\base\ActionFilter |
| isActive() | 返回一个值,指示过滤器对于给定操作是否处于活动状态。 | yii\base\ActionFilter |
| isOptional() | 检查给定操作的身份验证是否为可选。 | yii\filters\auth\AuthMethod |
属性详细信息
方法详细信息
| public mixed __call ( $name, $params ) | ||
| $name | string |
方法名 |
| $params | array |
方法参数 |
| 返回 | mixed |
方法返回值 |
|---|---|---|
| 抛出 | yii\base\UnknownMethodException |
调用未知方法时 |
public function __call($name, $params)
{
throw new UnknownMethodException('Calling unknown method: ' . get_class($this) . "::$name()");
}
定义于: yii\base\BaseObject::__construct()
构造函数。
默认实现执行两件事
- 使用给定的配置
$config初始化对象。 - 调用 init()。
如果此方法在子类中被重写,建议
- 构造函数的最后一个参数是一个配置数组,如这里的
$config。 - 在构造函数的末尾调用父类的实现。
| public void __construct ( $config = [] ) | ||
| $config | array |
将用于初始化对象属性的键值对 |
public function __construct($config = [])
{
if (!empty($config)) {
Yii::configure($this, $config);
}
$this->init();
}
定义于: yii\base\BaseObject::__get()
返回对象属性的值。
不要直接调用此方法,因为它是一个 PHP 魔术方法,在执行 $value = $object->property; 时会隐式调用。
另见 __set()。
| public mixed __get ( $name ) | ||
| $name | string |
属性名 |
| 返回 | mixed |
属性值 |
|---|---|---|
| 抛出 | yii\base\UnknownPropertyException |
如果属性未定义 |
| 抛出 | yii\base\InvalidCallException |
如果属性是只写 |
public function __get($name)
{
$getter = 'get' . $name;
if (method_exists($this, $getter)) {
return $this->$getter();
} elseif (method_exists($this, 'set' . $name)) {
throw new InvalidCallException('Getting write-only property: ' . get_class($this) . '::' . $name);
}
throw new UnknownPropertyException('Getting unknown property: ' . get_class($this) . '::' . $name);
}
定义于: yii\base\BaseObject::__isset()
检查属性是否已设置,即已定义且不为空。
不要直接调用此方法,因为它是一个 PHP 魔术方法,在执行 isset($object->property) 时会隐式调用。
注意,如果属性未定义,将返回 false。
| public boolean __isset ( $name ) | ||
| $name | string |
属性名或事件名 |
| 返回 | boolean |
命名的属性是否已设置(非空)。 |
|---|---|---|
public function __isset($name)
{
$getter = 'get' . $name;
if (method_exists($this, $getter)) {
return $this->$getter() !== null;
}
return false;
}
定义于: yii\base\BaseObject::__set()
设置对象属性的值。
不要直接调用此方法,因为它是一个 PHP 魔术方法,在执行 $object->property = $value; 时会隐式调用。
另见 __get()。
| public void __set ( $name, $value ) | ||
| $name | string |
属性名或事件名 |
| $value | mixed |
属性值 |
| 抛出 | yii\base\UnknownPropertyException |
如果属性未定义 |
|---|---|---|
| 抛出 | yii\base\InvalidCallException |
如果属性是只读 |
public function __set($name, $value)
{
$setter = 'set' . $name;
if (method_exists($this, $setter)) {
$this->$setter($value);
} elseif (method_exists($this, 'get' . $name)) {
throw new InvalidCallException('Setting read-only property: ' . get_class($this) . '::' . $name);
} else {
throw new UnknownPropertyException('Setting unknown property: ' . get_class($this) . '::' . $name);
}
}
定义于: yii\base\BaseObject::__unset()
将对象属性设置为 null。
不要直接调用此方法,因为它是一个 PHP 魔术方法,在执行 unset($object->property) 时会隐式调用。
注意,如果属性未定义,此方法将不做任何事。如果属性是只读,它将抛出一个异常。
| public void __unset ( $name ) | ||
| $name | string |
属性名 |
| 抛出 | yii\base\InvalidCallException |
如果属性是只读。 |
|---|---|---|
public function __unset($name)
{
$setter = 'set' . $name;
if (method_exists($this, $setter)) {
$this->$setter(null);
} elseif (method_exists($this, 'get' . $name)) {
throw new InvalidCallException('Unsetting read-only property: ' . get_class($this) . '::' . $name);
}
}
| public mixed afterAction ( $action, $result ) | ||
| $action | yii\base\Action |
刚刚执行的操作。 |
| $result | mixed |
操作执行结果 |
| 返回 | mixed |
处理后的操作结果。 |
|---|---|---|
public function afterAction($action, $result)
{
return $result;
}
| public void afterFilter ( $event ) | ||
| $event | yii\base\ActionEvent | |
public function afterFilter($event)
{
$event->result = $this->afterAction($event->action, $event->result);
$this->owner->off(Controller::EVENT_AFTER_ACTION, [$this, 'afterFilter']);
}
定义于: yii\base\ActionFilter::attach()
将行为对象附加到组件。
默认实现将设置 $owner 属性,并按照 events() 中声明的事件处理程序附加事件处理程序。如果您重写此方法,请确保您调用父类的实现。
| public void attach ( $owner ) | ||
| $owner | yii\base\Component |
要附加此行为的组件。 |
public function attach($owner)
{
$this->owner = $owner;
$owner->on(Controller::EVENT_BEFORE_ACTION, [$this, 'beforeFilter']);
}
验证当前用户。
| public yii\web\IdentityInterface|null authenticate ( $user, $request, $response ) | ||
| $user | yii\web\User | |
| $request | yii\web\Request | |
| $response | yii\web\Response | |
| 返回 | yii\web\IdentityInterface|null |
已认证的用户身份。如果未提供身份验证信息,将返回 null。 |
|---|---|---|
| 抛出 | yii\web\UnauthorizedHttpException |
如果提供身份验证信息但无效。 |
public function authenticate($user, $request, $response)
{
$authHeader = $request->getHeaders()->get($this->header);
if ($authHeader !== null) {
if ($this->pattern !== null) {
if (preg_match($this->pattern, $authHeader, $matches)) {
$authHeader = $matches[1];
} else {
return null;
}
}
$identity = $user->loginByAccessToken($authHeader, get_class($this));
if ($identity === null) {
$this->challenge($response);
$this->handleFailure($response);
}
return $identity;
}
return null;
}
定义于: yii\filters\auth\AuthMethod::beforeAction()
此方法在要执行操作之前调用(在所有可能的过滤器之后)。您可以覆盖此方法来为操作进行最后一分钟的准备。
| public boolean beforeAction ( $action ) | ||
| $action | yii\base\Action |
要执行的操作。 |
| 返回 | boolean |
操作是否应该继续执行。 |
|---|---|---|
public function beforeAction($action)
{
$response = $this->response ?: Yii::$app->getResponse();
try {
$identity = $this->authenticate(
$this->user ?: Yii::$app->getUser(),
$this->request ?: Yii::$app->getRequest(),
$response
);
} catch (UnauthorizedHttpException $e) {
if ($this->isOptional($action)) {
return true;
}
throw $e;
}
if ($identity !== null || $this->isOptional($action)) {
return true;
}
$this->challenge($response);
$this->handleFailure($response);
return false;
}
| public void beforeFilter ( $event ) | ||
| $event | yii\base\ActionEvent | |
public function beforeFilter($event)
{
if (!$this->isActive($event->action)) {
return;
}
$event->isValid = $this->beforeAction($event->action);
if ($event->isValid) {
// call afterFilter only if beforeFilter succeeds
// beforeFilter and afterFilter should be properly nested
$this->owner->on(Controller::EVENT_AFTER_ACTION, [$this, 'afterFilter'], null, false);
} else {
$event->handled = true;
}
}
定义于: yii\base\BaseObject::canGetProperty()
返回一个值,指示是否可以读取属性。
如果属性是可读的,则
- 如果类包含与指定名称关联的 getter 方法(在本例中,属性名称不区分大小写);
- 如果类包含具有指定名称的成员变量(当
$checkVars为 true 时);
另请参阅 canSetProperty().
| public boolean canGetProperty ( $name, $checkVars = true ) | ||
| $name | string |
属性名 |
| $checkVars | boolean |
是否将成员变量视为属性 |
| 返回 | boolean |
属性是否可读 |
|---|---|---|
public function canGetProperty($name, $checkVars = true)
{
return method_exists($this, 'get' . $name) || $checkVars && property_exists($this, $name);
}
定义于: yii\base\BaseObject::canSetProperty()
返回一个值,指示是否可以设置属性。
如果属性可写,则满足以下条件:
- 如果类包含与指定名称关联的 setter 方法(在本例中,属性名称不区分大小写);
- 如果类包含具有指定名称的成员变量(当
$checkVars为 true 时);
另请参阅 canGetProperty().
| public boolean canSetProperty ( $name, $checkVars = true ) | ||
| $name | string |
属性名 |
| $checkVars | boolean |
是否将成员变量视为属性 |
| 返回 | boolean |
属性是否可写 |
|---|---|---|
public function canSetProperty($name, $checkVars = true)
{
return method_exists($this, 'set' . $name) || $checkVars && property_exists($this, $name);
}
| public void challenge ( $response ) | ||
| $response | yii\web\Response | |
public function challenge($response)
{
}
::class 代替。
定义于: yii\base\BaseObject::className()
返回此类的完全限定名称。
| public static string className ( ) | ||
| 返回 | string |
此类的完全限定名称。 |
|---|---|---|
public static function className()
{
return get_called_class();
}
定义于: yii\base\ActionFilter::detach()
从组件中分离行为对象。
默认实现将取消设置 $owner 属性,并分离在 events() 中声明的事件处理程序。如果您覆盖此方法,请确保调用父类实现。
| public void detach ( ) |
public function detach()
{
if ($this->owner) {
$this->owner->off(Controller::EVENT_BEFORE_ACTION, [$this, 'beforeFilter']);
$this->owner->off(Controller::EVENT_AFTER_ACTION, [$this, 'afterFilter']);
$this->owner = null;
}
}
定义于: yii\base\Behavior::events()
为 $owner 的事件声明事件处理程序。
子类可以覆盖此方法来声明应将哪些 PHP 回调附加到 $owner 组件的事件。
当行为附加到所有者时,回调将附加到 $owner 的事件;当行为从组件分离时,回调将从事件分离。
回调可以是以下任何一种:
- 此行为中的方法:
'handleClick',等效于[$this, 'handleClick'] - 对象方法:
[$object, 'handleClick'] - 静态方法:
['Page', 'handleClick'] - 匿名函数:
function ($event) { ... }
以下是一个示例:
[
Model::EVENT_BEFORE_VALIDATE => 'myBeforeValidate',
Model::EVENT_AFTER_VALIDATE => 'myAfterValidate',
]
| public array events ( ) | ||
| 返回 | array |
事件(数组键)和相应的事件处理程序方法(数组值)。 |
|---|---|---|
public function events()
{
return [];
}
定义于: yii\base\ActionFilter::getActionId()
通过将 yii\base\Action::$uniqueId 转换为相对于模块的 ID 来返回动作 ID。
| protected string getActionId ( $action ) | ||
| $action | yii\base\Action | |
protected function getActionId($action)
{
if ($this->owner instanceof Module) {
$mid = $this->owner->getUniqueId();
$id = $action->getUniqueId();
if ($mid !== '' && strpos($id, $mid) === 0) {
$id = substr($id, strlen($mid) + 1);
}
} else {
$id = $action->id;
}
return $id;
}
定义于: yii\filters\auth\AuthMethod::handleFailure()
处理身份验证失败。
实现通常应该抛出 UnauthorizedHttpException 来指示身份验证失败。
| public void handleFailure ( $response ) | ||
| $response | yii\web\Response | |
| 抛出 | yii\web\UnauthorizedHttpException | |
|---|---|---|
public function handleFailure($response)
{
throw new UnauthorizedHttpException('Your request was made with invalid credentials.');
}
定义于: yii\base\BaseObject::hasMethod()
返回一个值,指示方法是否已定义。
默认实现是调用 php 函数 method_exists()。当您实现 php 魔术方法 __call() 时,您可以覆盖此方法。
| public boolean hasMethod ( $name ) | ||
| $name | string |
方法名 |
| 返回 | boolean |
方法是否定义 |
|---|---|---|
public function hasMethod($name)
{
return method_exists($this, $name);
}
定义于: yii\base\BaseObject::hasProperty()
返回一个值,指示属性是否已定义。
如果属性定义,则满足以下条件:
- 如果类包含与指定名称关联的 getter 或 setter 方法(在本例中,属性名称不区分大小写);
- 如果类包含具有指定名称的成员变量(当
$checkVars为 true 时);
另请参阅
| public boolean hasProperty ( $name, $checkVars = true ) | ||
| $name | string |
属性名 |
| $checkVars | boolean |
是否将成员变量视为属性 |
| 返回 | boolean |
属性是否定义 |
|---|---|---|
public function hasProperty($name, $checkVars = true)
{
return $this->canGetProperty($name, $checkVars) || $this->canSetProperty($name, false);
}
| public void init ( ) |
public function init()
{
}
定义于: yii\base\ActionFilter::isActive()
返回一个值,指示过滤器对于给定操作是否处于活动状态。
| 受保护 布尔值 isActive ( $action ) | ||
| $action | yii\base\Action |
正在过滤的操作 |
| 返回 | boolean |
过滤器对于给定操作是否处于活动状态。 |
|---|---|---|
protected function isActive($action)
{
$id = $this->getActionId($action);
if (empty($this->only)) {
$onlyMatch = true;
} else {
$onlyMatch = false;
foreach ($this->only as $pattern) {
if (StringHelper::matchWildcard($pattern, $id)) {
$onlyMatch = true;
break;
}
}
}
$exceptMatch = false;
foreach ($this->except as $pattern) {
if (StringHelper::matchWildcard($pattern, $id)) {
$exceptMatch = true;
break;
}
}
return !$exceptMatch && $onlyMatch;
}
| 受保护 布尔值 isOptional ( $action ) | ||
| $action | yii\base\Action |
要检查的操作。 |
| 返回 | boolean |
身份验证是可选的还是强制的。 |
|---|---|---|
protected function isOptional($action)
{
$id = $this->getActionId($action);
foreach ($this->optional as $pattern) {
if (StringHelper::matchWildcard($pattern, $id)) {
return true;
}
}
return false;
}
注册 或 登录 以便发表评论。