2 追蹤者

類別 yii\filters\auth\HttpHeaderAuth

繼承關係yii\filters\auth\HttpHeaderAuth » yii\filters\auth\AuthMethod » yii\base\ActionFilter » yii\base\Behavior » yii\base\BaseObject
實作介面yii\base\Configurable, yii\filters\auth\AuthInterface
子類別yii\filters\auth\HttpBearerAuth
自版本起可用2.0.14
原始碼 https://github.com/yiisoft/yii2/blob/master/framework/filters/auth/HttpHeaderAuth.php

HttpHeaderAuth 是一個動作過濾器,支援透過 HTTP 標頭進行 HTTP 身份驗證。

您可以將 HttpHeaderAuth 作為行為附加到控制器或模組,如下所示

public function behaviors()
{
    return [
        'basicAuth' => [
            'class' => \yii\filters\auth\HttpHeaderAuth::class,
        ],
    ];
}

HttpHeaderAuth 的預設實作使用應用程式元件 userloginByAccessToken() 方法,並傳遞 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

公共方法

隱藏繼承的方法

方法 描述 定義於
__call() 呼叫未定義為類別方法的具名方法。 yii\base\BaseObject
__construct() 建構子。 yii\base\BaseObject
__get() 傳回物件屬性的值。 yii\base\BaseObject
__isset() 檢查屬性是否已設定,即已定義且非 null。 yii\base\BaseObject
__set() 設定物件屬性的值。 yii\base\BaseObject
__unset() 將物件屬性設定為 null。 yii\base\BaseObject
afterAction() 此方法在動作執行後立即調用。 yii\base\ActionFilter
afterFilter() yii\base\ActionFilter
attach() 將行為物件附加到組件。 yii\base\ActionFilter
authenticate() 驗證當前用戶。 yii\filters\auth\HttpHeaderAuth
beforeAction() 此方法在動作即將執行之前調用 (在所有可能的過濾器之後)。您可以覆寫此方法以執行動作的最後準備工作。 yii\filters\auth\AuthMethod
beforeFilter() yii\base\ActionFilter
canGetProperty() 傳回一個值,指示屬性是否可以讀取。 yii\base\BaseObject
canSetProperty() 傳回一個值,指示屬性是否可以設定。 yii\base\BaseObject
challenge() 在身份驗證失敗時產生質詢。 yii\filters\auth\AuthMethod
className() 傳回此類別的完整限定名稱。 yii\base\BaseObject
detach() 從組件中分離行為物件。 yii\base\ActionFilter
events() 宣告 $owner 事件的事件處理程序。 yii\base\Behavior
handleFailure() 處理身份驗證失敗。 yii\filters\auth\AuthMethod
hasMethod() 傳回一個值,指示是否定義了方法。 yii\base\BaseObject
hasProperty() 傳回一個值,指示是否定義了屬性。 yii\base\BaseObject
init() 初始化物件。 yii\base\BaseObject

保護方法

隱藏繼承的方法

方法 描述 定義於
getActionId() 透過將 yii\base\Action::$uniqueId 轉換為相對於模組的 ID,傳回動作 ID。 yii\base\ActionFilter
isActive() 傳回一個值,指示過濾器對於給定的動作是否處於活動狀態。 yii\base\ActionFilter
isOptional() 檢查給定動作的身份驗證是否為可選。 yii\filters\auth\AuthMethod

屬性詳細資訊

隱藏繼承的屬性

$header 公共屬性

HTTP 標頭名稱

public string $header 'X-Api-Key'
$pattern 公共屬性

用於提取 HTTP 身份驗證值的模式

public string $pattern null

方法詳細資訊

隱藏繼承的方法

__call() public method

Defined in: yii\base\BaseObject::__call()

呼叫未定義為類別方法的具名方法。

請勿直接呼叫此方法,因為它是一個 PHP 魔術方法,當未知的方法被調用時,它會被隱式地呼叫。

public mixed __call ( $name, $params )
$name string

方法名稱

$params array

方法參數

return mixed

方法回傳值

throws yii\base\UnknownMethodException

當呼叫未知方法時

                public function __call($name, $params)
{
    throw new UnknownMethodException('Calling unknown method: ' . get_class($this) . "::$name()");
}

            
__construct() public method

Defined in: 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();
}

            
__get() public method

Defined in: yii\base\BaseObject::__get()

傳回物件屬性的值。

請勿直接呼叫此方法,因為它是一個 PHP 魔術方法,當執行 $value = $object->property; 時,它會被隱式地呼叫。

另請參閱 __set()

public mixed __get ( $name )
$name string

屬性名稱

return mixed

屬性值

throws yii\base\UnknownPropertyException

如果屬性未定義

throws 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);
}

            
__isset() public method

Defined in: yii\base\BaseObject::__isset()

檢查屬性是否已設定,即已定義且非 null。

請勿直接呼叫此方法,因為它是一個 PHP 魔術方法,當執行 isset($object->property) 時,它會被隱式地呼叫。

請注意,如果屬性未定義,將會回傳 false。

另請參閱 https://php.dev.org.tw/manual/en/function.isset.php

public boolean __isset ( $name )
$name string

屬性名稱或事件名稱

return boolean

指示已命名的屬性是否已設定 (非 null)。

                public function __isset($name)
{
    $getter = 'get' . $name;
    if (method_exists($this, $getter)) {
        return $this->$getter() !== null;
    }
    return false;
}

            
__set() public method

Defined in: yii\base\BaseObject::__set()

設定物件屬性的值。

請勿直接呼叫此方法,因為它是一個 PHP 魔術方法,當執行 $object->property = $value; 時,它會被隱式地呼叫。

另請參閱 __get()

public void __set ( $name, $value )
$name string

屬性名稱或事件名稱

$value mixed

屬性值

throws yii\base\UnknownPropertyException

如果屬性未定義

throws 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);
    }
}

            
__unset() public method

Defined in: yii\base\BaseObject::__unset()

將物件屬性設定為 null。

請勿直接呼叫此方法,因為它是一個 PHP 魔術方法,當執行 unset($object->property) 時,它會被隱式地呼叫。

請注意,如果屬性未定義,此方法將不會執行任何操作。如果屬性為唯讀,它將拋出例外。

另請參閱 https://php.dev.org.tw/manual/en/function.unset.php

public void __unset ( $name )
$name string

屬性名稱

throws 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);
    }
}

            
afterAction() public method

Defined in: yii\base\ActionFilter::afterAction()

此方法在動作執行後立即調用。

您可以覆寫此方法來為動作執行一些後處理。

public mixed afterAction ( $action, $result )
$action yii\base\Action

剛執行的動作。

$result mixed

動作執行結果

return mixed

處理後的動作結果。

                public function afterAction($action, $result)
{
    return $result;
}

            
afterFilter() public method
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']);
}

            
attach() public method

Defined in: 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']);
}

            
authenticate() public method

驗證當前用戶。

public yii\web\IdentityInterface|null authenticate ( $user, $request, $response )
$user yii\web\User
$request yii\web\Request
$response yii\web\Response
return yii\web\IdentityInterface|null

已驗證的使用者身分。如果未提供驗證資訊,將會回傳 null。

throws 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;
}

            
beforeAction() public method

Defined in: yii\filters\auth\AuthMethod::beforeAction()

此方法在動作即將執行之前調用 (在所有可能的過濾器之後)。您可以覆寫此方法以執行動作的最後準備工作。

public boolean beforeAction ( $action )
$action yii\base\Action

要執行的動作。

return 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;
}

            
beforeFilter() public method
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;
    }
}

            
canGetProperty() public method

Defined in: yii\base\BaseObject::canGetProperty()

傳回一個值,指示屬性是否可以讀取。

如果符合以下條件,屬性是可讀的

  • 類別具有與指定名稱相關聯的 getter 方法(在這種情況下,屬性名稱不區分大小寫);
  • 類別具有具有指定名稱的成員變數(當 $checkVars 為 true 時);

另請參閱 canSetProperty()

public boolean canGetProperty ( $name, $checkVars true )
$name string

屬性名稱

$checkVars boolean

是否將成員變數視為屬性

return boolean

指示屬性是否可讀

                public function canGetProperty($name, $checkVars = true)
{
    return method_exists($this, 'get' . $name) || $checkVars && property_exists($this, $name);
}

            
canSetProperty() public method

Defined in: yii\base\BaseObject::canSetProperty()

傳回一個值,指示屬性是否可以設定。

如果符合以下條件,屬性是可寫的

  • 類別具有與指定名稱相關聯的 setter 方法(在這種情況下,屬性名稱不區分大小寫);
  • 類別具有具有指定名稱的成員變數(當 $checkVars 為 true 時);

另請參閱 canGetProperty()

public boolean canSetProperty ( $name, $checkVars true )
$name string

屬性名稱

$checkVars boolean

是否將成員變數視為屬性

return boolean

指示屬性是否可寫

                public function canSetProperty($name, $checkVars = true)
{
    return method_exists($this, 'set' . $name) || $checkVars && property_exists($this, $name);
}

            
challenge() public method

Defined in: yii\filters\auth\AuthMethod::challenge()

在身份驗證失敗時產生質詢。

例如,可能會產生一些適當的 HTTP 標頭。

public void challenge ( $response )
$response yii\web\Response

                public function challenge($response)
{
}

            
className() public static method
自 2.0.14 版本起已過時。在 PHP >=5.5 版本上,請改用 ::class

Defined in: yii\base\BaseObject::className()

傳回此類別的完整限定名稱。

public static string className ( )
return string

此類別的完整限定名稱。

                public static function className()
{
    return get_called_class();
}

            
detach() public method

Defined in: 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;
    }
}

            
events() public method

Defined in: 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 ( )
return array

事件(陣列鍵)和對應的事件處理器方法(陣列值)。

                public function events()
{
    return [];
}

            
getActionId() protected method (available since version 2.0.7)

Defined in: 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;
}

            
handleFailure() public method

Defined in: yii\filters\auth\AuthMethod::handleFailure()

處理身份驗證失敗。

實作通常應拋出 UnauthorizedHttpException 以指示驗證失敗。

public void handleFailure ( $response )
$response yii\web\Response
throws yii\web\UnauthorizedHttpException

                public function handleFailure($response)
{
    throw new UnauthorizedHttpException('Your request was made with invalid credentials.');
}

            
hasMethod() public method

Defined in: yii\base\BaseObject::hasMethod()

傳回一個值,指示是否定義了方法。

預設實作是對 php 函數 method_exists() 的呼叫。當您實作 php 魔術方法 __call() 時,您可以覆寫此方法。

public boolean hasMethod ( $name )
$name string

方法名稱

return boolean

指示方法是否已定義

                public function hasMethod($name)
{
    return method_exists($this, $name);
}

            
hasProperty() public method

Defined in: yii\base\BaseObject::hasProperty()

傳回一個值,指示是否定義了屬性。

如果符合以下條件,屬性是已定義的

  • 類別具有與指定名稱相關聯的 getter 或 setter 方法(在這種情況下,屬性名稱不區分大小寫);
  • 類別具有具有指定名稱的成員變數(當 $checkVars 為 true 時);

另請參閱

public boolean hasProperty ( $name, $checkVars true )
$name string

屬性名稱

$checkVars boolean

是否將成員變數視為屬性

return boolean

指示屬性是否已定義

                public function hasProperty($name, $checkVars = true)
{
    return $this->canGetProperty($name, $checkVars) || $this->canSetProperty($name, false);
}

            
init() public method

Defined in: yii\base\BaseObject::init()

初始化物件。

在使用給定組態初始化物件後,此方法會在建構子的結尾被呼叫。

public void init ( )

                public function init()
{
}

            
isActive() protected method

Defined in: yii\base\ActionFilter::isActive()

傳回一個值,指示過濾器對於給定的動作是否處於活動狀態。

protected boolean isActive ( $action )
$action yii\base\Action

正在被過濾的動作

return 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() protected method (available since version 2.0.7)

Defined in: yii\filters\auth\AuthMethod::isOptional()

檢查給定動作的身份驗證是否為可選。

另請參閱 $optional

protected boolean isOptional ( $action )
$action yii\base\Action

要檢查的動作。

return boolean

指示驗證是否為選用。

                protected function isOptional($action)
{
    $id = $this->getActionId($action);
    foreach ($this->optional as $pattern) {
        if (StringHelper::matchWildcard($pattern, $id)) {
            return true;
        }
    }
    return false;
}