Yii2 - 验证IP上的AccessControl

时间:2017-07-06 12:58:24

标签: yii2

我想将控制器的访问权限限制为只有一个IP(或IP列表)。

配置的正确方法是什么? (例如,我希望只有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'],
            ],
        ],
    ];
}

1 个答案:

答案 0 :(得分:4)

变化

'roles' => ['?'] 

'roles' => ['@']