php - 如何在 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 行
解决方案
您的security.yaml配置中的列表错误。你有这个:
security:
encoders:
App\Entity\User:
algorithm: bcrypt
# ...
providers:
它应该是:
security:
encoders:
App\Entity\User:
algorithm: bcrypt
# ...
providers:
encoders
和应该都是配置下的providers
第 1 级子级security
。
推荐阅读
- cluster-computing - 我怎样才能让神鹰收集器运行
- flutter - 如果参数为空,则使用默认值
- email - 如何唯一标识邮件回复?
- android - 如何修复 Android Studio 上的 Error Type 3 错误?
- c++ - 如何从 C++ 模板中解压 std::tuple?
- c++ - 设置循环等待用户输入(C++)
- matlab - FOC 算法中的非常量转矩问题 (Simulink)
- node.js - post.save 不起作用(Node/Express/MongoDB)
- apache-spark - Spark SQL - 加入后保留一个结果
- c# - 获取动态 JSON 值并打印出来