【问题标题】:Symfony 5 : Authentication failed in prod envSymfony 5:产品环境中的身份验证失败
【发布时间】:2021-11-22 09:57:07
【问题描述】:

我有一个应用程序正在尝试上传到我的 FTP 服务器。

我跑过:symfony console make:usersymfony console make:auth 因此,当使用symfony serve 在本地运行服务器时,我可以使用 symfony 自动创建的文件登录没有问题。 我没有更改任何内容,但是当我将项目上传到我的 ftp(使用 Cyber​​duck 拖放)时,它不允许我登录,即使我使用了错误的凭据也不会显示错误..它只是刷新页面.. 我不知道它是否对这个问题有任何影响,但我也在上传之前运行了composer require symfony/apache-pack,但即使使用这个require,我的登录仍然在本地工作

在我的 Authenticator 类中(由make:auth 命令在 src/Security/ 文件夹中生成的文件)我更改了 authenticate() 函数以添加一个 dd() 只是为了查看它是否被调用,结果是:

  • 本地:调用 dd()
  • 在我的 FTP 中:dd() 不是..

这是我完整的 Authenticator 类:

<?php

namespace App\Security;

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\Security;
use Symfony\Component\Security\Http\Authenticator\AbstractLoginFormAuthenticator;
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\CsrfTokenBadge;
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
use Symfony\Component\Security\Http\Authenticator\Passport\Credentials\PasswordCredentials;
use Symfony\Component\Security\Http\Authenticator\Passport\Passport;
use Symfony\Component\Security\Http\Authenticator\Passport\PassportInterface;
use Symfony\Component\Security\Http\Util\TargetPathTrait;

class Authenticator extends AbstractLoginFormAuthenticator
{
    use TargetPathTrait;

    public const LOGIN_ROUTE = 'app_login';

    private $urlGenerator;

    public function __construct(UrlGeneratorInterface $urlGenerator)
    {
        $this->urlGenerator = $urlGenerator;
    }

    public function authenticate(Request $request): PassportInterface
    {
        $username = $request->request->get('username', '');
        $request->getSession()->set(Security::LAST_USERNAME, $username);
        $passeport = new Passport(
            new UserBadge($username),
            new PasswordCredentials($request->request->get('password', '')),
            [
                new CsrfTokenBadge('authenticate', $request->get('_csrf_token')),
            ]
        );
        dd($passeport);
        return $passeport;
    }

    public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): ?Response
    {
        if ($targetPath = $this->getTargetPath($request->getSession(), $firewallName)) {
            return new RedirectResponse($targetPath);
        }

        // For example:
        return new RedirectResponse($this->urlGenerator->generate('dashboard'));
        throw new \Exception('TODO: provide a valid redirect inside '.__FILE__);
    }

    protected function getLoginUrl(Request $request): string
    {
        return $this->urlGenerator->generate(self::LOGIN_ROUTE);
    }
}

如果您需要我项目的任何其他部分,请告诉我, 谢谢!

【问题讨论】:

  • 新代码可能无法执行,请检查您的服务器是否使用 OpCache。简单的方法是将opcache_reset(); 作为public/index.php 的第一行。不过不要把它放在那里,只是为了检查。如果你把它留在里面,会严重降低性能。

标签: php symfony authentication symfony5


【解决方案1】:

这可能是缓存问题,您有两种不同的解决方案,删除var/cache/prod 文件夹并运行composer install

但清除方法是使用为它创建的命令清除缓存php bin/console cache:clear

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-24
    • 1970-01-01
    • 2017-11-27
    相关资源
    最近更新 更多