このガイドに従って、独自のカスタム ユーザー ログインを実装しました。残念ながら、Bad credentials
ログイン中に表示されます。この例外は、 の 72 行目から発生しますSymfony\Component\Security\Core\Authentication\Provider\UserAuthenticationProvider
。ユーザーを取得できないため、この例外がスローされます。
カスタムのニーズに合わせて変更したのは、ユーザーにユーザー名がないことです。彼らは自分のメールアドレスでログインします。しかし、それを実装するのは問題ないと思います。
security.yml:
security:
encoders:
Acme\UserBundle\Entity\User: plaintext
role_hierarchy:
ROLE_ADMIN: ROLE_USER
ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]
providers:
administrators:
entity: { class: AcmeUserBundle:User }
firewalls:
secured_area:
pattern: ^/
anonymous: ~
form_login: ~
access_control:
- { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/register, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/, roles: ROLE_USER }
ユーザーリポジトリ:
class UserRepository extends EntityRepository implements UserProviderInterface
{
public function loadUserByUsername($username)
{
$q = $this
->createQueryBuilder('u')
->where('u.email = :email')
->setParameter('email', $username)
->getQuery();
try {
// The Query::getSingleResult() method throws an exception
// if there is no record matching the criteria.
$user = $q->getSingleResult();
} catch (NoResultException $e) {
$message = sprintf(
'Unable to find an active admin AcmeUserBundle:User object identified by "%s".',
$username
);
throw new UsernameNotFoundException($message, 0, $e);
}
return $user;
}
public function refreshUser(UserInterface $user)
{
$class = get_class($user);
if (!$this->supportsClass($class)) {
throw new UnsupportedUserException(
sprintf(
'Instances of "%s" are not supported.',
$class
)
);
}
return $this->find($user->getId());
}
public function supportsClass($class)
{
return $this->getEntityName() === $class
|| is_subclass_of($class, $this->getEntityName());
}
}
login.twig.html:
{% if error %}
<div>{{ error.message }}</div>
{% endif %}
<form action="{{ path('login_check') }}" method="post">
<legend>Login</legend>
<label for="email">Email:</label>
<input type="email" id="email" name="_email" value="{{ email }}"
<label for="password">Password:</label>
<input type="password" id="password" name="_password" />
<button type="submit">Login</button>
</form>
ここで私は何を間違えましたか?メールをユーザー名として明確にUserRepository
クエリするのに、なぜユーザーを見つけられないのでしょうか? 私はそれが何かと関係があるという推測を持っていcsrf_token
ますか?コントローラーとtwigファイルに追加するにはどうすればよいですか? これはまったく問題ですか、それとも私が間違ったことをしたのでしょうか?