2017-07-06 51 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' => ['@']