从Symfony2中的AuthenticationHandler设置flashMessage

雷尼埃

我遇到FOSUserBundle的问题,因为任何时候我使用错误的凭据登录时都会得到完整的stacktrace作为错误消息:

错误!/ var / www / html / vendor / symfony / symfony / src / Symfony / Component / Security / Core / Authentication / Provider / UserAuthenticationProvider中带有消息“错误凭据”的异常“ Symfony \ Component \ Security \ Core \ Exception \ BadCredentialsException”。的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

    防火墙:主要:模式:^ / form_login:提供者:fos_userbundle csrf_provider:form.csrf_provider login_path:/ login check_path:/ login_check success_handler:authentication_handler failure_handler:authentication_handler

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

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

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

为什么?我检查getSession()方法,并将其HttpFoundation包含在use语句中,所以我在这里做错了什么?

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

唐·卡里斯托

$request->getSession()->setFlash('error', 'error'); 是“旧” symfony版本的一种表示法。

现在您应该使用

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

此外,您确定显示$exception短信是一种好习惯吗?我的意思是,我不知道如果用户或什至是管理员,该Flash不会显示在哪个页面上-当然,对于一般的PHP和编程知识也不了解-可以看到tha页面,因此消息,您应该尝试记录该消息以达到目的,但向用户显示其他内容。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在Symfony2 Doctrine2中将varchar设置为int类型?

来自分类Dev

在Symfony2中的表单值转换器上设置默认值

来自分类Dev

为Symfony2中的实体类型设置默认值

来自分类Dev

Symfony2中的链接

来自分类Dev

使用Symfony2中的内存提供程序设置显示名称

来自分类Dev

Symfony2如何在选择实体字段中设置选项值

来自分类Dev

如何在Symfony2中为所有控制器设置会话变量?

来自分类Dev

Set flashMessage from AuthenticationHandler in Symfony2

来自分类Dev

如何在Symfony2中设置表单名称?

来自分类Dev

Symfony2更新数组中的行

来自分类Dev

如何在symfony2中设置创建和更新日期?

来自分类Dev

为symfony2中的特定控制器设置max_execution_time

来自分类Dev

如何使用symfony2在Twig中设置全局变量

来自分类Dev

在Symfony2表单输入中设置带有翻译的占位符属性

来自分类Dev

如何在Symfony2命令中设置退出状态代码?

来自分类Dev

Symfony2:在PHP中设置路由类型

来自分类Dev

使用Symfony2中的内存提供程序设置显示名称

来自分类Dev

Symfony2和Doctrine2-设置为onDelete

来自分类Dev

如何在Symfony2中动态设置参数?

来自分类Dev

在symfony2中将orm配置设置为类似于Sylius

来自分类Dev

从Symfony2中的AuthenticationHandler设置flashMessage

来自分类Dev

symfony2中的Oracle数据库错误(原则)。parameters.yml设置正确吗?

来自分类Dev

如何在Symfony2中为复选框设置样式?

来自分类Dev

Symfony2中的配置

来自分类Dev

Symfony2中的继承

来自分类Dev

symfony2中的会话

来自分类Dev

Nginx + Symfony2 + HHVM的Vhost设置

来自分类Dev

可在Symfony2中的自定义字段类型上设置时间戳记

来自分类Dev

Symfony2 - 设置 ChoiceType 的标签

Related 相关文章

热门标签

归档