登录 [ Auth->identify() ] 在 CakePHP 3 上始终为 false
Login [ Auth->identify() ] always false on CakePHP 3
我在使用 CakePHP 2 一段时间后开始使用 CakePHP 3,但我在创建身份验证登录时遇到了问题。
新的授权函数$this->Auth->identify()
总是return false。
数据库上,密码加密完美,查询谁带用户也可以。
我的代码:
应用程序控制器:
[...]
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']);
}
}
用户控制器:
[...]
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'));
}
}
[...]
用户(模型实体):
<?php
namespace App\Model\Entity;
use Cake\Auth\DefaultPasswordHasher;
use Cake\ORM\Entity;
class User extends Entity{
protected $_accessible = [*];
protected function _setPassword($password){
return (new DefaultPasswordHasher)->hash($password);
}
}
查看:
<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 默认使用与 2 不同的散列算法(bcrypt 与 SHA1),因此您需要使密码长度更长。将您的密码字段更改为 VARCHAR(255) 以确保安全。
当 CakePHP 3 尝试通过 this->Auth->identify() 与数据库中的散列密码来识别您的内存散列密码时,它永远不会匹配,因为某些字符丢失。更改为 255 超出了需要,但如果将来使用更安全的散列,则可以帮助将来证明。建议使用 255,因为字符数可以存储在一个字节中。
已解决:数据库中的类型少于要求。更改为 varchar(255),现在工作正常:)
我遇到了同样的问题。登录 [ Auth->identify() ] 对我不起作用。在数据库中更改密码长度将解决问题。
您好,将我的代码片段分享到 Login Auth,所有测试都正常,在 CakePHP 3.1 中,海关 (Table + 查看登录 BootStrap 3 + SQL 基础 + 自定义 bootstrap.php Inflector::rules(*****))
中的西班牙语
中的所有代码
我通过添加 use Cake\Auth\DefaultPasswordHasher;
及其后续覆盖方法 _setPassword
.
得出了一个解决方案
更改如下:
Model/table.php
<?php
use Cake\Auth\DefaultPasswordHasher;
// Somewhere in the class
protected function _setPassword($password) {
return (new DefaultPasswordHasher)->hash($password);
}
我在使用 CakePHP 2 一段时间后开始使用 CakePHP 3,但我在创建身份验证登录时遇到了问题。
新的授权函数$this->Auth->identify()
总是return false。
数据库上,密码加密完美,查询谁带用户也可以。
我的代码:
应用程序控制器:
[...]
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']);
}
}
用户控制器:
[...]
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'));
}
}
[...]
用户(模型实体):
<?php
namespace App\Model\Entity;
use Cake\Auth\DefaultPasswordHasher;
use Cake\ORM\Entity;
class User extends Entity{
protected $_accessible = [*];
protected function _setPassword($password){
return (new DefaultPasswordHasher)->hash($password);
}
}
查看:
<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 默认使用与 2 不同的散列算法(bcrypt 与 SHA1),因此您需要使密码长度更长。将您的密码字段更改为 VARCHAR(255) 以确保安全。
当 CakePHP 3 尝试通过 this->Auth->identify() 与数据库中的散列密码来识别您的内存散列密码时,它永远不会匹配,因为某些字符丢失。更改为 255 超出了需要,但如果将来使用更安全的散列,则可以帮助将来证明。建议使用 255,因为字符数可以存储在一个字节中。
已解决:数据库中的类型少于要求。更改为 varchar(255),现在工作正常:)
我遇到了同样的问题。登录 [ Auth->identify() ] 对我不起作用。在数据库中更改密码长度将解决问题。
您好,将我的代码片段分享到 Login Auth,所有测试都正常,在 CakePHP 3.1 中,海关 (Table + 查看登录 BootStrap 3 + SQL 基础 + 自定义 bootstrap.php Inflector::rules(*****))
中的西班牙语 中的所有代码我通过添加 use Cake\Auth\DefaultPasswordHasher;
及其后续覆盖方法 _setPassword
.
更改如下:
Model/table.php
<?php
use Cake\Auth\DefaultPasswordHasher;
// Somewhere in the class
protected function _setPassword($password) {
return (new DefaultPasswordHasher)->hash($password);
}