php - 使用新的 Symfony 5 Authenticator 注册后如何手动验证用户?

标签 php symfony symfony5

Symfony 5 已将其保护身份验证方法更改为新的基于 Passport 的方法,使用新的安全配置:enable_authenticator_manager: true ;
我想知道在 ORM (Doctrine) 持久化用户后,如何在我的 Controller 中的注册表单方法中对用户进行身份验证;
我已成功使用登录表单对用户进行身份验证,但我仍然不知道如何手动执行此操作。

最佳答案

根据 Cerad的评论,这里是完整的答案。
以下只是与问答相关的代码部分。这些不是完整的文件。
此外,这仅适用于 Symfony ^5.2,即 不是 使用guard来验证用户。

/* config/packages/security.yaml */

security:
    enable_authenticator_manager: true
    firewalls:
        main:
            custom_authenticators:
                - App\Security\SecurityAuthenticator
/* src/Security/SecurityAuthenticator.php */

use Symfony\Component\Security\Http\Authenticator\AbstractLoginFormAuthenticator;

/* automatically generated with the make:auth command,
     the important part is to undestand that this is not a Guard implement 
     for the Authenticator class */
class SecurityAuthenticator extends AbstractLoginFormAuthenticator
{
  
}
/* src/Controller/RegistrationController.php */

use App\Entity\User;
use App\Form\RegistrationFormType;
use App\Security\SecurityAuthenticator;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use Symfony\Component\Security\Http\Authentication\UserAuthenticatorInterface;

class RegistrationController extends AbstractController
{

    /**
     * @Route("/register", name="app_register")
     */
    public function register(
        Request $request, 
        UserPasswordEncoderInterface $passwordEncoder, 
        UserAuthenticatorInterface $authenticator, 
        SecurityAuthenticator $formAuthenticator): Response
    {
      /* Automatically generated by make:registration-form, but some changes are
         needed, like the auto-wiring of the UserAuthenticatorInterface and 
         SecurityAuthenticator */
        $user = new User();
        $form = $this->createForm(RegistrationFormType::class, $user);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            // encode the plain password
            $user->setPassword($passwordEncoder->encodePassword($user, $form->get('password')->getData()));

            $entityManager = $this->getDoctrine()->getManager();
            $entityManager->persist($user);
            $entityManager->flush();

            // substitute the previous line (redirect response) with this one.
            return $authenticator->authenticateUser(
                $user, 
                $formAuthenticator, 
                $request); 
        }

        return $this->render('registration/register.html.twig', [
            'registrationForm' => $form->createView(),
        ]);
    }
}

关于php - 使用新的 Symfony 5 Authenticator 注册后如何手动验证用户?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66924935/

相关文章:

php - 如何在 Symfony 5.3 的 Controller 中获取 Session 对象?

php - 如何显示来自特定类别的一个特定 WP 帖子(提取的 ID)

php - 流量大怎么会导致 "The session id is too long or contains illegal characters"

php - 消除重复条目的最优雅的方法

Symfony StopWatch 事件未出现在探查器时间线中

php - Symfony 5.2.3 接口(interface)类型提示

php - 如何使用PHP检查MySQL中是否存在一行

symfony - 如何在 symfony2 中创建国际化的静态页面?

php - Symfony 3 bcrypt 密码不验证

php - 在Symfony 5上使用DateTime约束时,为什么会收到 "This value should be of type string"?