HTTPBearerAuth を使用してベースコントローラーで認証しています
public function behaviors()
{
$behaviors['authenticator'] = [
'class' => CompositeAuth::className(),
'authMethods' => [
HttpBearerAuth::className(),
],
];
return $behaviors;
}
UserController でベースコントローラーを拡張AccessControl
し、ゲストユーザーがログインにアクセスできるようにします。
public function behaviors()
{
$behaviors = parent::behaviors();
$behaviors['access'] = [
'class' => AccessControl::className(),
'only' => ['login', 'logout', 'signup'],
'rules' => [
[
'actions' => ['login'],
'allow' => true,
'roles' => ['?'],
],
[
'actions' => ['logout'],
'allow' => true,
'roles' => ['@'],
],
],
];
$behaviors['verbs'] = [
'class' => VerbFilter::className(),
'actions' => [
'logout' => ['post'],
],
];
return $behaviors;
}
認証の詳細なしでログインにアクセスしようとすると、
{
"name": "Unauthorized",
"message": "You are requesting with an invalid credential.",
"code": 0,
"status": 401,
"type": "yii\web\UnauthorizedHttpException"
}
Unauthorized 401.認証の詳細で、私は得る
{
"name": "Forbidden",
"message": "You are not allowed to perform this action.",
"code": 0,
"status": 403,
"type": "yii\web\ForbiddenHttpException"
}
なぜ私はこれを手に入れたのですか?