在 CakePHP 3 上登录 [ Auth->identify() ] 始终为 false

Login [ Auth-gt;identify() ] always false on CakePHP 3(在 CakePHP 3 上登录 [ Auth-identify() ] 始终为 false)
本文介绍了在 CakePHP 3 上登录 [ Auth->identify() ] 始终为 false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

在使用 CakePHP 2 一段时间后,我开始使用 CakePHP 3,但在创建身份验证登录时遇到了麻烦.

新的身份验证函数 $this->Auth->identify() 总是返回 false.

在数据库上,密码加密完美,查询谁带用户也可以.

我的代码:

应用控制器:

<代码>[...]类 AppController 扩展控制器{公共函数初始化(){$this->loadComponent('Flash');$this->loadComponent('Auth', ['登录重定向' =>['控制器' =>'行政','动作' =>'指数'],'注销重定向' =>['控制器' =>'页面','动作' =>'展示']]);}公共函数 beforeFilter(Event $event){$this->Auth->allow(['display']);}}

用户控制器:

<代码>[...]类 UsersController 扩展了 AppController{公共函数 beforeFilter(Event $event){parent::beforeFilter($event);$this->Auth->allow(['logout']);}[...]公共函数登录(){如果 ($this->request->is('post')) {$user = $this->Auth->identify();如果($用户){$this->Auth->setUser($user);返回 $this->redirect($this->Auth->redirectUrl());}$this->Flash->error(__('用户名或密码无效,再试一次'));}}[...]

用户(模型实体):

hash($password);}}

查看:

<?= $this->Flash->render('auth') ?><?= $this->Form->create() ?><字段集><legend><?= __('请输入您的用户名和密码') ?></legend><?= $this->Form->input('username') ?><?= $this->Form->input('password') ?></fieldset><?= $this->Form->button(__('Login'));?><?= $this->Form->end() ?>

解决方案

CakePHP3 默认使用与 2 不同的哈希算法(bcrypt vs. SHA1),因此您需要使密码长度更长.将您的密码字段更改为 VARCHAR(255) 以确保安全.

当 CakePHP 3 尝试从 this->Auth->identify() 与数据库中的散列密码识别内存中的散列密码时,它永远不会匹配,因为缺少某些字符.更改为 255 是不必要的,但如果将来使用更安全的散列,则可以帮助将来证明.建议使用 255,因为字符数可以存储在一个字节中.

I started using CakePHP 3 after a time using CakePHP 2 and I am having troubles to create the authentication login.

The new auth function $this->Auth->identify() always return false.

On the database, the password are encrypted perfect and the query who takes the user it's ok too.

My code:

AppController:

[...]
class AppController extends Controller{
    public function initialize(){
        $this->loadComponent('Flash');
        $this->loadComponent('Auth', [
            'loginRedirect' => [
                'controller' => 'Admin',
                'action' => 'index'
            ],
            'logoutRedirect' => [
                'controller' => 'Pages',
                'action' => 'display'
            ]
        ]);
    }

    public function beforeFilter(Event $event)
    {
        $this->Auth->allow(['display']);
    }
}

UserController:

[...]
class UsersController extends AppController{
    public function beforeFilter(Event $event)
    {
    parent::beforeFilter($event);
    $this->Auth->allow(['logout']);
    }
[...]
    public function login()
    {
        if ($this->request->is('post')) {
            $user = $this->Auth->identify();
            if ($user) {
                $this->Auth->setUser($user);
                return $this->redirect($this->Auth->redirectUrl());
            }
            $this->Flash->error(__('Invalid username or password, try again'));
        }
    }
[...]

User (Model Entity):

<?php
namespace AppModelEntity;

use CakeAuthDefaultPasswordHasher;
use CakeORMEntity;

class User extends Entity{
    protected $_accessible = [*];
    protected function _setPassword($password){
        return (new DefaultPasswordHasher)->hash($password);
    }
}

View:

<div class="users form">
<?= $this->Flash->render('auth') ?>
<?= $this->Form->create() ?>
    <fieldset>
        <legend><?= __('Please enter your username and password') ?></legend>
        <?= $this->Form->input('username') ?>
        <?= $this->Form->input('password') ?>
    </fieldset>
<?= $this->Form->button(__('Login')); ?>
<?= $this->Form->end() ?>
</div>

解决方案

CakePHP3 uses a different hashing algorithm by default than 2 (bcrypt vs. SHA1), so you need to make your password length longer. Change your password field to VARCHAR(255) to be safe.

When CakePHP 3 tries to identify your in-memory hashed password from this->Auth->identify() vs. the hashed password in the database, it will never match because some characters are missing. Changing to 255 is more than needed, but can help future proof if an even more secure hash is used in the future. 255 is recommended because the the character count can be stored in one byte.

这篇关于在 CakePHP 3 上登录 [ Auth->identify() ] 始终为 false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)