Auth->identify 总是让有效的用户名登录而不检查密码。我只需要一个有效的用户名,我就可以轻松打开仪表板。当我将 login.ctp 中的输入密码名称从 password 更改为 password2 时,我无法再登录了。这是我昨天遇到的一个奇怪问题,但没有弄清楚如何解决。
这是app.php
$this->loadComponent('Auth',['loginRedirect'=>['controller'=>'Dashboard','action'=>'index'],
'logoutRedirect'=>['controller'=>'Users',"action"=>"login"],
'authorize' => array('Controller')
]);
这是我的登录 View :
<form class="login-form" method="post">
<input type="text" placeholder="<?php echo __("Username");?>" name="username" />
<input type="password" placeholder="<?php echo __("Password");?>" name="password"/>
<button id="btn_login"><?php echo __("Login");?></button>
</form>
我在 UsersController 中的登录功能:
public function login()
{
if ($this->request->is('post')) {
$user = $this->Auth->identify();
if ($user) {
$this->Auth->setUser($user);
return $this->redirect($this->Auth->redirectUrl());
} else {
$this->Flash->error(__('Username or password is incorrect'));
}
}
}
最后可能会出现困惑,老开发人员为用户创建了一个名为 app_members 的表。数据库中没有名为 users 的表,所以他写了以下内容:
在模型/实体中,user.php 和 appMember 都包含这个:
<?php
namespace App\Model\Entity;
use Cake\Auth\DefaultPasswordHasher;
use Cake\ORM\Entity;
class User (or AppMember) extends Entity
{
protected $_accessible = ['*'=> true,'id' => false];
protected function _setPassword($password)
{
return (new DefaultPasswordHasher)->hash($password);
}
}
?>
对于 model/table/UsersTable.php,他从 appMember 扩展:
<?php
namespace App\Model\Table;
use Cake\ORM\Table;
use Cake\Validation\Validator;
class UsersTable extends Table
{
public function initialize(array $config)
{
$this->table('app_member');
}
public function validationDefault(Validator $validator)
{
return $validator
->notEmpty('username', 'A username is required')
->notEmpty('password', 'A password is required')
->notEmpty('role', 'A role is required');
}
}
编辑 1: app.php isAuthorized 函数:
public function isAuthorized($users)
{
if(isset($users))
{
return true;
}
else{
return false;
}
}
最佳答案
在花了很多时间寻找问题之后。我发现有人修改了 CakePHP src/Auth/BaseAuthenticate.php 并评论:
/*if (!$hasher->check($password, $hashedPassword)) {
return false;
}*/
PokerFace
关于php - CakePHP 3 : Auth->identify() always let a valid username in without checking the password,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47925389/