-1

我在 Symfony 6 中登录时遇到问题。当我访问 /admin/login 时,我收到错误“Class App\Controller\AuthenticationUtils 不存在”

这是我的security.yaml

security:
encoders:
    App\Entity\User:
        algorithm: auto
    App\Entity\AdminUser:
        algorithm: auto


enable_authenticator_manager: true
# https://symfony.com/doc/current/security.html#registering-the-user-hashing-passwords
password_hashers:
    Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface: 'auto'
    App\Entity\User:
        algorithm: auto
    App\Entity\AdminUser:
        algorithm: auto

# https://symfony.com/doc/current/security.html#loading-the-user-the-user-provider
providers:
    # used to reload user from session & other features (e.g. switch_user)
    app_user_provider:
        entity:
            class: App\Entity\User
            property: email
    admin_user_provider:
        entity:
            class: App\Entity\AdminUser
            property: email
firewalls:
    dev:
        pattern: ^/(_(profiler|wdt)|css|images|js)/
        security: false
    admin:
        pattern: ^/admin
        lazy: true
        provider: admin_user_provider
        custom_authenticator: App\Security\AdminLoginFormAuthenticator
        logout:
            path: admin_logout
            # where to redirect after logout
            # TODO target: app_any_route
    main:
        lazy: true
        provider: app_user_provider
        custom_authenticator: App\Security\LoginFormAuthenticator
        logout:
            path: app_logout
            # where to redirect after logout
            # TODO target: app_any_route


        # activate different ways to authenticate
        # https://symfony.com/doc/current/security.html#the-firewall

        # https://symfony.com/doc/current/security/impersonating_user.html
        # switch_user: true

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

这是我的 AdminSecurityController.php


<?php

namespace App\Controller;

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

class AdminSecurityController extends AbstractController
{
    /**
     * @Route("/admin/security", name="admin_security")
     */
    public function index(): Response
    {
        return $this->render('admin_security/index.html.twig', [
            'controller_name' => 'AdminSecurityController',
        ]);
    }

    /**
     * @Route("/admin/login", name="admin_login")
     */
    public function login(AuthenticationUtils $authenticationUtils): Response
    {
        // get the login error if there is one
//        $error = $authenticationUtils->getLastAuthenticationError();
//        // last username entered by the user
//        $lastUsername = $authenticationUtils->getLastUsername();

        return $this->render('admin_login/index.html.twig'); //, ['last_username' => $lastUsername, 'error' => $error]
    }

    /**
     * @Route("/admin/logout", name="admin_logout")
     */
    public function logout()
    {
        throw new \Exception('This method can be blank - it will be intercepted by the logout key on your firewall');
    }
}

我附上了一些带有错误信息的图片

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

我不知道这个问题,希望你能帮助我!

谢谢 !!

4

1 回答 1

1

你错过了你的进口

use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;

在控制器的顶部

于 2022-02-04T16:50:59.930 回答