簡體   English   中英

Symfony4登錄無效憑據錯誤

[英]Symfony4 login invalid credentials error

我正在按照本指南設置簡單的登錄表單:

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

我已經完成了該文檔的所有操作,但是當我嘗試登錄時卻收到此錯誤:

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: {▶}
}

我也跟蹤了其他一些類似的問題,但是沒有一個問題對我有幫助。 最常見的情況通常與用戶注冊而不是登錄有關。 但是,通過比較https://bcrypt-generator.com/中的哈希值,我可以知道密碼已正確保存。 無論如何,這是我的注冊表:

報名表格:

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:

/**
 * @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()
    ]);
}

我可以確認$user->getPlainPassword()從表單密碼輸入中返回了正確的字符串。

我的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 }

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>

這個錯誤使我的發展停了一整天,我對此一無所知! 這實際上可能是什么問題?

您需要定義一個用戶提供程序,以從某些數據源加載用戶。 當前,您僅定義了in_memory用戶提供程序,因此,這是symfony嘗試加載用戶的位置。

您可以使用實體提供程序將您的用戶實體用作數據源。

將此添加到您的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