首页 > 解决方案 > 如何在 Symfony 4 中创建登录页面而不出现错误“InvalidConfigurationException”?

问题描述

配置/包/security.yaml:

security:
  encoders:
    App\Entity\User:
      algorithm: bcrypt

      # ...

      providers:
        our_db_provider:
          entity:
            class: App\Entity\User
            property: username
            # if you're using multiple entity managers
            # manager_name: customer

            firewalls:
              main:
                anonymous: ~
                form_login:
                  login_path: login
                  check_path: login

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

src/控制器/SecurityController.php:

<?php

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;

class SecurityController extends Controller
{
  /**
  * @Route("/login", name="login")
  */
  public function login(Request $request, AuthenticationUtils $authenticationUtils)
  {
      // get the login error if there is one
      $error = $authenticationUtils->getLastAuthenticationError();

      // last username entered by the user
      $lastUsername = $authenticationUtils->getLastUsername();

      return $this->render('security/login.html.twig', array(
          'last_username' => $lastUsername,
          'error'         => $error,
      ));
  }
}

src/Controller/DefaultController.php

<?php

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class DefaultController extends Controller
{
    /**
     * @Route("/admin")
     */
    public function admin()
    {
        return new Response('<html><body>Admin page!</body></html>');
    }
}

模板/安全/login.html.twig:

{% extends 'base.html.twig' %}

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

<form action="{{ path('login') }}" method="post">
    <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" />

    {#
        If you want to control the URL the user
        is redirected to on success (more details below)
        <input type="hidden" name="_target_path" value="/account" />
    #}

    <button type="submit">login</button>
</form>

我想加载我的 /admin 或 /login 页面,但出现错误:

InvalidConfigurationException “security.encoders.App\Entity\User”下无法识别的选项“providers”

在 ArrayNode->normalizeValue(array('providers' => array('our_db_provider' => array('entity' => array('class' => 'App\Entity\User', 'property' => 'email' , 'firewalls' => array('main' => array('anonymous' => null, 'form_login' => array('login_path' => 'login', 'check_path' => 'login', 'access_control' => array(array('path' => '^/login', 'roles' => 'IS_AUTHENTICATED_ANONYMOUSLY'), array('path' => '^/', 'roles' => 'ROLE_ADMIN'))) ))))))) 在 BaseNode.php 第 368 行

标签: phpsymfonysecurityloginerror-handling

解决方案


您的security.yaml配置中的列表错误。你有这个:

security:
  encoders:
    App\Entity\User:
      algorithm: bcrypt

      # ...

      providers:

它应该是:

security:
  encoders:
    App\Entity\User:
      algorithm: bcrypt

      # ...

  providers:

encoders和应该都是配置下的providers第 1 级子级security


推荐阅读