用户在使用Symfony框架的生产服务器上登录失败(身份验证请求无法处理,由于…)


Failed user login on production server using Symfony framework (Authentication request could not be processed due to...)

我正在使用Symfony的一个项目,我一直试图让登录工作在生产服务器没有成功的过去2天。我一直得到错误

由于系统问题,无法处理身份验证请求。

我已经按照这里的指南(http://symfony.com/doc/current/cookbook/security/entity_provider.html)设置从数据库加载用户。

我的>安全。yml 文件:

security:
encoders:
    Symfony'Component'Security'Core'User'User: plaintext
    Acceptme'UserBundle'Entity'User: plaintext
role_hierarchy:
    ROLE_SUPER_ADMIN:   [ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]
providers:
    in_memory:
        memory:
            users:
                patricia:
                    password: patricia
                    roles: 'ROLE_ADMIN'
    users:
        name: user_provider
        entity: { class: AcceptmeUserBundle:User, property: username }
firewalls:
    user_area:
        pattern: ^/
        anonymous: ~
        provider: user_provider
        form_login:
            login_path: login_route
            check_path: _login_check
            default_target_path: homepage
    dev:
        pattern: ^/(_(profiler|wdt|error)|css|images|js)/
        security: false
    default:
                anonymous: ~
                http_basic: ~
access_control:
        - { path: ^/admin, roles: ROLE_ADMIN }
我的<

strong> SecurityController.php:

namespace AppBundle'Controller;
use Symfony'Component'HttpFoundation'Request;
use Sensio'Bundle'FrameworkExtraBundle'Configuration'Route;
use Symfony'Bundle'FrameworkBundle'Controller'Controller;
use Sensio'Bundle'FrameworkExtraBundle'Configuration'Template;
use Sensio'Bundle'FrameworkExtraBundle'Configuration'Security;
use Symfony'Component'Security'Core'SecurityContext;
class SecurityController extends Controller
{
/**
 * @Route("/login", name="login_route")
 * @Template("security/login.html.twig")
 */
public function loginAction(Request $request)
{
    if ($request->attributes->has(SecurityContext::AUTHENTICATION_ERROR)) {
        $error = $request->attributes->get(SecurityContext::AUTHENTICATION_ERROR);
    } else {
        $error = $request->getSession()->get(SecurityContext::AUTHENTICATION_ERROR);
    }
    return array(
        'last_username' => $request->getSession()->get(SecurityContext::LAST_USERNAME),
        'error'         => $error,
    );
}
/**
 * @Route("/login_check", name="_login_check")
 */
public function securityCheckAction()
{
    // this controller will not be executed,
    // as the route is handled by the Security system
}
}

我试过在2个不同的网络主机上上传项目(FatCow &GoDaddy),但问题依然存在。本地我使用PHP 5.4.19 (FatCow使用5.3.2和GoDaddy使用5.4.37)。请记住,当使用XAMPP在本地主机上工作时,一切都很好!

我已经确认在这两种情况下都启用了PDO。我已经确认了参数中的数据库用户名、密码和主机是正确的。yml 文件。本地和远程服务器的错误日志都没有显示任何内容。

我已经按照之前的帖子的所有方向部署Symfony2应用程序,得到fosuserbundle错误,仍然没有成功。

看起来错误:

由于系统问题,无法处理身份验证请求。

太一般了,没有告诉任何问题在哪里(这里有一个关于这个问题的问题)。

我通过检查日志来解决我的问题,看看发生了什么(在var/logs/dev.log),希望这能帮助别人。

在我的例子中,parameters中有一个错误的参数。Yml关于数据库连接。但是,同样,这个错误太普遍了,并不一定意味着问题与数据库连接有关。

这个问题可以通过php bin/console cache:clear --env=prod --no-debug

UPDATE: Issue solved。问题是实体php文件中的一个表用大写字母命名,而数据库表用小写字母命名。+1给cl mentbertillon指出了正确的方向,即prod.log

@ShinDarth提到它。它太通用了,日志检查将帮助我们的案例中的人们通过它。

如果它能帮助我的情况,那就是:

在SF3中安装SonataUserBundle之后,我必须

bin/console doctrine:schema:update --force

我的上下文是特殊的,我已经安装和使用FOSUserBundle之前安装SonataUserBundle。(由于SF3兼容FOSUser/SonataUSer…在那之后,数据库已经进行了16次查询。伟大的工作。

您可能使用了Symfony docs给出的模板:

{% if error %}
    <div class="alert alert-danger">{{ error.messageKey|trans(error.messageData, 'security') }}</div>
{% endif %}

实际上给你这个错误消息。解决此问题的最简单可靠的方法是将这一行替换为以下内容:

<div class="alert alert-danger">{{ error }}</div>

将为您提供错误的完整堆栈跟踪,并(希望)帮助您调试应用程序。不要忘记在投入生产之前对其进行还原!

在我的例子中,我更改了用户实体,然后我忘记更新表。

:

php bin/console doctrine:schema:update --force

目前Symfony和生产IF在认证系统发生错误(缺少表,缺少列或任何其他异常)时存在一个错误-它被记录为INFO而不是error,并且默认的错误记录选项根本不记录。

https://github.com/symfony/symfony/pull/28462

我认为现在有两个选择-临时记录生产中的所有内容(包括INFO),直到您找到真正的错误。

第二个选项:使用这个补丁或直接在生产中调试。

我肯定这个错误太普遍了。在我的例子中,下面是不正确的:

class: App/Entity/User;

更正:

class: App'Entity'User;

这个解决方案对我来说是正确的:https://stackoverflow.com/a/39782535/2400373

如果没有终端,可以直接进入服务器,删除var/cache下的文件夹。

如果您可以访问控制台,另一个解决方案是输入

#rm -rf var/cache/*

$sudo rm -rf var/cache/*

此解决方案适用于symfony 3

在我的情况下,通过纠正.env文件中连接详细信息中的错字来修复该问题。

另一个可能的原因是MySQL Server。在我的情况下,我忘记启动MAMP/MySQL服务器和Symfony导致这个消息。

这是Symfony 5.3上的一个只发生在Internet服务器上的错误。在我的本地服务器上,没有问题。尝试降级到4.5或5.0。然后重试验证。在我的例子中,无法创建用于验证用户身份的令牌,也无法对数据库进行查询以验证用户密码是否正确。

根据gogaz给出的答案,Symfony的安全包似乎默认情况下不会记录这些错误,因为(我猜)它假定它们将是身份验证问题。

在操作中注入一个记录器并(有条件地)记录错误可能是值得的。

public function loginAction(AuthenticationUtils $authenticationUtils, LoggerInterface $logger): Response
{
    if (($error = $authenticationUtils->getLastAuthenticationError()) && ($error instanceof AuthenticationServiceException)) {
        $logger->critical($error->getMessage(), $error->getTrace());
    }
    ...
}

请注意,如果您的安全实现可以抛出您可能想要记录的其他错误类型,则可能必须扩展日志记录。