Cakephp 3.0登录每次都会返回false [英] Cakephp 3.0 Login returns false every time

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

问题描述

我正在尝试在我的Cakephp 3.0网站上登录用户。我的表格有一个用户名和密码字段,原样命名。我可以添加一个用户,并且密码已成功散列并存储在表中。尽管如此,我什至无法登录一次。

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']);

我在Controller中的登录视图功能

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. 受保护的$ _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 ['password']重置为散列版。这不起作用,因为

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/zh-CN/controllers/components/authentication.html#configuring-authentication -处理程序

基本上,您需要将其添加到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天全站免登陆