【发布时间】:2016-04-17 20:07:40
【问题描述】:
我有以下代码,我试图在用户登录时重新编码密码(数据库已从旧网站迁移)。但是,我不确定自己做错了什么,因为我不断收到错误:
尝试调用“AppBundle\Service\HubAuthenticator”类的名为“forward”的未定义方法。
我的设置如下:
security.yml
security:
encoders:
AppBundle\Entity\Member:
id: club.hub_authenticator
services.yml
services:
//This should be central service than then calls the second
club.hub_authenticator:
class: AppBundle\Service\HubAuthenticator
club.password_rehash:
class: AppBundle\Service\PasswordRehash
Hubauthenticator.php
namespace AppBundle\Service;
use Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface;
class HubAuthenticator extends \Symfony\Component\Security\Core\Encoder\BCryptPasswordEncoder implements PasswordEncoderInterface
{
function __construct($cost=13)
{
parent::__construct($cost);
}
function isPasswordValid($encoded, $raw, $salt)
{
// Test for legacy authentication (and conditionally rehash the password stored in the database if true)
if ($this->comparePasswords($encoded, sha1("saltA".$raw."saltB"))) {
$this->forward('club.password_rehash:rehash');
}
// Test for Symfony's Bcrypt authentication (any passwords just rehashed in previous step should work here)
if (parent::isPasswordValid($cost=13, $encoded,$raw,$salt)) return true ;
}
}
PasswordRehash.php
namespace AppBundle\Service;
use Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface;
class PasswordRehash extends \Symfony\Component\Security\Core\Encoder\BCryptPasswordEncoder
{
// Customises BCryptPasswordEncoder class to use legacy SHA method
function rehash($member, $raw, $salt)
{
//Salt is null as Symfony documentation says it is better to generate a new one
parent::encodePassword($member->getPlainPassword, $salt=null ) ;
}
}
之前的一些其他完整性尝试:
我的猜测是问题在于我误解了我可以使用哪些对象。我的理解是用户此时尚未通过身份验证,因此尝试并删除了以下尝试:
尝试将$member 注入HubAuthenticator 服务:
function __construct($cost=13)
{
parent::__construct($cost, \Member $member);
}
当试图让普通密码重新散列时:
$this->get('security.context')->getToken()->getUser()->getPlainPassword();
【问题讨论】:
-
在调用
forward()方法(不存在)时,您打算在HubAuthenticator 类中实现什么?为什么这个类完全扩展了BcryptPasswordEncoder? -
嗨@xabbuh -
forward()错了 - 我只是在尝试不同的尝试来让它工作(!)。BcryptPasswordEncoder我尝试按照您的建议删除,但我收到“错误:无法访问父级::”....因为我有if (parent::isPasswordValid(...)) return true在其中
标签: fosuserbundle symfony