用户 App Entity User 没有用户提供程序 您的用户提供程序的 supportsClass() 方法不应该为这个类名返回 true 吗?
Posted
技术标签:
【中文标题】用户 App Entity User 没有用户提供程序 您的用户提供程序的 supportsClass() 方法不应该为这个类名返回 true 吗?【英文标题】:There is no user provider for user App Entity User Shouldn't the supportsClass() method of your user provider return true for this classname? 【发布时间】:2021-05-01 06:37:33 【问题描述】:我创建了一个电子邮件/密码登录表单,但遇到了一个非常令人失望的问题:(
首先,这是我得到的(我使用的是 Symfony 4.4.18):
&这些是我的代码:
这是用户实体(supportsClass() 方法添加)App\Entity\User:
<?php
namespace App\Entity;
use App\Repository\UserRepository;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Security\Core\User\UserInterface;
/**
* @ORM\Entity(repositoryClass=UserRepository::class)
*/
class User implements UserInterface, \Serializable
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255, unique=true)
*/
private $first_name;
/**
* @ORM\Column(type="string", length=255)
*/
private $last_name;
/**
* @ORM\Column(type="string", length=255, unique=true)
* @Assert\Regex(pattern="/^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/", message="Enter Valid Email")
*/
private $email;
/**
* @ORM\Column(type="string", length=11)
*/
private $phone_number;
/**
* @ORM\Column(type="string", length=255)
* @Assert\NotBlank(message="New password can not be blank.")
* @Assert\Regex(pattern="/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]8,$/", message="Minimum eight characters, at least one uppercase letter, one lowercase letter, one number and one special character")
*/
private $password;
public function getId(): ?int
return $this->id;
public function getFirstName(): ?string
return $this->first_name;
public function setFirstName(string $first_name): self
$this->first_name = $first_name;
return $this;
public function getLastName(): ?string
return $this->last_name;
public function setLastName(string $last_name): self
$this->last_name = $last_name;
return $this;
public function getEmail(): ?string
return $this->email;
public function setEmail(string $email): self
$this->email = $email;
return $this;
public function getPhoneNumber(): ?string
return $this->phone_number;
public function setPhoneNumber(string $phone_number): self
$this->phone_number = $phone_number;
return $this;
public function getPassword(): ?string
return $this->password;
public function setPassword(string $password): self
$this->password = $password;
return $this;
public function getRoles(): array
return [
'ROLE_USER'
];
/**
* @inheritDoc
*/
public function getSalt()
// TODO: Implement getSalt() method.
/**
* @inheritDoc
*/
public function getUsername()
// TODO: Implement getUsername() method.
/**
* @inheritDoc
*/
public function eraseCredentials()
// TODO: Implement eraseCredentials() method.
/**
* @inheritDoc
*/
public function serialize()
return serialize([
$this->id,
$this->first_name,
$this->last_name,
$this->email,
$this->phone_number,
$this->password,
]);
/**
* @inheritDoc
*/
public function unserialize($serialized)
list(
$this->id,
$this->first_name,
$this->last_name,
$this->email,
$this->phone_number,
$this->password,
) = unserialize($serialized, ['allowed_classes' => false]);
public function supportsClass($class)
return $class === User::class;
登录表单验证器 App\Security\LoginFormAuthenticator(未使用 bin/console make:auth):
<?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\Encoder\UserPasswordEncoderInterface;
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\Guard\PasswordAuthenticatedInterface;
use Symfony\Component\Security\Http\Util\TargetPathTrait;
class LoginFormAuthenticator extends AbstractFormLoginAuthenticator implements PasswordAuthenticatedInterface
use TargetPathTrait;
public const LOGIN_ROUTE = 'login';
private $entityManager;
private $urlGenerator;
private $csrfTokenManager;
private $passwordEncoder;
public function __construct(EntityManagerInterface $entityManager, UrlGeneratorInterface $urlGenerator, CsrfTokenManagerInterface $csrfTokenManager, UserPasswordEncoderInterface $passwordEncoder)
$this->entityManager = $entityManager;
$this->urlGenerator = $urlGenerator;
$this->csrfTokenManager = $csrfTokenManager;
$this->passwordEncoder = $passwordEncoder;
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)
throw new CustomUserMessageAuthenticationException("Email Not Found!");
return $user;
public function checkCredentials($credentials, UserInterface $user)
return $this->passwordEncoder->isPasswordValid($user, $credentials['password']);
public function getPassword($credentials): ?string
return $credentials['password'];
public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey)
if (!$targetPath = $this->getTargetPath($request->getSession(), $providerKey))
return new RedirectResponse($targetPath);
return new RedirectResponse($this->urlGenerator->generate('dashboard'));
protected function getLoginUrl()
return $this->urlGenerator->generate(self::LOGIN_ROUTE);
登录控制器 App\Controller\LoginController:
<?php
namespace App\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
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\Http\Authentication\AuthenticationUtils;
class LoginController extends AbstractController
/**
* @Route("/login", name="login")
* @Method("POST")
* @param Request $request
* @param AuthenticationUtils $utils
* @return Response
*/
public function login(Request $request, AuthenticationUtils $utils): Response
$error = $utils->getLastAuthenticationError();
$lastEmail = $utils->getLastUsername();
return $this->render('login/login.html.twig', [
'error' => $error,
'email' => $lastEmail,
]);
/**
* @Route("/logout", name="logout")
*/
public function logout()
login.html.twig:
% extends 'base.html.twig' %
% block title %LoginController!% endblock %
% block body %
<style>
.error color: red;
</style>
<h1 class="page-title">Login</h1>
<div class="page-title">
% if error %
<span class="alert alert-danger"> error.messageKey|trans(error.messageData, 'security') </span>
% endif %
% if app.user %
<div class="mb-3">
You are logged in as app.user.firstName , <a href=" path('logout') ">Logout</a>
</div>
% endif %
<form method="post">
<label for="email">Email: </label>
<input type="email" name="email" value=" email " placeholder="Email here ...">
<label for="password">Password: </label>
<input type="password" name="password" placeholder="Password here ...">
<input type="hidden" name="_csrf_token" value=" csrf_token('authenticate') ">
<button type="submit">Login</button>
</form>
</div>
% endblock %
security.yaml 配置:
security:
encoders:
App\Entity\User:
algorithm: bcrypt
providers:
user_provider:
entity:
class: APP\Entity\User
property: email
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main:
anonymous: true
provider: user_provider
guard:
authenticators:
- App\Security\LoginFormAuthenticator
logout:
path: logout
access_control:
- path: ^/login$, roles: IS_AUTHENTICATED_ANONYMOUSLY
- path: ^/register, roles: IS_AUTHENTICATED_ANONYMOUSLY
- path: ^/, roles: ROLE_USER
- path: ^/admin, roles: ROLE_ADMIN
我对supportsClass() 没有任何想法。 & 之前尝试过自定义用户提供程序的用户存储库,但他们没有工作
【问题讨论】:
【参考方案1】:这里有一个错字:APP\Entity\User -> App\Entity\User
【讨论】:
以上是关于用户 App Entity User 没有用户提供程序 您的用户提供程序的 supportsClass() 方法不应该为这个类名返回 true 吗?的主要内容,如果未能解决你的问题,请参考以下文章
命名类型 [com.go_task.entity.User@5b4d25e7] 没有实现 BasicType 和 UserType
ZF2:在 'Application\Entity\ #user' 中找不到目标实体