首页 > 解决方案 > Symfony 4 切换到生产模式时无法登录

问题描述

我有一个奇怪的问题,当我在 symfony 4 上将环境切换到生产环境时,登录不起作用,它没有给我任何错误,它只是重新加载登录页面。一旦我再次将其切换到开发,它就可以工作。由于没有错误,我不知道发生了什么,并想知道是否有人以前发现过这个问题?

我还注意到,由于某种原因,symfony 从未在 var/log 中生成任何日志文件——没有开发日志或生产日志,知道为什么会这样吗?

登录在管理端不起作用,字体端起作用,但它使用不同的方法,前面是有角度的,并且对服务器进行 ajax 调用以登录然后使用 JWT,但管理只是基本的 symfony 不同的用户模型和 AdminAuthenticator.php ->

<?php

namespace App\Security;

use App\Entity\AdminUser;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException;
use Symfony\Component\Security\Core\Exception\InvalidCsrfTokenException;
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\Guard\PasswordAuthenticatedInterface;
use Symfony\Component\Security\Http\Util\TargetPathTrait;

/**
 * Class AdminAuthenticator
 * @package App\Security
 */
class AdminAuthenticator extends AbstractFormLoginAuthenticator implements PasswordAuthenticatedInterface
{

    use TargetPathTrait;

    /**
     *
     */
    public const LOGIN_ROUTE = 'adminLogin';

    /**
     * @var EntityManagerInterface
     */
    private $entityManager;
    /**
     * @var UrlGeneratorInterface
     */
    private $urlGenerator;
    /**
     * @var CsrfTokenManagerInterface
     */
    private $csrfTokenManager;
    /**
     * @var UserPasswordEncoderInterface
     */
    private $passwordEncoder;

    /**
     * AdminAuthenticator constructor.
     * @param EntityManagerInterface $entityManager
     * @param UrlGeneratorInterface $urlGenerator
     * @param CsrfTokenManagerInterface $csrfTokenManager
     * @param UserPasswordEncoderInterface $passwordEncoder
     */
    public function __construct(EntityManagerInterface $entityManager, UrlGeneratorInterface $urlGenerator, CsrfTokenManagerInterface $csrfTokenManager, UserPasswordEncoderInterface $passwordEncoder)
    {
        $this->entityManager = $entityManager;
        $this->urlGenerator = $urlGenerator;
        $this->csrfTokenManager = $csrfTokenManager;
        $this->passwordEncoder = $passwordEncoder;
    }


    /**
     * @return string|void
     */
    protected function getLoginUrl()
    {
        return $this->urlGenerator->generate(self::LOGIN_ROUTE);
    }

    /**
     * @param Request $request
     * @return bool|void
     */
    public function supports(Request $request)
    {
        return self::LOGIN_ROUTE === $request->attributes->get('_route')
            && $request->isMethod('POST');
    }

    /**
     * @param Request $request
     * @return mixed|void
     */
    public function getCredentials(Request $request)
    {
        $credentials = [
            'email' => $request->request->get('email'),
            'password' => $request->request->get('password'),
            'csrf_token' => $request->request->get('_csrf_token'),
        ];
        $request->getSession()->set(
            Security::LAST_USERNAME,
            $credentials['email']
        );

        return $credentials;
    }

    /**
     * @param mixed $credentials
     * @param UserProviderInterface $userProvider
     * @return object
     */
    public function getUser($credentials, UserProviderInterface $userProvider)
    {
        $token = new CsrfToken('authenticate', $credentials['csrf_token']);
        if (!$this->csrfTokenManager->isTokenValid($token)) {
            throw new InvalidCsrfTokenException();
        }

        $AdminUser = $this->entityManager->getRepository(AdminUser::class)->findOneBy(['email' => $credentials['email']]);

        if (!$AdminUser) {
            // fail authentication with a custom error
            throw new CustomUserMessageAuthenticationException('Email could not be found.');
        }

        return $AdminUser;
    }

    /**
     * @param mixed $credentials
     * @param UserInterface $user
     * @return bool|void
     */
    public function checkCredentials($credentials, UserInterface $user)
    {
        return $this->passwordEncoder->isPasswordValid($user, $credentials['password']);
    }

    /**
     * @param Request $request
     * @param TokenInterface $token
     * @param string $providerKey
     * @return Response|void|null
     * @throws \Exception
     */
    public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $providerKey)
    {
        if ($targetPath = $this->getTargetPath($request->getSession(), $providerKey)) {
            return new RedirectResponse($targetPath);
        }

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

    }

    /**
     * @param $credentials
 * @return string|null
 */
public function getPassword($credentials): ?string
{
    return $credentials['password'];
}

}

这是安全配置:

security:

  # https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
  providers:
    # used to reload user from session & other features (e.g. switch_user)
    chain_provider:
      chain:
        providers: [app_user_username,app_user_email]
    app_user_username:
      entity:
        class: App\Entity\User
        property: username
    app_user_email:
      entity:
        class: App\Entity\User
        property: email
    adminusers:
      entity:
        class: App\Entity\AdminUser
        property: username


  firewalls:
    dev:
      pattern: ^/(_(profiler|wdt)|css|images|js)/
      security: false
    admin:
      anonymous: ~
      provider: adminusers
      pattern: ^/admin/
      form_login:
        check_path: /admin/login
        login_path: /admin/login
        default_target_path: adminDashboard
      logout:
        path: /admin/logout
        target: /admin/login
    main:
      anonymous: ~
      provider: chain_provider
      user_checker: App\Security\UserChecker
      json_login:
        check_path: /user/login
      logout:
        path: /user/logout
        target: /
      guard:
        authenticators:
          - App\Security\TokenAuthenticator



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

      # 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:
    - { path: ^/admin/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/admin, roles: ROLE_ADMIN }
  encoders:
    App\Entity\AdminUser:
      algorithm: bcrypt
      cost: 15

    App\Entity\User:
      algorithm: bcrypt
      cost: 15

为什么它在开发模式下运行良好但在生产模式下运行良好是没有意义的。

标签: phpsymfonysymfony4

解决方案


推荐阅读