Php 在Symfony中找不到对null上的成员函数isPasswordValid()调用的解决方案

Php 在Symfony中找不到对null上的成员函数isPasswordValid()调用的解决方案,php,symfony,authentication,Php,Symfony,Authentication,当我尝试登录到注册用户时,我不能超过此错误。 我的授权人 <?php namespace App\Security; use App\Entity\User; use Doctrine\ORM\EntityManagerInterface; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Ro

当我尝试登录到注册用户时,我不能超过此错误。 我的授权人

<?php

namespace App\Security;
use App\Entity\User;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException;
use Symfony\Component\Security\Core\Exception\InvalidCsrfTokenException;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Csrf\CsrfToken;
use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
use Symfony\Component\Security\Guard\Authenticator\AbstractFormLoginAuthenticator;
use Symfony\Component\Security\Http\Util\TargetPathTrait;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;

class UserLoginAuthentificatorAuthenticator extends AbstractFormLoginAuthenticator
{
    use TargetPathTrait;

    public const LOGIN_ROUTE = 'app_login';

    private $entityManager;
    private $urlGenerator;
    private $csrfTokenManager;
    private $passwordEncoder;

    public function __construct(EntityManagerInterface $entityManager, UrlGeneratorInterface $urlGenerator, CsrfTokenManagerInterface $csrfTokenManager)
    {
        $this->entityManager = $entityManager;
        $this->urlGenerator = $urlGenerator;
        $this->csrfTokenManager = $csrfTokenManager;
    }

    public function supports(Request $request)
    {
        return self::LOGIN_ROUTE === $request->attributes->get('_route')
            && $request->isMethod('POST');
    }

    public function getCredentials(Request $request)
    {
        $credentials = [
            'email' => $request->request->get('email'),
            'password' => $request->request->get('password'),
            'csrf_token' => $request->request->get('_csrf_token'),
        ];
        $request->getSession()->set(
            Security::LAST_USERNAME,
            $credentials['email']
        );

        return $credentials;
    }

    public function getUser($credentials, UserProviderInterface $userProvider)
    {
        $token = new CsrfToken('authenticate', $credentials['csrf_token']);
        if (!$this->csrfTokenManager->isTokenValid($token)) {
            throw new InvalidCsrfTokenException();
        }

        $user = $this->entityManager->getRepository(User::class)->findOneBy(['email' => $credentials['email']]);

        if (!$user) {
            // fail authentication with a custom error
            throw new CustomUserMessageAuthenticationException('Email could not be found.');
        }

        return $user;
    }

    public function checkCredentials($credentials, UserInterface $user)
    {


    return $this->passwordEncoder->isPasswordValid($user->getPassword(), $credentials['password'],$user->getSalt());

        // Check the user's password or other credentials and return true or false
        // If there are no credentials to check, you can just return true
        //throw new \Exception('TODO: check the credentials inside '.__FILE__);
    }

    public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $providerKey)
    {
        if ($targetPath = $this->getTargetPath($request->getSession(), $providerKey)) {
            return new RedirectResponse($targetPath);
        }

        // For example : return new RedirectResponse($this->urlGenerator->generate('some_route'));
        throw new \Exception('TODO: provide a valid redirect inside '.__FILE__);
    }

    protected function getLoginUrl()
    {
        return $this->urlGenerator->generate(self::LOGIN_ROUTE);
    }
}

我是Symfony的乞丐我不止一次读过Symfony安全章节。我一定错过了什么。如果有人能帮我在AUAuthenticator中使用这个,而不是这个,那就太好了:

public function checkCredentials($credentials, UserInterface $user)
{
return $this->passwordEncoder->isPasswordValid($user->getPassword(), $credentials['password'],$user->getSalt());
}
providers:
    users_in_memory: { memory: null }
    main:
        anonymous: false
        lazy: true
        provider: users_in_memory
        guard:
            authenticators:
                - App\Security\UserLoginAuthentificatorAuthenticator
使用以下命令:

public function checkCredentials($credentials, UserInterface $user)
{
return $this->passwordEncoder->isPasswordValid($user, $credentials['password']);
}
providers:
    app_user_provider:
        entity:
            class: App\Entity\User
            property: email
    main:
        anonymous: true
        lazy: true
        provider: app_user_provider
        guard:
            authenticators:
                - App\Security\UserAuthenticator

在security.yaml中,而不是在该文件中:

public function checkCredentials($credentials, UserInterface $user)
{
return $this->passwordEncoder->isPasswordValid($user->getPassword(), $credentials['password'],$user->getSalt());
}
providers:
    users_in_memory: { memory: null }
    main:
        anonymous: false
        lazy: true
        provider: users_in_memory
        guard:
            authenticators:
                - App\Security\UserLoginAuthentificatorAuthenticator
使用以下命令:

public function checkCredentials($credentials, UserInterface $user)
{
return $this->passwordEncoder->isPasswordValid($user, $credentials['password']);
}
providers:
    app_user_provider:
        entity:
            class: App\Entity\User
            property: email
    main:
        anonymous: true
        lazy: true
        provider: app_user_provider
        guard:
            authenticators:
                - App\Security\UserAuthenticator

而不是这个:

public function checkCredentials($credentials, UserInterface $user)
{
return $this->passwordEncoder->isPasswordValid($user->getPassword(), $credentials['password'],$user->getSalt());
}
providers:
    users_in_memory: { memory: null }
    main:
        anonymous: false
        lazy: true
        provider: users_in_memory
        guard:
            authenticators:
                - App\Security\UserLoginAuthentificatorAuthenticator
使用以下命令:

public function checkCredentials($credentials, UserInterface $user)
{
return $this->passwordEncoder->isPasswordValid($user, $credentials['password']);
}
providers:
    app_user_provider:
        entity:
            class: App\Entity\User
            property: email
    main:
        anonymous: true
        lazy: true
        provider: app_user_provider
        guard:
            authenticators:
                - App\Security\UserAuthenticator

并在控制器中添加:

    $this->passwordEncoder = $passwordEncoder;
之后:

    $this->csrfTokenManager = $csrfTokenManager;
像这样:

public function __construct(EntityManagerInterface $entityManager, UrlGeneratorInterface $urlGenerator, CsrfTokenManagerInterface $csrfTokenManager, UserPasswordEncoderInterface $passwordEncoder)
{
    $this->entityManager = $entityManager;
    $this->urlGenerator = $urlGenerator;
    $this->csrfTokenManager = $csrfTokenManager;
    $this->passwordEncoder = $passwordEncoder;
}

您似乎忘记在验证器的构造函数中分配
$this->passwordEncoder=$passwordEncoder
。感谢您的帮助意味着您正在使用新的验证器功能。它与保护身份验证器不兼容。即使您有了工作,也应该重新启动一个项目,决定是否要使用新的身份验证系统,然后再次运行make:auth。@Cerad,您说得对。谢谢您的帮助,就这样。我以前尝试过这些修改,但我没有向好的提供者寻址,也没有分配$passwordEncoder。我必须删除security.yalm中的anonymous=true。我在告诉别人没有使用匿名时出错,我不得不删除它。@如果这个答案解决了你的问题,或者对找到你的解决方案最有帮助,菲尔就接受这个答案