我已经在 stackoverflow 上搜索了许多帖子以寻找答案,并且可能我只是忽略了一些东西,但我似乎无法让 $this->Auth->login() 工作。我从其他帖子中尝试了许多不同的建议。在描述我尝试过的其他方法时,我会尽量做到彻底。
我确实添加了一个正在工作的用户。MD5 散列工作正常。我对密码进行了哈希处理,然后使用奇迹沙拉 md5 http://www.miraclesalad.com/webtools/md5.php检查了它
我不使用盐进行散列。我使用不加盐的 MD5。
我使用的数据库是 Postgresql 9.0。我知道一些 CakePhp 魔法并不适用于所有数据库(或者有人告诉我)。
应用程序/配置/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');
应用程序/控制器/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);
}
}
我留下了调试,这样你就可以看到它们的处理顺序,我将向你展示它们是如何打印的。
应用程序/控制器/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());
}
应用程序/模型/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;
}
}
应用程序/视图/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');
在登录视图中,表单是这样创建的:
$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的建议尝试这样做,但不工作
我的 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')
)
);
如果我没有详细说明,或者我犯了错误,我深表歉意。我已经为此工作了几天,这真的让我筋疲力尽。我感谢我可能收到的任何帮助。谢谢!