我在 stackoverflow 上搜索了许多帖子以寻找答案,而且我可能只是忽略了一些东西,但我似乎无法让 $this->Auth->login() 工作。我尝试了其他帖子中的许多不同建议。在描述我尝试过的其他方法时,我会尽量详尽。
我确实添加了一个用户工作。 MD5 哈希工作正常。我散列了一个密码,然后使用奇迹沙拉 md5 进行检查 http://www.miraclesalad.com/webtools/md5.php
我不使用盐进行散列。我使用没有盐的 MD5。
我使用的数据库是 Postgresql 9.0。我知道 CakePhp 的一些魔法不适用于所有数据库(或者我被告知)。
app/Config/core.php
Configure::write('Security.level', 'medium');
/**
* A random string used in security hashing methods.
*/
Configure::write('Security.salt', '');
我正在使用 Auth->fields 将密码映射到 user_password 并将用户名映射到数据库中的 user_name。 user_password 和 user_name 是 core_users 表中的列。我也有 beforeFilter() 方法。
$this->Auth->fields = array('username' => 'user_name', 'password' => 'user_password');
app/Controller/AppController.php
class AppController extends Controller {
public $components = array(
'Session',
'Auth' => array(
'loginRedirect' => array('controller' => 'pages', 'action' => 'index'),
'logoutRedirect' => array('controller' => 'pages', 'action' => 'display', 'home'),
'loginAction' => array('admin' => false, 'controller' => 'CoreUsers', 'action' => 'login'),
/*'fields' => array('password' => 'user_password', 'username' => 'user_name'),*/
'userModel' => 'CoreUser'
)
);
public function beforeFilter() {
Security::setHash('md5');
$this->Auth->allow('login');
//debug($this->Auth);
}
}
我把调试留在了,所以你可以看到它们的处理顺序,我会告诉你它们是如何打印的。
app/Controller/CoreUsersController.php
public function login() {
Security::setHash('md5');
//debug($this->Auth);
if ($this->request->is('post')) {
debug(Security::hash($this->Auth->request->data['CoreUser']['user_password']));
debug($this->Auth);
debug(Configure::version());
debug($this->Auth->request->data['CoreUser']['user_password']);
debug($this->Auth->request->data['CoreUser']['user_name']);
if ($this->Auth->login()) {
debug($this->Auth->request->data['CoreUser']['user_password']);
$this->redirect($this->Auth->redirect());
} else {
debug($this->Auth->request->data['CoreUser']['user_password']);
$this->Session->setFlash(__('Invalid username or password, try again'));
}
}
}
public function logout() {
$this->redirect($this->Auth->logout());
}
app/Model/CoreUser.php
App::uses('AuthComponent', 'Controller/Component');
class CoreUser extends AppModel{
public $primaryKey = 'user_id';
public $sequence = 'core_user_id_seq';
public $name = 'CoreUser';
public $validate = array(
'user_name' => array(
'required' => array(
'rule' => array('notEmpty'),
'message' => 'User name is required'
)
),
'user_password' => array(
'required' => array(
'rule' => array('notEmpty'),
'message' => 'Password is required'
)
),
'privilege_id' => array(
'required' => array(
'rule' => array('notEmpty'),
'message' => 'Privilege ID is required'
),
'legalValues' => array(
'rule' => array('between',1,4),
'message' => 'Privilege must be between 1 and 4'
)
),
'user_initial' => array(
'required' => array(
'rule' => array('notEmpty'),
'message' => 'User initials is required'
)
),
'email' => array(
'rule' => array('email',true),
'message' => 'Email must have an \'@\' symbol and a domain e.g. .com'
)
);
public function beforeSave() {
Security::setHash('md5');
if (isset($this->data[$this->alias]['user_password'])) {
$this->data[$this->alias]['user_password'] = AuthComponent::password($this->data[$this->alias]['user_password']);
}
return true;
}
}
应用程序/ View /CoreUsers/login.ctp
<h3>Login</h3>
<div class="users form">
<?php echo $this->Session->flash('auth'); ?>
<?php echo $this->Form->create('CoreUser');?>
<fieldset>
<legend><?php echo __('Please enter your username and password'); ?></legend>
<?php
echo $this->Form->input('user_name');
echo $this->Form->input('user_password');
?>
</fieldset>
<?php echo $this->Form->end(__('Login'));?>
</div>
调试输出
所有这些都来自 CoreUsersController 并按照它们的处理顺序进行。
散列密码。这与我添加用户时数据库中的内容相同。
'098f6bcd4621d373cade4e832627b4f6'
身份验证对象
object(AuthComponent) {
components => array(
(int) 0 => 'Session',
(int) 1 => 'RequestHandler'
)
authenticate => array(
(int) 0 => 'Form'
)
authorize => false
ajaxLogin => null
flash => array(
'element' => 'default',
'key' => 'auth',
'params' => array()
)
loginAction => array(
'admin' => false,
'controller' => 'CoreUsers',
'action' => 'login'
)
loginRedirect => array(
'controller' => 'pages',
'action' => 'index'
)
logoutRedirect => array(
'controller' => 'pages',
'action' => 'display',
(int) 0 => 'home'
)
authError => 'You are not authorized to access that location.'
allowedActions => array(
(int) 0 => 'login'
)
request => object(CakeRequest) {
params => array(
'plugin' => null,
'controller' => 'CoreUsers',
'action' => 'login',
'named' => array(),
'pass' => array()
)
data => array(
'CoreUser' => array(
'user_name' => 'testy5',
'user_password' => 'test'
)
)
query => array()
url => 'CoreUsers/login'
base => '/cpm_v2_dev'
webroot => '/cpm_v2_dev/'
here => '/cpm_v2_dev/CoreUsers/login'
}
response => object(CakeResponse) {
}
settings => array(
'loginRedirect' => array(
'controller' => 'pages',
'action' => 'index'
),
'logoutRedirect' => array(
'controller' => 'pages',
'action' => 'display',
(int) 0 => 'home'
),
'loginAction' => array(
'admin' => false,
'controller' => 'CoreUsers',
'action' => 'login'
),
'userModel' => 'CoreUser'
)
userModel => 'CoreUser'
}
CakePHP 的版本
'2.1.0'
调用 login() 之前的密码
'test'
调用 login() 之前的用户名
'testy5'
调用 login() 后的密码
'test'
这是我在我尝试过的其他 stackoverflow 帖子中阅读的内容的快速列表。如果您需要我详细说明,请告诉我。
1)我将用户名和密码映射到数据库中的字段。这是字段的注释所在。我也尝试在 beforeFilter() 方法中这样做。使用:
$this->Auth->fields = array('username' => 'user_name', 'password' => 'user_password');
在登录 View 中,表单是这样创建的:
$this->Form->input('username');
$this->Form->input('password');
2)我尝试在登录前手动散列密码,如下所示:
$this->Auth->request->data['CoreUser']['password'] = Security::hash($this->Auth->request->data['CoreUser']['password'])
if ($this->Auth->login()) {
$this->redirect($this->Auth->redirect());
}
编辑0
3) 我只是按照 CakePHP 2.0 Auth Login not working 的建议尝试这样做
我的 AuthComponent 现在看起来像这样:
public $components = array(
'Session',
'Auth' => array(
'authenticate' => array(
'Form' => array(
'userModel' => 'CoreUser',
'fields' => array(
'username' => 'user_name',
'password' => 'user_password'
)
)
),
'loginRedirect' => array('controller' => 'pages', 'action' => 'index'),
'logoutRedirect' => array('controller' => 'pages', 'action' => 'display', 'home'),
'loginAction' => array('admin' => false, 'controller' => 'CoreUsers', 'action' => 'login')
)
);
如果我没有详细说明,或者我犯了错误,我深表歉意。我已经为此工作了几天,它真的让我筋疲力尽。我感谢我可能得到的任何帮助。谢谢!
最佳答案
我最终为解决这个问题所做的是完全按照 CakePHP 的教程进行操作。我也升级到 2.1.2。我正在运行 2.1.0。
http://book.cakephp.org/2.0/en/tutorials-and-examples/blog-auth-example/auth.html
然后我慢慢添加了我需要的配置。有关我引用的 Auth 组件的信息:
http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html
真的我的问题是故障排除。我会做一系列的改变,而不是一次一个。我有我的 login.ctp 查看两种不同的方式
$this->Form->input('username');
$this->Form->input('password');
现在它看起来像这样:
echo $this->Form->input('user_name');
echo $this->Form->Label('Password');
echo $this->Form->password('user_password');
第二个版本有效。
编辑0:
这是非常重要的。如果不调用 AppController 父级,登录将无法工作。
class CoreUsersController extends AppController{
public $helpers = array('Html','Form');
public function beforeFilter() {
parent::beforeFilter();
}
Auth 组件的修订版有效:
public $components = array(
'Session',
'Auth' => array(
'authenticate' => array(
'Form' => array(
'userModel' => 'CoreUser',
'fields' => array(
'username' => 'user_name',
'password' => 'user_password'
)
)
),
'loginRedirect' => array('controller' => 'pages', 'action' => 'index'),
'logoutRedirect' => array('controller' => 'pages', 'action' => 'display', 'home'),
'loginAction' => array('admin' => false, 'controller' => 'CoreUsers', 'action' => 'login')
)
);
我的盐仍然是一个空字符串:
Configure::write('Security.salt', '');
只需要在一处设置 md5 哈希值。在模型中的 beforeSave() 中不需要它:
public function beforeFilter() {
Security::setHash('md5');
$this->Auth->allow('login','add');
}
之前保存():
public function beforeSave() {
if (isset($this->data[$this->alias]['user_password'])) {
$this->data[$this->alias]['user_password'] = AuthComponent::password($this->data[$this->alias]['user_password']);
}
return true;
}
关于CakePHP 2.1 Auth->login() 不起作用,但添加用户可以,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10234757/