Аутентификатор Guard не поддерживает запрос после перехода с XAMPP на LAMPP. - PullRequest
0 голосов
/ 20 апреля 2020

У меня есть приложение symfony 4, разработанное в XAMPP. Теперь я установил LAMPP в Unbuntu через несколько недель go. Теперь, когда я впервые установил свой проект symfony в Ubuntu с правами доступа 775, он не работает. В браузере я получаю сообщение «Not Found - запрошенный URL не найден на этом сервере». И единственное, что я могу найти в моих журналах (ошибок), это:

[2020-04-19T22:56:51.564711+02:00] request.INFO: Matched route "homepage". {"route":"homepage","route_parameters":{"_route":"homepage","path":"/login","permanent":false,"keepQueryParams":true,"_controller":"Symfony\Bundle\FrameworkBundle\Controller\RedirectController::urlRedirectAction"},"request_uri":"http://192.168.0.100:8082/","method":"GET"} []
[2020-04-19T22:56:51.569203+02:00] security.DEBUG: Checking for guard authentication credentials. {"firewall_key":"main","authenticators":1} []
[2020-04-19T22:56:51.569331+02:00] security.DEBUG: Checking support on guard authenticator. {"firewall_key":"main","authenticator":"App\Security\LoginFormAuthenticator"} []
[2020-04-19T22:56:51.569400+02:00] security.DEBUG: Guard authenticator does not support the request. {"firewall_key":"main","authenticator":"App\Security\LoginFormAuthenticator"} []

И я не знаю, почему это не работает. Вы можете мне помочь?

Вот код аутентификатора гвардии:

<?php

namespace App\Security;

use App\Entity\User;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException;
use Symfony\Component\Security\Core\Exception\InvalidCsrfTokenException;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Csrf\CsrfToken;
use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
use Symfony\Component\Security\Guard\Authenticator\AbstractFormLoginAuthenticator;
use Symfony\Component\Security\Http\Util\TargetPathTrait;
use Symfony\Contracts\Translation\TranslatorInterface;

/**
* Authenticator for login form
*
*/
class LoginFormAuthenticator extends AbstractFormLoginAuthenticator
{
   use TargetPathTrait;

   private $entityManager;
   private $urlGenerator;
   private $csrfTokenManager;
   private $passwordEncoder;
   private $translator;

   /**
    * LoginFormAuthenticator constructor
    *
    * @param EntityManagerInterface $entityManager entity manager
    * @param UrlGeneratorInterface $urlGenerator url generator
    * @param CsrfTokenManagerInterface $csrfTokenManager csrf token manager
    * @param UserPasswordEncoderInterface $passwordEncoder password encoder
    * @param TranslatorInterface $translator translator
    */
   public function __construct(EntityManagerInterface $entityManager, UrlGeneratorInterface $urlGenerator, CsrfTokenManagerInterface $csrfTokenManager, UserPasswordEncoderInterface $passwordEncoder, TranslatorInterface $translator)
   {
       $this->entityManager = $entityManager;
       $this->urlGenerator = $urlGenerator;
       $this->csrfTokenManager = $csrfTokenManager;
       $this->passwordEncoder = $passwordEncoder;
       $this->translator = $translator;
   }

   /**
     * Called on every request to decide if this authenticator should be
     * used for the request.
     *
     * @param Request $request request
     *
     * @return boolean Returning false will cause this authenticator to be skipped.
     */
   public function supports(Request $request)
   {
       return 'login' === $request->attributes->get('_route') && $request->isMethod('POST');
   }

   /**
    * Get credentials
    *
    * @param Request $request request
    *
    * @return Array Returns with credentials array
    */
   public function getCredentials(Request $request)
   {
       $credentials = [
           'email' => $request->request->get('login')["email"],
           'password' => $request->request->get('login')["password"],
           'csrf_token' => $request->request->get('login')["_token"],
       ];
       $request->getSession()->set(
           Security::LAST_USERNAME,
           $credentials['email']
       );

       return $credentials;
   }

   /**
    * Get user
    *
    * @param Array $credentials credentials
    * @param UserInterface $user user
    *
    * @return boolean returns true if password is valid. Otherwise false.
    */
   public function getUser($credentials, UserProviderInterface $userProvider)
   {
       $token = new CsrfToken('authenticate', $credentials['csrf_token']);
       if (!$this->csrfTokenManager->isTokenValid($token)) {
           //throw new CustomUserMessageAuthenticationException($this->translator->trans('security.login.csrf', [], 'security'));
           throw new InvalidCsrfTokenException();
       }

       $user = $this->entityManager->getRepository(User::class)->findUser($credentials['email']);

       if (!$user) {
           // fail authentication with a custom error
           throw new CustomUserMessageAuthenticationException($this->translator->trans('general.error.emailOrUsername.not.found', [], 'messages'));
       }
       else {
           if(!$user->getActive()) {
               throw new CustomUserMessageAuthenticationException($this->translator->trans('general.error.emailOrUsername.not.active', [], 'messages'));
           }
           elseif(!$user->getAccepted()) {
               throw new CustomUserMessageAuthenticationException($this->translator->trans('general.error.emailOrUsername.not.accepted.terms', [], 'messages'));
           }
       }

       return $user;
   }

   /**
    * Check credentials
    *
    * @param Array $credentials credentials
    * @param UserInterface $user user
    *
    * @return boolean returns true if password is valid. Otherwise false.
    */
   public function checkCredentials($credentials, UserInterface $user)
   {
       return $this->passwordEncoder->isPasswordValid($user, $credentials['password']);
   }

   /**
    * Override to change what happens after a correct username/password is submitted.
    *
    * @param Request $request request
    * @param TokenInterface $token token
    * @param String $providerKey provider key
    *
    * @return RedirectResponse
    */
   public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey)
   {
       if(!in_array($request->attributes->get('_route'), ['register'])) {
           $user = $token->getUser();
           $user->setLastLogin(new \DateTime());
           $this->entityManager->persist($token->getUser());
           $this->entityManager->flush();
       }

       if ($targetPath = $this->getTargetPath($request->getSession(), $providerKey)) {
           return new RedirectResponse($targetPath);
       }

       return new RedirectResponse($this->urlGenerator->generate('admin_dashboard'));
   }

   /**
    * get login url
    *
    *  @return string returns url
    */
   protected function getLoginUrl()
   {
       return $this->urlGenerator->generate('login');
   }
}

1 Ответ

0 голосов
/ 24 апреля 2020

Я нашел свою проблему. В Xampp мой symfony проект работает без .htaccess в каталоге / publi c. В LAMPP мне нужен .htaccess в каталоге / publi c.

Поэтому я установил composer require symfony/apache-pack в мой проект symfony, нажав y (ы) во время установки. Теперь это работает.

...