当前位置: 首页 > 知识库问答 >
问题:

身份验证成功处理程序和登录后重定向

贝滨海
2023-03-14

我试图在登录symfony2应用程序后实现重定向,以便在我的用户是否有一个属性时重定向。我已在项目的Handler文件夹中创建了AuthenticationSuccessHandler.php类:


    namespace Me\MyBundle\Handler;

    use Symfony\Component\Security\Http\HttpUtils;
    use Symfony\Component\HttpFoundation\RedirectResponse;
    use Symfony\Component\HttpFoundation\Request;
    use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
    use Symfony\Component\Security\Http\Authentication\DefaultAuthenticationSuccessHandler;


    class AuthenticationSuccessHandler extends DefaultAuthenticationSuccessHandler {

        public function __construct( HttpUtils $httpUtils, array $options ) {
            parent::__construct( $httpUtils, $options );
        }

        public function onAuthenticationSuccess( Request $request, TokenInterface $token ) {


        $user = $token->getUser();

        if($user->getProfile()!=1){
            $url = 'fos_user_profile_edit';
        }else{
            $url = 'My_route';
        }

        return new RedirectResponse($this->router->generate($url));
        }
    }

但是当我登录时,我得到一个错误:

注意:未定义的属性:Me\MyBundle\Handler\AuthenticationSuccessHandler::$router in/var/www/MyBundle/src/Me/MyBundle/Handler/AuthenticationSuccessHandler.php第28行

错误发生在"返回新的重定向响应($this-

我也有我的服务:



    my_auth_success_handler:
            class: Me\MyBundle\Handler\AuthenticationSuccessHandler
            public: false
            arguments: [ @security.http_utils, [] ]

以及security.yml中的成功处理程序



    fos_facebook:
            success_handler: my_auth_success_handler

有什么想法吗?非常感谢你。


共有2个答案

韶景曜
2023-03-14

与Symfony

use Symfony\Component\Routing\Router; 
use Symfony\Component\Security\Http\HttpUtils;

class AuthenticationSuccessHandler extends DefaultAuthenticationSuccessHandler 
{

    /**
     * @var Router
     */
    protected $router;

    public function __construct(HttpUtils $httpUtils, array $options = [], Router $router) 
    {
        parent::__construct($httpUtils, $options);
        $this->router = $router;
    }

请注意,默认值“$选项 = []" 对AutowirePass很重要:如果没有它,将引发异常。但是你有一个空数组。

into services.yml:

 my_auth_success_handler:
        class: Me\MyBundle\Handler\AuthenticationSuccessHandler
        public: false
        autowire: true

无需在此处指定参数;-)

羊舌涵涤
2023-03-14

您没有@路由器服务。修改构造函数

protected $router;
public function __construct( HttpUtils $httpUtils, array $options, $router ) {
    $this->router = $router;
    parent::__construct( $httpUtils, $options );
}

和服务定义:

...
arguments: [ @security.http_utils, [], @router ]
 类似资料:
  • 我刚刚在这个论坛的帮助下添加了一个AuthenticationSuccessHandler,当用户通过fosuserbundle或fosfacebookbundle登录时,它在我的站点上实现了重定向。当用户完成或未完成配置文件时,重定向会发生变化,如果已经完成,我希望将它们重定向到以前的位置,这就是我使用referer变量的原因。 security.yml,我已在fos_facebook和form

  • Flatter Web(Navigator 2.0/Router API):如何处理经过身份验证的路由及其成功身份验证后的重定向? e、 g.我的系统中有这种路由 若用户直接打开这个URL,我想让用户先登录,那个么路由将被重定向到。。 一旦登录,我想用户导航以前打开的网址,如果有任何其他主页。。 似乎这种事情可能会有所帮助,任何想法——我们如何才能实现类似的事情?https://stackover

  • 在从主机推送docker映像(成功登录后)时,我得到了“未经授权:需要身份验证”。 详情如下。 Docker版本:1.9.1(客户端和服务器) /var/log/messages显示403,我不知道这个docker。见下文。 非常感谢您的帮助,如果您需要更多信息,请告诉我。我也用-f推了推。不走运!

  • 我使用Laravel 5.2。我想问一个问题。如何更改重定向登录表单在laravel当我们已经做登录?我使用的是来自Php artisan make: auth的auth。 我已经登录并重定向到localhost:8000/admin 然后我再次打开localhost:8000/登录。它将重定向到“/”或localhost:8000/ 我的问题是如何将“/”更改为“/”管理员“?因此,即使我已经登

  • 使用,https://github.com/firebase/FirebaseUI-Android/tree/master/codelabs/chat作为登录的参考,我在键入时似乎遇到了问题 我只能键入Auth的提供者,而不能键入,为什么会这样,它提示我键入社会提供者。

  • 我使用Spring Security 4.2.1来认证一个SpringMVC webapp。我有一个非常难以捉摸的问题,似乎有时工作,有时被打破(打破比它的工作)。 问题如下: > < li> 在尝试登录时,AuthenticationProvider正确地验证了用户,并且我在日志中看到了验证成功(并且经过验证的用户具有所需的所有权限,等等,我可以在日志中看到它们)。 然后,框架似乎为用户清除了S