2017-07-06 54 views
3

我想限制只有一個IP(或IP列表)的控制器的訪問。Yii2 - 通過IP驗證AccessControl

什麼是正確的配置方式? (例如,我只希望IP 172.19.37.175有權訪問index.php?r=painel/restrict)。

我試着這樣說:

public function behaviors() 
{ 
    return [ 
     'access' => [ 
      'class' => AccessControl::classname(), 
      'only' => ['index'], 
      'rules' => [ 
       [ 
        'allow' => true, 
        'roles' => ['?'], 
        'ips' => ['172.19.37.175'], 
       ], 
      ], 
      'denyCallback' => function ($rule, $action) { 
      throw new \Exception('You are not allowed to access this page'); 
        }     
     ], 
     'verbs' => [ 
      'class' => VerbFilter::className(), 
      'actions' => [ 
       'delete' => ['post'], 
      ], 
     ], 
    ]; 
} 
+0

你的代碼..似乎是正確的..什麼是你的問題..你有錯誤? worng結果? – scaisEdge

+0

當試圖訪問我的IP 172.19.37.175時,它顯示:您不能訪問此頁訪問索引 – gugoan

+0

?的index.php?R =索引? – scaisEdge

回答

2

變化

'roles' => ['?'] 

'roles' => ['@']