【问题标题】:Set flashMessage from AuthenticationHandler in Symfony2在 Symfony2 中从 AuthenticationHandler 设置 flashMessage
【发布时间】:2014-10-03 05:33:51
【问题描述】:

我在使用 FOSUserBundle 时遇到问题,因为每当我使用错误的凭据登录时,我都会收到完整的堆栈跟踪作为错误消息:

错误!例外 'Symfony\Component\Security\Core\Exception\BadCredentialsException' 带有消息“错误凭据” /var/www/html/vendor/symfony/symfony/src/Symfony/Component/Security/Core/Authentication/Provider/UserAuthenticationProvider.php:90

这对我来说很丑,所以对用户来说也很丑。所以我正在考虑两种解决方案:更改为我正在处理的 AJAX 登录,但它不起作用或者我做错了什么(将在下面解释)并找到了一种方法来更改那个丑陋的消息(我没有得到这是一个,所以任何建议都会有所帮助)。

现在关于第一个解决方案,这就是我所做的:

  • 实现AuthenticationHandler:

    <?php
    
    namespace UsuarioBundle\Handler;
    
    use Symfony\Component\HttpFoundation\Response;
    use Symfony\Component\HttpFoundation\RedirectResponse;
    use Symfony\Bundle\FrameworkBundle\Routing\Router;
    use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
    use Symfony\Component\HttpFoundation\Request;
    use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
    use Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface;
    use Symfony\Component\Security\Core\Exception\AuthenticationException;
    
    class AuthenticationHandler
        implements AuthenticationSuccessHandlerInterface,
        AuthenticationFailureHandlerInterface
    {
        private $router;
    
        public function __construct(Router $router)
        {
            $this->router = $router;
        }
    
        public function onAuthenticationSuccess(Request $request, TokenInterface $token)
        {
            if ($request->isXmlHttpRequest()) {
                // do I need something here?
            } else {
                // If the user tried to access a protected resource and was forces to login
                // redirect him back to that resource
                if ($targetPath = $request->getSession()->get('_security.target_path')) {
                    $url = $targetPath;
                } else {
                    // Otherwise, redirect him to wherever you want
                    $url = $this->router->generate('user_view', array('nickname' => $token->getUser()->getNickname()));
                }
    
                return new RedirectResponse($url);
            }
        }
    
        public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
        {
            if ($request->isXmlHttpRequest()) {
                // Handle XHR here
            } else {
                // Create a flash message with the authentication error message
                $request->getSession()->setFlash('error', $exception->getMessage());
                $url = $this->router->generate('user_login');
    
                return new RedirectResponse($url);
            }
        }
    }
    
  • 定义服务并注册处理程序:

    parameters:
        vendor_security.authentication_handler: UsuarioBundle\Handler\AuthenticationHandler
    
    services:
        authentication_handler:
            class:  %vendor_security.authentication_handler%
            arguments:  [@router]
            tags:
                - { name: 'monolog.logger', channel: 'security' }
    
  • 更改security.yml中的引用:

    防火墙: 主要的: 模式:^/ 表单登录: 提供者:fos_userbundle csrf_provider:form.csrf_provider 登录路径:/登录 check_path: /login_check 成功处理程序:身份验证处理程序 failure_handler: authentication_handler

          logout:
               path: fos_user_security_logout
               target: /
               invalidate_session: false
          anonymous: ~
    

但是当我尝试使用无效凭据登录时出现此错误:

试图在类上调用方法“setFlash” “Symfony\Component\HttpFoundation\Session\Session”在 /var/www/html/src/UsuarioBundle/Handler/AuthenticationHandler.php 行 52.

为什么?我检查了getSession() 方法,它是HttpFoundation 的一部分,我包含在use 语句中,所以我在这里做错了什么?

注意:我主要从this 主题中获取代码,因此我对此仍有一些疑问。

【问题讨论】:

  • 我认为你必须使用 session->getFlashBag()->add() 而不是 setFlash。

标签: php ajax symfony login fosuserbundle


【解决方案1】:

$request-&gt;getSession()-&gt;setFlash('error', 'error'); 是“旧”symfony 版本的符号。

现在你应该使用

$reqest->getSession()->getFlashBag()->add('error', $exception->getMessage());

此外,您确定显示$exception 短信是一个好习惯吗?我的意思是,我不知道这个 flash 将显示在哪个页面,但是如果用户,甚至是管理员——当然对 PHP 和一般编程一无所知——可以看到这个页面,所以消息,您应该尝试为您的目的记录该消息,但向用户显示其他内容。

【讨论】:

  • 不应使用$this-&gt;get('session'),因为容器尚未注入处理程序。
  • @Qoop:你说得对:我处于“自动驾驶”应答模式,并没有注意到代码的 sn-p 不是控制器
  • 呼……我现在又可以放松了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-19
  • 2013-09-18
  • 2011-06-16
  • 1970-01-01
  • 2011-09-16
相关资源
最近更新 更多