简体   繁体   English

Symfony4登录无效凭据错误

[英]Symfony4 login invalid credentials error

I am following this guide to set up a simple login form: 我正在按照本指南设置简单的登录表单:

https://symfony.com/doc/current/security/form_login_setup.html https://symfony.com/doc/current/security/form_login_setup.html

I have done everything from this doc, however when I try to login I get this error: 我已经完成了该文档的所有操作,但是当我尝试登录时却收到此错误:

BadCredentialsException {#777 ▼
  -token: UsernamePasswordToken {#778 ▶}
  #message: "Bad credentials."
  #code: 0
  #file: "/var/www/html/advert-board/vendor/symfony/security/Core/Authentication/Provider/UserAuthenticationProvider.php"
  #line: 69
  trace: {▶}
}

I have followed some other similar questions but none of them helped me. 我也跟踪了其他一些类似的问题,但是没有一个问题对我有帮助。 Most common cases are usually related to the user registration rather than login. 最常见的情况通常与用户注册而不是登录有关。 However by comparing the hashes in https://bcrypt-generator.com/ I can tell that the password is saved correctly. 但是,通过比较https://bcrypt-generator.com/中的哈希值,我可以知道密码已正确保存。 In any case this is my registration form: 无论如何,这是我的注册表:

RegistrationForm: 报名表格:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('email', EmailType::class)
        ->add('username', TextType::class)
        ->add('plainPassword', RepeatedType::class, [
            'type' => PasswordType::class,
            'first_options' => ['label' => 'Password'],
            'second_options' => ['label' => 'Repeat Password']
        ]);
}

public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults([
        'data_class' => User::class,
        'csrf_protection' => true,
        'csrf_field_name' => '_token',
        'csrf_token_id'   => 'user_item',
    ]);
}

RegistrationController: 这个RegistrationController:

/**
 * @Route("/register", name="registration")
 *
 * @param Request $request
 * @return \Symfony\Component\HttpFoundation\Response
 */
public function register(Request $request, UserPasswordEncoderInterface $passwordEncoder)
{

    $user = new User();
    $form = $this->createForm(RegistrationForm::class, $user);

    $form->handleRequest($request);

    if($form->isSubmitted() && $form->isValid()) {

        $params = $request->request->all();
        $password = $passwordEncoder->encodePassword($user, $user->getPlainPassword());

        $user->setPassword($password);

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

        return $this->redirectToRoute('homepage');
    }

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

I can confirm that $user->getPlainPassword() is returning the correct string from the form password input. 我可以确认$user->getPlainPassword()从表单密码输入中返回了正确的字符串。

My security.yml file: 我的security.yml文件:

security:
    encoders:
        App\Entity\User: bcrypt
    # https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
    providers:
        in_memory: { memory: ~ }
    firewalls:
        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false
        main:
            anonymous: ~
            form_login:
                login_path: login
                check_path: login
        secured_area:
            form_login:
                csrf_token_generator: security.csrf.token_manager
            # activate different ways to authenticate

            # http_basic: true
            # https://symfony.com/doc/current/security.html#a-configuring-how-your-users-will-authenticate

            # form_login: true
            # https://symfony.com/doc/current/security/form_login_setup.html

    # Easy way to control access for large sections of your site
    # Note: Only the *first* access control that matches will be used
    access_control:
         - { path: ^/, roles: IS_AUTHENTICATED_ANONYMOUSLY }
         - { path: ^/profile, roles: ROLE_USER }

The login.html.twig: login.html.twig:

    {% if error %}
        {{ dump(error) }}
        <div>{{ error.messageKey|trans(error.messageData, 'security') }}</div>
    {% endif %}

    <form action="{{ path('login') }}" method="post">
        <input type="hidden" name="_csrf_token" value="{{ csrf_token('authenticate') }}">

        <label for="username">Username:</label>
        <input type="text" id="username" name="_username" value="{{ last_username }}" />

        <label for="password">Password:</label>
        <input type="password" id="password" name="_password" />

        <input type="hidden" name="_target_path" value="/profile" />
        <button type="submit">login</button>
    </form>

This error has stoped my development for a full day an i am losing my mind over this!! 这个错误使我的发展停了一整天,我对此一无所知! What can actually be the problem here? 这实际上可能是什么问题?

You need to define a user provider which loads the user from some data source. 您需要定义一个用户提供程序,以从某些数据源加载用户。 Currently you only have the in_memory user provider defined, so this is where symfony tries to load the users. 当前,您仅定义了in_memory用户提供程序,因此,这是symfony尝试加载用户的位置。

You could use the entity provider to use your user entity as a data source. 您可以使用实体提供程序将您的用户实体用作数据源。

Add this to your security.yml : 将此添加到您的security.yml

providers:
    doctrine_user_provider:
        entity:
            class: App\Entity\User
            property: username

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM