gpt4 book ai didi

fosuserbundle - 如何访问注入(inject)服务中的用户 token 以重新编码密码?

转载 作者:行者123 更新时间:2023-12-02 01:26:50 27 4
gpt4 key购买 nike

我有以下代码,我在其中尝试在用户登录时重新编码密码(数据库已从遗留网站迁移过来)。但是,我不确定自己做错了什么,因为我不断收到错误:

Attempted to call an undefined method named "forward" of class "AppBundle\Service\HubAuthenticator".

我的设置如下:

安全.yml

security:
encoders:
AppBundle\Entity\Member:
id: club.hub_authenticator

服务.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 ;
}
}

密码重新哈希.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 注入(inject)到 HubAuthenticator 服务中:

function __construct($cost=13)
{
parent::__construct($cost, \Member $member);
}

当试图让明文密码重新散列时:

$this->get('security.context')->getToken()->getUser()->getPlainPassword();

最佳答案

在您的服务中,您只能访问您注入(inject)的依赖项。

因此,要访问当前用户对象,您需要将其作为参数传递:

服务:

club.password_rehash:
class: AppBundle\Service\PasswordRehash
arguments: [ "@security.token_storage" ]

构造函数:

use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;

class HubAuthenticator extends \Symfony\Component\Security\Core\Encoder\BCryptPasswordEncoder implements PasswordEncoderInterface
{
private $storage;

function __construct($cost = 13, TokenStorageInterface $storage)
{
parent::__construct($cost);

$this->storage = $storage;
// Now you can use:
// $user = $this->storage->getToken()->getUser();
}
}

然后,以同样的方式访问第二个服务,注入(inject)它。

将其添加到服务参数中:

club.password_rehash:
class: AppBundle\Service\PasswordRehash
arguments: [ "@security.token_storage", "@club.password_rehash" ]

将其添加到您的构造函数中:

private $storage;
private $passwordRehash

function __construct($cost = 13, TokenStorageInterface $storage, PasswordRehash $passwordRehash)
{
parent::__construct($cost);

$this->storage = $storage;
$this->passwordRehash = $passwordRehash;
// Now you can use:
// $this->passwordRehash->rehash(...);
}

希望对你有帮助。

关于fosuserbundle - 如何访问注入(inject)服务中的用户 token 以重新编码密码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36681622/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com