繁体   English   中英

在CakePHP 2.4中使用AuthComponent登录

[英]Login using AuthComponent in CakePHP 2.4

我认为CakePHP没有详细讨论2.4 AuthComponent。 由于2.4具有用于散列(bcrypt)的新算法,因此CookBook应该明确说明,否则我不明白这一点。 我发现很多人都有这个问题。 如果我看到SQL转储,则AuthComponent不会检查密码。 这是我的代码:

//UsersController
public $components = array(
    'Session',
    'Auth' => array(
    'loginRedirect' => array('controller' => 'users', 'action' => 'home'),
    'logoutRedirect' => array('controller' => 'users', 'action' => 'logout')
    )
);
public function login(){
    if ($this->request->is('post')) {
        if ($this->Auth->login()) {
            return $this->redirect($this->Auth->redirectUrl());
        } else {
            $this->Session->setFlash(__('Username or password is incorrect'), 'default', array());
        }
    }
}

//UserModel
class UserModel extends AppModel{
}
//Login.ctp
echo $this->Form->create('User');
echo $this->Form->input('username');
echo $this->Form->input('password');
echo $this->Form->end('Login');

SQL转储:

SELECT User.id, User.username, User.password, User.pass_hint, User.joined FROM 
oes.users AS User WHERE User.username = 'guest' LIMIT 1

我发现无论我的登录凭据是否正确, $this->Auth->login()不会返回true 但是我想使用AuthComponent成功登录。

提前致谢...

AuthComponent do not check for passwords if I see SQL dumps

错误: AuthComponent会检查密码。 它正在拉User.password ,在那里他们检查密码。

AuthComponent使用BasicAuthenticate来检查密码。 它运行getUser来获取用户,并使用BaseAuthenticateBaseAuthenticate::_findUser()检查密码。 不难遵循,那就是魔术发生的地方。

这里的规则是,您将需要在数据库中预先存储密码,这样它就可以立即使用。

要获取哈希令牌,请使用

echo $this->Auth->password('the-chosen-password'); //DEPRECATED since 2.4.0

或使用Security实用程序:

echo Security::hash('the-chosen-password', null, true);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM