2015-02-04 10 views
7

Ho sempre ricevuto "Stai richiedendo una credenziale non valida". ma ho bisogno di avere un endpoint pubblica specificamente azione "vista" che tutti possono accedere whitout inviare token di accesso e mantenere le altre azioni con la convalida del tokenYii2: Posso applicare il comportamento dell'autenticatore solo ad alcune azioni?

Questo fa parte del mio regolatore Api:

/** 
* @inheritdoc 
*/ 
public function behaviors() 
{ 
    return [ 
     'contentNegotiator' => [ 
      'class' => ContentNegotiator::className(), 
      'formats' => [ 
       'application/json' => Response::FORMAT_JSON, 
       //'application/xml' => Response::FORMAT_XML, 
      ], 
     ], 
     'verbFilter' => [ 
      'class' => VerbFilter::className(), 
      'actions' => $this->verbs(), 
     ], 
     'access' => [ 
      'class' => AccessControl::className(), 
      'only' => ['view'], 
      'rules' => [ 
       [ 
        'actions' => ['view'], 
        'allow' => true, 
        'roles' => ['?'], 
       ], 
      ], 
     ], 
     'authenticator' => [ 
      'class' => CompositeAuth::className(), 
      'authMethods' => [ 
       HttpBasicAuth::className(), 
       HttpBearerAuth::className(), 
       QueryParamAuth::className(), 
      ], 
     ], 
     'rateLimiter' => [ 
      'class' => RateLimiter::className(), 
     ], 
    ]; 
} 

provo utilizzando:

'access' => [ 
    'class' => AccessControl::className(), 
    'only' => ['view'], 
    'rules' => [ 
     [ 
      'actions' => ['view'], 
      'allow' => true, 
      'roles' => ['?'], 
     ], 
    ], 

],

ma il comportamento autenticatore non permettere che il mio punto di vista l'azione è un'azione pubblica

risposta

15

ho trovato le soluzioni è solo con 'solo' o 'tranne' chiave sul comportamento autenticatore

'authenticator' => [ 
      'class' => CompositeAuth::className(), 
      'except' => ['view'], 
      'authMethods' => [ 
       HttpBasicAuth::className(), 
       HttpBearerAuth::className(), 
       QueryParamAuth::className(), 
      ], 
     ], 

Fonte: https://github.com/yiisoft/yii2/issues/4575 https://github.com/yiisoft/yii2/blob/master/docs/guide/structure-filters.md#using-filters-

Grazie, Godetevi Yii2 e riposo;)

0

Esistono due proprietà per ignorare l'autenticatore sulle azioni 1. only => ignora il resto dell'azione nella matrice configurata 2. tranne => di passare solo configurato nell'array

public function behaviors() 
{ 
    $behaviors = parent::behaviors(); 
    $behaviors['authenticator'] = [ 
     'class' => CompositeAuth::className(), 
     'except' => ['login', 'register','regenerate'], 
     //'only'=>['index'], 
     'authMethods' => [ 
      [ 
       'class' => HttpBasicAuth::className(), 
       'auth' => function ($username, $password) { 
        $user = User::findByLogin($username); 
        return $user->validatePassword($password) 
         ? $user 
         : null; 
       } 
      ], 
      HttpBearerAuth::className(), 
      QueryParamAuth::className() 
     ], 
    ]; 
    return $behaviors; 
} 
Problemi correlati