php Yii2 Gii 禁止代码 403 您无权访问此页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26315808/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 18:16:35  来源:igfitidea点击:

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

phpyii2gii

提问by Ionut Flavius Pogacian

I have a servermachine and I am trying to allow my PC ip address to use gii.

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

My PC ip address is 192.168.1.101

我的电脑IP地址是 192.168.1.101

The servermachine ip is 192.168.1.102.

server机的ip是192.168.1.102

I used composerto install the gii module.

composer以前安装gii module.

This is how my composer.jsonsettings look like:

这是我的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": "*"
    },

I have used php initand composer updateand php yii migrate.

我已经使用php initcomposer updatephp yii migrate

I am also logged in in the frontend.

我也登录了frontend.

This is the main.phpfile content:

这是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'
        ],
    ],
];

回答by johnsnails

I had a similar issue and tried all different ipFilter changes. In the end I needed to add this to main-local.php. Which was strange because I had an advanced application, and the settings were for a 'yii2 basic' setup.
http://www.yiiframework.com/doc-2.0/guide-start-gii.html

我遇到了类似的问题并尝试了所有不同的 ipFilter 更改。最后我需要将它添加到 main-local.php。这很奇怪,因为我有一个高级应用程序,并且设置是针对 'yii2 basic' 设置的。
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';}

I should also point out, I did add this to main.php

我还应该指出,我确实将它添加到 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
    ],
],

回答by mfgmicha

After init in devmode I had to change my \backend\config\main-local.phpand add the 'allowedIPs'.

dev模式下初始化后,我必须更改我的\backend\config\main-local.php并添加“allowedIPs”。

Allows ALLIPs, so only recommended for internal dev use! Adjust to your needs.

允许所有IP,因此仅推荐用于内部开发人员使用!根据您的需要进行调整。

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

回答by Doychin Dokov

In the current version of Yii, you should do that in web.php to allow access to Gii:

在当前版本的 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']
];

回答by user2161402

Change your /common/config/main-local.php as follows:

更改您的 /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' => ['*']
        ]
    ]
];

回答by RAPOS

The code worked for me(yii 2.0.8) after adding a exclamation mark(!) before YII_ENV_DEV inside if part::

在 if 部分内部的 YII_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'] = ['*'];

 }

回答by Mihai P.

When in doubt check the logs. There is a warning in there that should tell you something like

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

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

Probably you are wrong about the Ip. I just tried the configuration you have and it works for me.

可能你对 Ip 的看法是错误的。我刚刚尝试了您拥有的配置,它对我有用。

PS1: You should not have Gii enabled on a server but I assume you know that already and this is still the development environment.

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

PS2: there is no passoword setting for gii in Yii2

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

回答by Ionut Flavius Pogacian

I found the answer, and this should be well documented by the yii team!

我找到了答案,yii 团队应该对此有详细记录!

After I used the initcommand, in /frontend/config/main-local.php, I found:

在我使用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';
}

My app is in devmode, and te above declaration, stops my giito work, so ... comment that line

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

回答by Xcoder

I had to add this to my module configurations

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

'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('*'),
    ),