【发布时间】:2014-01-02 23:43:41
【问题描述】:
我的问题是用户表的型号名称不同:Breeder。我的登录页面总是显示不正确的用户名或密码,因为输入的密码没有经过哈希处理。
这里是视图:
<?php
echo $this->Form->create();
echo $this->Form->input('username');
echo $this->Form->input('password');
echo $this->Form->end('Log in');
echo $this->Html->link('Register', '/breeders/register');
?>
这是 AppController:
类 AppController 扩展控制器 {
public $helpers = array('Html', 'Form', 'Session');
public $components = array('Session', 'Paginator',
'Auth' => array(
'loginAction' => array('controller' => 'breeders', 'action' => 'login'),
'loginRedirect' => array('controller' => 'breeders', 'action' => 'desk'),
'logoutRedirect' => array('controller' => 'breeders', 'action' => 'login'),
'authorize' => array('Controller'),
'authenticate' => array(
'Form' => array(
'fields' => array(
'username' => 'login',
'password' => 'password'),
'passwordHasher' => array(
'className' => 'Simple',
'hashType' => 'sha256'
)
)
)
)
);
public function isAuthorized($user)
{
return true;
}
public function beforeFilter() {
$this->Auth->allow('login', 'logout', 'register', 'profile');
}
}
我的登录方式:
public function login() {
$this->set('title_for_layout', __('Connection'));
if ($this->Session->read('Auth.Breeder')) {
return $this->redirect('/');
}
if ($this->request->is('post')) {
if ($this->Auth->login()) {
return $this->redirect($this->Auth->redirectUrl());
}
}
}
以及模型中的 beforeSave 方法:
public function beforeSave($options = array()) {
if (!$this->id) {
$passwordHasher = new SimplePasswordHasher();
$this->data['Breeder']['password'] = $passwordHasher->hash(
$this->data['Breeder']['password']
);
}
return true;
}
我不知道我需要添加什么来使密码被散列。欢迎任何帮助。
【问题讨论】:
标签: php cakephp authentication