Cakephp 3.0登录返回false每次 [英] Cakephp 3.0 Login returns false everytime

查看:201
本文介绍了Cakephp 3.0登录返回false每次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图登录我的Cakephp 3.0网站上的用户。我的表有一个'username'和'password'字段,命名为。我可以添加一个用户,我的密码成功获得哈希和存储在我的表中。即使这样,我也无法登录一次。

I am trying to log in a user on my Cakephp 3.0 site. My table has a 'username' and 'password' field, named as is. I can add a user and my password is successfully getting hashed and stored in my table. Despite this, I am unable to log in even once.

我的AppController initialize()方法:

My AppController initialize() method:

$this->loadComponent('Auth',['loginAction' => [
                            'controller' => 'ShopOwners',
                            'action' => 'login'
                         ],'authenticate' => [
                              'Form' => [
                                'fields' => [
                                  'username' => 'username',
                                  'password' => 'password',
                                 ],
                                 'userModel' => 'ShopOwners'
                               ]
                         ],'loginRedirect' => [
                             'controller' => 'ShopOwners',
                             'action' => 'view'
                         ],
                    ]);
$this->Auth->allow(['home','add']);

我的控制器中的登录视图功能

My login view function in my Controller

public function login(){
    $this->log('Inside login','debug');
    if($this->request->is('post')){
       $this->log('Inside login is post','debug');
       $shopOwner = $this->Auth->identify();
       if($shopOwner){
          $this->log('Inside is owner','debug');
          $this->Auth->setUser($shopOwner);
          return $this->redirect($this->Auth->redirectUrl());
       }
      $this->Flash->error(__('Invalid username or password, try again'));
    }
}



最后我的登录视图文件login.ctp



And finally my login view file login.ctp

<h1>Login</h1>
<?= $this->Form->create(); ?>
<?= $this->Form->input('username'); ?>
<?= $this->Form->input('password'); ?>
<?= $this->Form->button(__('Login')); ?>
<?= $this->Form->end(); ?>

我已经尝试了许多在网站和其他地方提供的解决方案来尝试和解决这个问题。这些包括:

I have tried many solutions offered on the site and elsewhere to try and fix this. These include:


  1. protected $ _accessible已正确设置

  2. 检查表列名称是用户名和密码正确

  3. SQL查询返回1行,通过控制台检查,但Auth-> identify()不返回任何内容。

  4. 在输入错误的密码时,输入了许多新用户,没有任何问题得到解决。

  5. 检查我的文件/表/列名称

  6. 还通过使用
    $ this-> Auth-> user('username')检查用户是否已经登录没有返回任何东西

  1. protected $_accessible is set corrrectly
  2. Checked that table column names are username and password exactly
  3. SQL query is returning 1 row, as checked through the Console, but Auth->identify() is not returning anything.
  4. Made many new users in case I had entered the wrong password, no luck nothing cleared
  5. Checked my file/table/column names to make everything matches[at least I think it's all fine]
  6. Also checked if the user is already logged in by using $this->Auth->user('username') which returned nothing

新的Cakephp,现在被困在这一天了。非常感谢任何帮助。

New to Cakephp, and have been stuck at this one for a whole day now. Really appreciate any help.

编辑:有人可以告诉我为什么identify()不返回任何东西。我在控制器的登录函数中做了跟踪,并返回了

Can someone at least tell me why identify() is not returning anything. I did a trace in my login function in the controller and it returned

App\Controller\ShopOwnersController::login() - 
 APP/Controller\ShopOwnersController.php, line 132
Cake\Controller\Controller::invokeAction() - 
 CORE\src\Controller\Controller.php, line 405
Cake\Routing\Dispatcher::_invoke() - 
 CORE\src\Routing\Dispatcher.php, line 114
Cake\Routing\Dispatcher::dispatch() - 
  CORE\src\Routing\Dispatcher.php, line 87
[main] - ROOT\webroot\index.php, line 37

结束于:

Undefined variable: _SESSION [CORE\src\Network\Session.php, line 436]

为什么它在index.php停止?什么可能是错误?在索引中我检查了

Why is it stopping at index.php? What could be wrong? In the index I checked

print_r(Request::createFromGlobals());

在其中我可以看到我的数据数组包含用户名和密码,但仍然没有。密码是文本而不是散列,是问题

In which I can see my data array contain the username and password, but still nothing. The password is text and not hashed, is that the problem??

编辑2:我的请求对象包含以下

EDIT 2: My Request Object contains the following

[params] => Array ( [plugin] => [controller] => [action] => [_ext] => [pass] => Array ( ) ) 
[data] => Array ( [username] => user9 [password] => user9 ) 
[query] => Array ( )

为什么我的查询数组为空?它应该是?

Why is my query Array empty? Is it supposed to be??

编辑3 - 我试图发送一个散列版本的我的密码和重置request-> data ['密码']到散列版。这不起作用,因为

EDIT 3 - I tried to send in a hashed version of my password and reset request->data['password'] to the hashed version. This did not work because


  1. 每次使用相同字词的散列版本不同。我为此使用了(new DefaultPasswordHasher) - > hash()

  2. 我试图从数据库直接复制粘贴密码的哈希版本到我的登录表单。没有运气


推荐答案

如果您不使用名为Users和密码,你需要告诉CakePHP关于它,如本手册的这一节所解释:

If you are not using a table called "Users" for storing the username and the password, you need to tell CakePHP about it, as explained in this section of the manual:

http://book.cakephp.org/3.0/en/controllers/components/authentication.html#configuring-authentication -handlers

基本上你需要将它添加到你的AuthComponent配置中:

Basically you need to add this to your AuthComponent config:

$this->loadComponent('Auth',['loginAction' => [
                        'controller' => 'ShopOwners',
                        'action' => 'login'
                     ],'authenticate' => [
                          'Form' => [
                            'userModel' => 'ShopOwners', // Added This
                            'fields' => [
                              'username' => 'username',
                              'password' => 'password',
                             ]
                           ]
                     ],'loginRedirect' => [
                         'controller' => 'ShopOwners',
                         'action' => 'view'
                     ],
                ]);

需要记住的一个重要的事情是,密码散列通常很长,因此请确保您的密码列数据库足够大(我使用255以防万一)

One important thing to remember is that password hashes are usually very long, so make sure your password column in the database is big enough (I use 255 just in case)

这篇关于Cakephp 3.0登录返回false每次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆