嗨,我正在使用cakePHP版本2.3.6,我一直试图创建一个注册和登录,但是当我用一个注册的用户名和密码登录时,它总是说错误的用户名或密码,我也使用电子邮件的用户名。请帮帮我谢谢。
AppController
class AppController extends Controller {
public $components = array(
'Session',
'Auth' => array(
'loginRedirect' => array('controller' => 'users', 'action' => 'index'),
'logoutRedirect' => array('controller' => 'pages''action' =>'display', 'home')
)
);
public function beforeFilter() {
$this->Auth->allow('index', 'view');
}
}
View Login
<div class="users form">
<?php echo $this->Session->flash('auth'); ?>
<?php echo $this->Form->create('User'); ?>
<fieldset>
<legend><?php echo __('Please enter your username and password'); ?></legend>
<?php echo $this->Form->input('email');
echo $this->Form->input('password');
?>
</fieldset>
<?php echo $this->Form->end(__('Login')); ?>
</div>
UserController
<?php
class usersController extends AppController
{
public function beforeFilter() {
parent::beforeFilter();
$this->Auth->allow('add','login','logout');
}
var $name = 'Users';
public function view($id = null) {
$this->User->id = $id;
if (!$this->User->exists()) {
throw new NotFoundException(__('Invalid user'));
}
$this->set('user', $this->User->read(null, $id));
}
public function add()
{
if (!empty($this ->data))
{
$this->User->create();
if ($this->User->save($this->data))
{
$this->Session->setFlash('Thank you for registering');
$this->redirect(array('action'=>'index'));
}
}
}
function index()
{
}
public function login() {
if ($this->request->is('post')) {
if ($this->Auth->login())
{
$this->redirect($this->Auth->redirectUrl());
}
else
{
$this->Session->setFlash(__('Username or password is incorrect'), 'default', array(), 'auth');
}
}
}
public function logout() {
return $this->redirect($this->Auth->logout());
}
}
**UserModel**App::uses('AuthComponent','Controller/Component');
class User extends AppModel
{
var $name = 'User';
public $validate = array(
'email' => array(
'valid' => array(
'rule' => 'email',
'message' => 'Please enter an email address for username',
),
'unique' => array(
'rule' => 'isUnique',
'message' => 'This username has already been taken',
),
'eValid' => array(
'required' => true,
'rule' => array('notEmpty'),
'message' => 'Please enter a username'
)
),
'password' => array(
'pValid' => array(
'required' => true,
'rule' => array('notEmpty'),
'message' => 'Please enter a valid password'
),
'minPword' => array(
'rule' => array('minLength', 8),
'message' => 'Please enter a password that is minimum 8 characters'
)
),
);
public function beforeSave($options = array()) {
if (isset($this->data[$this->alias]['password'])) {
$this->data[$this->alias]['password'] = AuthComponent::password($this- >data[$this->alias]['password']);
}
return true;
}
}发布于 2013-09-17 11:25:40
CakePHP的Auth组件默认使用“用户名”和“密码”字段。首先,您必须告诉Auth组件使用"email“字段而不是"username”字段。
'Auth' => array
(
'authenticate' => array
(
'Form' => array
(
'fields' => array('username' => 'email', 'password' => 'password')
)
),
'loginRedirect' => array('controller' => 'users', 'action' => 'index'),
'logoutRedirect' => array('controller' => 'pages', 'action' =>'display', 'home')
)我建议您激活isAuthorized检查,以便完全控制授权策略(http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#authorization-objects)。请记住,您甚至可以设置一个自定义密码散列方法来提高安全性。
编码愉快!
编辑
源中存在多个问题:首先,转储中的密码字段太短,不能使用密码哈希.在食谱教程中,您将看到密码字段是VARCHAR(50),而不是VARCHAR(30).。
其次,CakePHP是一个“约定高于配置”的框架,因此总是记住遵循食谱说明。控制器的名称必须是CamelCased,所以usersController必须是UsersController等等:这将帮助您避免使用
$name = 'Users';语句。干控制器&更少的指令意味着更少的头痛;)并且尊重文件夹的名称的约定!
第三,在SQL转储中,您的电子邮件有很多多条记录。这意味着,即使解决了字段的问题,您也可以在登录策略中找到更多的问题。幸运的是,您在使用模型验证进行了一些测试之后,对此进行了修补。要解决这个问题,只需清空您的用户表并尝试一下。
第四,安全性改进:在我通过电子邮件发送给您的源代码中,我添加了以下内容
Security::setHash('sha256');在登录()和add()函数的顶部。这将为您的密码提供更强的散列,并结合应用程序的salt。始终在必需的函数中声明这一点(这意味着保存或编辑用户的表密码字段)。请记住,这需要对用户表的密码字段进行编辑:将其更改为VARCHAR( 64 )或更高(实际上,SHA256返回64个字符字符串)。
仅此而已,我真的希望你会喜欢CakePHP。
再一次..。编码愉快!
https://stackoverflow.com/questions/18816111
复制相似问题