Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/248.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
CakePHP 2.4认证->;login()始终返回TRUE_Php_Cakephp_Authentication_Login_Cakephp 2.0 - Fatal编程技术网

CakePHP 2.4认证->;login()始终返回TRUE

CakePHP 2.4认证->;login()始终返回TRUE,php,cakephp,authentication,login,cakephp-2.0,Php,Cakephp,Authentication,Login,Cakephp 2.0,我正在为我妈妈开发一个带有cake 2.4的简单登录应用程序。下面是用户模型的代码 App::uses('SimplePasswordHasher', 'Controller/Component/Auth'); class User extends AppModel{ public $validate = array( 'username' => array( 'required' =>

我正在为我妈妈开发一个带有cake 2.4的简单登录应用程序。下面是用户模型的代码

App::uses('SimplePasswordHasher', 'Controller/Component/Auth');

class User extends AppModel{
        public $validate = array(
                'username' => array(
                        'required' => array(
                                'rule' => array('notEmpty'),
                                'message' => 'A username is required'
                        )
                ),
                'password' => array(
                        'required' => array(
                                'rule' => array('notEmpty'),
                                'message' => 'A password is required'
                        )
                )
        );

        public function beforeSave($options = array()) {
                if(isset($this->data[$this->alias]['password'])) {
                        $passwordHasher = new SimplePasswordHasher();
                        $this->data[$this->alias]['password'] = $passwordHasher->hash(
                        $this->data[$this->alias]['password']
                        );
                }
                return true;
        }
}
我的UsersController代码是

class UsersController extends AppController{
        public $helpers = array('Html','Form');

        public function beforeFilter(){
                parent::beforeFilter();
                $this->Auth->allow('add');
        }
        public function add(){
                if($this->request->is('post')){
                        $this->User->create();
                        if($this->User->save($this->request->data)){
                                $this->Session->setFlash(__('The user has been saved'));
                                return $this->redirect(array('action' => 'index'));
                        }
                        $this->Session->setFlash(__('The user could not be saved. Please, try again.'));
                }
        }
        public function login(){
                $this->layout = 'mlayout';
                if($this->request->is('post')){
                        debug($this->Auth->login());
                        var_dump($this->request->data);
                        if($this->Auth->login()){
                                $this->Session->setFlash('Logged In');
                                //return $this->redirect($this->Auth->redirectUrl());
                        }else{
                                $this->Session->setFlash(__('Usuario o Contraseña invalido, intentelo de nuevo.'));
                        }
                }
        }
        public function logout(){
                return $this->redirect($this->Auth->logout());
        }
}
AppController代码:

class AppController extends Controller{

        public $components = array('Session',
                'Auth' => array(
                        'loginRedirect' => array(
                                'controller' => 'homes',
                                'action' => 'index'
                        ),
                        'logoutRedirect' => array(
                                'controller' => 'homes',
                                'action' => 'index'
                        )
                )
        );

        public function beforeFilter(){
                $this->Auth->allow('index','view','login');
        }
}
Login.ctp

                <table id="formtable">
                <form id="UserForm" method="post" action="/mercadito/users/login">
                <tr>
                        <td align="right">Login -> </td>
                        <td><input type="text" name="username" style="width: 150px; height: 30px;"/></td>
                </tr>
                <tr>
                        <td align="right">Contraseña -> </td>
                        <td><input type="password" name="password" style="width: 150px; height: 30px;"/></td>
                </tr>
                <tr>
                        <td></td>
                        <td align="center"><input type="submit" value="ENTRAR" style="width: 100px; height: 30px;"/></td>
                </tr>
                </form>
                </table>

登录->
逆逆逆逆逆逆逆逆逆逆逆逆逆逆逆逆逆逆逆逆逆逆逆逆逆逆逆逆逆逆逆逆逆逆逆逆逆逆逆逆逆逆逆逆逆逆逆逆逆逆逆逆逆逆逆逆逆逆
调试($this->Auth->login())返回: /app/Controller/userscocontroller.php(第23行) 真的

var_dump()返回: 数组(2){[“用户名”]=>string(9)“ddfdsffsd”[“密码”]=>string(9)“fdsfdssfd”}


无论登录表单中的输入是什么,每次都会发生这种情况。

您需要配置授权处理程序以使用控制器中的beforeFilter。请参阅:

将“授权”=>“控制器”添加到AppController:

class AppController extends Controller{

     public $components = array('Session',
            'Auth' => array(
                    'loginRedirect' => array(
                            'controller' => 'homes',
                            'action' => 'index'
                    ),
                    'logoutRedirect' => array(
                            'controller' => 'homes',
                            'action' => 'index'
                    ),
                    'authorize' => 'Controller'
            )
    );

    public function beforeFilter(){
            $this->Auth->allow('index','view','login');
    }

}

试着输入您的
Auth
数组:
'authenticate'=>数组('Form')
谢谢!但现在,它总是返回FALSE(您可能需要将“允许登录”部分移动到UsersController。由于您正在执行路由到UsersController的请求,因此它将使用该控制器的beforeFilter检查用户是否有权访问该特定操作。我更改了“允许->(“登录”)的位置。),然后我检查了我的密码是否经过了哈希处理,确实如此。但问题仍然存在。