删除特定操作的htpasswd保护

时间:2018-02-15 06:33:06

标签: .htaccess cakephp cakephp-3.x restriction .htpasswd

我在cakephp 3中有一个带有htpasswd保护的网站。我想访问没有密码的特定操作,例如/ apis / models

我可以通过将以下代码放在webroot / .htaccess文件中来删除特定文件的密码保护,但是如何在cakephp 3中为特定操作实现此目的。

<Files "test.php">
    Satisfy Any
</Files>

1 个答案:

答案 0 :(得分:0)

我想如果我必须这样做,我会在控制器中做到这一点。在cakephp中,我们可以轻松选择身份验证选项,包括基本身份验证。

class AppController extends Controller
{

    public function initialize() {

        parent::initialize();

        $this->loadComponent('Auth', [
            'authenticate' => [
                'Basic' => [
                    'fields' => [
                        //  username and password from database
                        'username' => 'email', 
                        'password' => 'password'
                    ],
                ],
            ],
            'storage' => 'Memory',
            'unauthorizedRedirect' => false
        ]);     

        /**
         *  Allowed actions
         *  ['Controller' => ['method' => 1, 'method' => 1]]
         */
        $excluded_actions = [
            'Users' => [
                'index' => 1, 
                'edit' => 1
            ]
        ];

        $controller = $this->request->getParam('controller');
        $method = $this->request->getParam('action');

        if(isset($excluded_actions[$controller]) && isset($excluded_actions[$controller][$method])) {

            $this->Auth->allow();
        }

    }
}