2

我正在我的 symfony 应用程序中实现一个带保护的登录系统。

我已经设置了系统,但是我犯了一个错误,因此我无法登录...

要点:

我的用户实体实现了“AdvancedUserInterface,\Serializable”,它提供了电子邮件属性而不是用户名属性。此外,我将“getUsername”函数更改为:

public function getUsername()
{
    return $this->email;
}

安全性.yml:

# app/config/security.yml
security:
    providers:
        main_db_provider:
            entity:
                class: AppBundle:User
                property: email

    encoders:
        AppBundle\Entity\User:
            algorithm: bcrypt

    firewalls:
        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false

        main:
            anonymous: ~
            logout:
                path:   /logout
                target: /
            guard:
                authenticators:
                    - form_authenticator
            provider: main_db_provider

    access_control:
        - { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/, roles: ROLE_ADMIN }

服务.yml:

services:
    form_authenticator:
          class: AppBundle\Security\FormAuthenticator
          arguments: ["@service_container"]

login.html.twig:

<form action="{{ path('login') }}" method="POST">
    <input type="text" name="email">
    <input type="text" name="password">
    <input type="submit" name="submit" value="Submit">
</form>

登录控制器(部分):

/**
 * @Route("/login", name="login")
 */
public function loginAction(Request $request) {

    $authenticationUtils = $this->get('security.authentication_utils');

    $error = $authenticationUtils->getLastAuthenticationError();
    $lastUsername = $authenticationUtils->getLastUsername();

    return $this->render(
        'AppBundle:login:index.html.twig',
        [
            'error' => $error ? $error->getMessage() : NULL,
            'last_username' => $lastUsername
        ]
    );
}

最后是我的 FormAuthentificator:

class FormAuthenticator extends AbstractGuardAuthenticator
{

    private $container;

    /**
     * Default message for authentication failure.
     *
     * @var string
     */
    private $failMessage = 'Invalid credentials';

    /**
     * Creates a new instance of FormAuthenticator
     */
    public function __construct(ContainerInterface $container)
    {
        $this->container = $container;
    }

    /**
     * {@inheritdoc}
     */
    public function getCredentials(Request $request)
    {
        if ($request->getPathInfo() != '/login' || !$request->isMethod('POST')) {
            return;
        }

        return array(
            'email' => $request->request->get('email'),
            'password' => $request->request->get('password'),
        );
    }

    /**
     * {@inheritdoc}
     */
    public function getUser($credentials, UserProviderInterface $userProvider)
    {
        $email = $credentials['email'];

        return $userProvider->loadUserByUsername($email);
    }

    /**
     * {@inheritdoc}
     */
    public function checkCredentials($credentials, UserInterface $user)
    {
        $plainPassword = $credentials['password'];
        $encoder = $this->container->get('security.password_encoder');

        if (!$encoder->isPasswordValid($user, $plainPassword)) {
            throw new CustomUserMessageAuthenticationException($this->failMessage);
        }

        return true;
    }

    /**
     * {@inheritdoc}
     */
    public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey)
    {
        $url = $this->container->get('router')->generate('backend');
        return new RedirectResponse($url);
    }

    /**
     * {@inheritdoc}
     */
    public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
    {
        $request->getSession()->set(Security::AUTHENTICATION_ERROR, $exception);
        $url = $this->container->get('router')->generate('login');
        return new RedirectResponse($url);
    }

    /**
     * {@inheritdoc}
     */
    public function start(Request $request, AuthenticationException $authException = null)
    {
        $url = $this->container->get('router')->generate('login');
        return new RedirectResponse($url);
    }

    /**
     * {@inheritdoc}
     */
    public function supportsRememberMe()
    {
        return false;
    }
}

当我输入我的有效凭据时,我得到:

Invalid credentials:

我也尝试过使用其他凭据,但总是同样的错误。

有人可以帮我解决这个问题吗?

谢谢和问候!

4

0 回答 0