Symfony的认证成功处理的问题 - httpUtils [英] Symfony authentication success handler issues - httpUtils

查看:193
本文介绍了Symfony的认证成功处理的问题 - httpUtils的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到这个错误:注意:未定义的属性:MyApp的\\ AuthBundle \\其它\\ AuthenticationSuccessHandler :: $ httpUtils

正如我在登录成功处理程序使用此code的结果是:

As a result of using this code in my login success handler:

public function onAuthenticationSuccess(Request $request, TokenInterface $token)
{
    return $this->httpUtils->createRedirectResponse($request, $this->determineTargetUrl($request));
}

我知道这是因为我没有在我的课的httpUtils属性。不过,我不知道如何注入呢?

I realise this is because I don't have an httpUtils property in my class. However I don't know how to inject it?

目前我只是在构造RouterInterface $路由器的EntityManager $他们。我是否需要添加更多的参数?或更新任何文件阳明海运?

Currently I just have RouterInterface $router and EntityManager $em in the constructor. Do I need to add more arguments? Or update any YML files?

推荐答案

您需要改变你的构造函数接受 security.http_utils 服务:

You need to change your constructor to accept the security.http_utils service:

class LoginSuccessHandler ...
{

    protected $httpUtils;

    ...

    public function __construct(HttpUtils $httpUtils, ...)
    {
        $this->httpUtils = $httpUtils;
        $this->router = $router;
        $this->security = $security;
    }

然后更新您的服务:

Then update your services:

login_success_handler:
    class:  AppBundle\EventListener\LoginSuccessHandler
    arguments:  ["@security.http_utils", ... ]
    tags:
      - { name: 'monolog.logger', channel: 'security' }

我也建议你扩展 DefaultAuthenticationSuccessHandler 这会给你的 determineTargetUrl 方法:

class LoginSuccessHandler extends DefaultAuthenticationSuccessHandler
{
    public function __construct(HttpUtils $httpUtils, ...)
    {
        parent::__construct($httpUtils);

        $this->router = $router;
        $this->security = $security;
    }

    // ...

}
或者,您可以通过它复制到你的类:

 } Or you can copy it over to your class:

/**
 * Builds the target URL according to the defined options.
 *
 * @param Request $request
 *
 * @return string
 */
protected function determineTargetUrl(Request $request)
{
    if ($this->options['always_use_default_target_path']) {
        return $this->options['default_target_path'];
    }

    if ($targetUrl = $request->get($this->options['target_path_parameter'], null, true)) {
        return $targetUrl;
    }

    if (null !== $this->providerKey && $targetUrl = $request->getSession()->get('_security.'.$this->providerKey.'.target_path')) {
        $request->getSession()->remove('_security.'.$this->providerKey.'.target_path');

        return $targetUrl;
    }

    if ($this->options['use_referer'] && ($targetUrl = $request->headers->get('Referer')) && $targetUrl !== $this->httpUtils->generateUri($request, $this->options['login_path'])) {
        return $targetUrl;
    }

    return $this->options['default_target_path'];
}

BTW。如果你想知道哪里http_utils服务是它的私人:

BTW. If you wondered where the http_utils service was its private:

$ php app/console container:debug --show-private | grep security.http_utils

这篇关于Symfony的认证成功处理的问题 - httpUtils的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆