0
我想爲我的cakephp項目創建custompasswordhasher。 cakephp版本是2.5。我遵循CakePHP的烹飪書,並創建目錄控制器的以下自定義類/認證/ CustomPasswordHasher.phpcakephp創建custompasswordhash內部服務器錯誤
App::uses('AbstractPasswordHasher', 'Controller/Component/Auth');
class CustomPasswordHasher extends AbstractPasswordHasher {
public function hash($password) {
$hasher = md5(Configure::read('Security.salt') . $password . Configure::read('Security.cipherSeed'));
return $hasher;
}
public function check($password, $hashedPassword) {
//debug('PHPassHasher'); die('Using custom hasher'); //<--THIS NEVER HAPPENS!
$password = md5(Configure::read('Security.salt') . $password . Configure::read('Security.cipherSeed'));
echo $password."==".$hashedPassword;exit;
return password_verify($password, $hashedPassword);
}
}
,這裏是在控制我的登錄功能
public function admin_login() {
if ($this->Auth->loggedIn()) {
return $this->redirect($this->Auth->redirect());
}
if ($this->request->is('post')) {
if ($this->Auth->login()) {
return $this->redirect($this->Auth->redirect());
} else {
$this->Session->setFlash('Username or password is incorrect', 'error');
}
}
}
和appController.php我配置功能
public function beforeFilter() {
if ($this->request->prefix == 'admin') {
$this->layout = 'admin';
AuthComponent::$sessionKey = 'Auth.User';
$this->Auth->loginAction = array('controller' => 'administrators', 'action' => 'login');
$this->Auth->loginRedirect = array('controller' => 'administrators', 'action' => 'dashboard');
$this->Auth->logoutRedirect = array('controller' => 'administrators', 'action' => 'login');
$this->Auth->authenticate = array(
'all' => array(
'scope' => array(
'User.is_active' => 1
)
),
'Form' => array(
'userModel' => 'User',
'passwordHasher' => array(
'className' => 'Auth/CustomPasswordHasher'
)
)
);
$this->Auth->allow('login');
} else {
/* do another stuff for user authentication */
}
}
這裏是我的登錄表單。
<div class="login-box">
<div class="login-logo">Admin Login</div>
<div class="login-box-body">
<p class="login-box-msg">Sign in to start your session</p>
<?php echo $this->Session->flash(); ?>
<?php echo $this->Form->create(); ?>
<div class="form-group has-feedback">
<?php
echo $this->Form->input('User.username',
array(
'label' => false,
'class' => 'form-control',
'placeholder' => 'Username',
'autocomplete' => 'off',
'autofocus' => true,
'value' => @$username
)
);
?>
<span class="glyphicon glyphicon-envelope form-control-feedback"></span>
</div>
<div class="form-group has-feedback">
<?php
echo $this->Form->input('User.password',
array(
'type' => 'password',
'label' => false,
'class' => 'form-control',
'placeholder' => 'Password',
'value' => @$password
)
);
?>
<span class="glyphicon glyphicon-lock form-control-feedback"></span>
</div>
<div class="row">
<div class="col-xs-8">
<div class="checkbox icheck ">
<label>
<input type="checkbox" name="data[Admin][remember_me]"> Remember Me </label>
</div>
</div>
<div class="col-xs-4">
<button type="submit" class="btn btn-primary btn-block btn-flat">Sign In</button>
</div>
</div>
<?php echo $this->Form->end(); ?>
<a href="#forgotpassword" data-toggle="modal" data-target="#forgot_password_modal1">I forgot my password</a><br>
</div>
</div>
每次提交表格時如何。我得到的堆棧跟蹤
所以每個人都可以幫我這個?
那麼,passwordHasher中第138行的實際錯誤信息是什麼? – Challe
我現在看到了原因。因爲我在我的控制器中調用了錯誤的className。它應該是「自定義」而不是「CustompasswordHasher」。 Cake會自動添加。 –