Yii2 Gii禁止代码403您不允许访问此页面


Yii2 Gii Forbidden code 403 You are not allowed to access this page

我有一台server机器,我正试图允许我的PC ip地址使用gii

我的电脑ip地址是192.168.1.101

server机器ip是192.168.1.102

我用composer安装了gii module

这就是我的composer.json设置的样子:

"require": {
        "php": ">=5.4.0",
        "yiisoft/yii2": "*",
        "yiisoft/yii2-bootstrap": "*",
        "yiisoft/yii2-swiftmailer": "*",
        "yiisoft/yii2-gii": "*"
    },
    "require-dev": {
        "yiisoft/yii2-codeception": "*",
        "yiisoft/yii2-debug": "*",
        "yiisoft/yii2-gii": "*",
        "yiisoft/yii2-faker": "*"
    },

我使用了php initcomposer update以及php yii migrate

我也登录了frontend

这是main.php文件内容:

return [
    'id' => 'app-frontend',
    'basePath' => dirname(__DIR__),
    'bootstrap' => ['gii'],
    'controllerNamespace' => 'frontend'controllers',
    'components' => [
        'user' => [
            'identityClass' => 'common'models'User',
            'enableAutoLogin' => true,
        ],
        'log' => [
            'traceLevel' => YII_DEBUG ? 3 : 0,
            'targets' => [
                [
                    'class' => 'yii'log'FileTarget',
                    'levels' => ['error', 'warning'],
                ],
            ],
        ],
        'errorHandler' => [
            'errorAction' => 'site/error',
        ],
    ],
    'params' => $params,
    'modules' => [
        'gii' => [
            'class' => 'yii'gii'Module',
            'allowedIPs' => ['127.0.0.1', '::1', '192.168.1.101'],
            'password' => '123456'
        ],
    ],
];

我遇到了类似的问题,并尝试了所有不同的ipFilter更改。最后,我需要将其添加到main-local.php中。这很奇怪,因为我有一个高级应用程序,并且设置是为"yii2基本"设置
http://www.yiiframework.com/doc-2.0/guide-start-gii.html

if (!YII_ENV_TEST) {
// configuration adjustments for 'dev' environment
$config['bootstrap'][] = 'debug';
$config['modules']['debug'] = 'yii'debug'Module';
$config['bootstrap'][] = 'gii';
$config['modules']['gii'] = 'yii'gii'Module';}

我还应该指出,我确实将此添加到了main.php 中

    'modules' => [
    'gii' => [
        'class' => 'yii'gii'Module',
        'allowedIPs' => ['127.0.0.1', '::1', '192.168.1.*', 'XXX.XXX.XXX.XXX'] // adjust this to your needs
    ],
],

dev模式下初始化后,我不得不更改'backend'config'main-local.php并添加"allowedIP"。

允许ALLIP,因此仅建议内部开发人员使用!根据您的需要进行调整。

$config['bootstrap'][] = 'gii';
$config['modules']['gii'] = [
    'class' => 'yii'gii'Module',
    'allowedIPs' => ['*'],
];

更改/common/config/main-local.php如下:

    return [
    'components' => [
        'db' => [
            'class' => 'yii'db'Connection',
            'dsn' => 'mysql:host=localhost;dbname=YourDatbase',
            'username' => 'YourDBUserName',
            'password' => 'YourDBPassword',
            'charset' => 'utf8',
        ],
        'mailer' => [
            'class' => 'yii'swiftmailer'Mailer',
            'viewPath' => '@common/mail',
            // send all mails to a file by default. You have to set
            // 'useFileTransport' to false and configure a transport
            // for the mailer to send real emails.
            'useFileTransport' => true,
        ],
    ],
    // Add this to get debug and gii to work
    'modules' => [
        'gii' => [
            'class' => 'yii'gii'Module',
             // permits any and all IPs
             // you should probably restrict this
            'allowedIPs' => ['*']
        ],
        'debug' => [
            'class' => 'yii'debug'Module',
             // permits any and all IPs
             // you should probably restrict this
            'allowedIPs' => ['*']
        ]
    ]
];

在当前版本的Yii中,您应该在web.php中这样做,以允许访问Gii:

//$config['modules']['gii'] = 'yii'gii'Module'; // <--- replace this line
$config['modules']['gii'] = [
    'class' => 'yii'gii'Module',
    'allowedIPs' => ['XXX.XXX.XXX.XXX', 'YYY.YYY.YYY.YYY']
];

在if part::内的YI_ENV_DEV之前添加感叹号(!)后,代码对我(yii 2.0.8)有效

if (!YII_ENV_TEST) {
     // configuration adjustments for 'dev' environment
     $config['bootstrap'][] = 'debug';
     $config['modules']['debug'] = [
          'class' => 'yii'debug'Module',
     ];
     $config['modules']['debug']['allowedIPs'] = ['*'];
     $config['bootstrap'][] = 'gii';
     $config['modules']['gii'] = [
          'class' => 'yii'gii'Module',
     ];
     $config['modules']['gii']['allowedIPs'] = ['*'];
 }

如果有疑问,请检查日志。里面有一个警告,应该会告诉你一些类似的东西

10  06:00:19.040    warning yii'gii'Module::checkAccess Access to Gii is denied due to IP address restriction. The requested IP is 127.0.0.1
11  06:00:19.041    error   yii'web'HttpException:403   exception 'yii'web'ForbiddenHttpException' with message 'You are not allowed to access this page.' in ......./html/vendor/yiisoft/yii2-gii/Module.php:112

也许你对Ip的看法是错误的。我刚刚尝试了你的配置,它对我有效。

PS1:你不应该在服务器上启用Gii,但我想你已经知道了,这仍然是开发环境。

PS2:Yii2 中没有gii的密码设置

我找到了答案,yii团队应该对此进行充分的记录!

在我使用init命令后,在/frontend/config/main-local.php中,我发现:

if (!YII_ENV_TEST) {
    // configuration adjustments for 'dev' environment
    $config['bootstrap'][] = 'debug';
    $config['modules']['debug'] = 'yii'debug'Module';
    $config['bootstrap'][] = 'gii';
    $config['modules']['gii'] = 'yii'gii'Module';
}

我的应用程序处于dev模式,并且上面的声明使我的gii停止工作,所以。。。注释行

我不得不将其添加到我的模块配置中

'gii' => array(
        'generatorPaths' => array('bootstrap.gii'),
        'class' => 'system.gii.GiiModule',
        'password' => 'aaa123',
        // If removed, Gii defaults to localhost only. Edit carefully to taste.
        'ipFilters' => array('*'),
    ),