AuthenticationSuccessHandler,登录后重定向和referrer

wal*_*nux 4 symfony fosuserbundle

我刚刚在这个论坛的帮助下添加了 AuthenticationSuccessHandler,当用户通过 fosuserbundle 或 fosfacebookbundle 登录时,它在我的网站上实现重定向。当用户是否完成配置文件时,重定向会发生变化,如果已完成,我希望将它们重定向到之前的位置,这就是我使用引用变量的原因。

    命名空间 Me\MyBundle\Handler;
    
    使用 Symfony\Component\Security\Http\HttpUtils;
    使用 Symfony\Component\HttpFoundation\RedirectResponse;
    使用 Symfony\Component\HttpFoundation\Request;
    使用 Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
    使用 Symfony\Component\Security\Http\Authentication\DefaultAuthenticationSuccessHandler;
    
    
    类 AuthenticationSuccessHandler 扩展 DefaultAuthenticationSuccessHandler {
    
        受保护的$router;
    
        公共函数 __construct( HttpUtils $httpUtils, 数组 $options, $router) {
        $这个->路由器=$路由器;
            父::__construct( $httpUtils, $options );
        }
    
        公共函数 onAuthenticationSuccess( 请求 $request, TokenInterface $token ) {
            
        $user = $token->getUser();
        
        if($user->getProfileComplete()!=1){
    //重定向到配置文件编辑
            $url = 'fos_user_profile_edit';
        }别的{
            $referer = $request->headers->get('referer');
            if($referer == NULL){
    //如果没有任何referer则重定向到自定义url
                $url = 'My_customurl';
            }别的{
    //重定向到引用者
                $url = $referer;
            }
        }
    
        return new RedirectResponse($this->router->generate($url));
        }
    }

my security.yml note that I have the use_referrer declared in both fos_facebook and form_login:

firewalls:
        main:
            pattern: ^/
            fos_facebook:
              app_url: "http://apps.facebook.com/app"
              server_url: "http://www.app.com"
              login_path: /user/login
              check_path: /facebook/login_check
              provider: fos_facebook_provider
              success_handler: my_auth_success_handler
              use_referer: true
            form_login:
                provider: fos_userbundle
                csrf_provider: form.csrf_provider
                login_path: /user/login
                check_path: /user/login_check
                use_forward: false
                success_handler: my_auth_success_handler
                use_referer: true
                failure_path: null
            logout:
                path: /user/logout
            anonymous: ~
            remember_me:
                  key:      mySuperKey
                  lifetime: 4147200
                  path:     /
                  domain:   ~

When I login and I don't have my profile completed, I am getting redirected successfully but when it is completed and referrer is not null I am having 500 errors:

当我使用 Facebook 登录时,出现 500 错误:“无法为指定路由“http://app.com/app_dev.php/user/login”生成 URL,因为此类路由不存在。” Firefox 中的网址是这样的:“http://app.com/app_dev.php/facebook/login_check”

当我使用表单登录时,我收到 500 错误:“无法为命名路由“http://app.com/app_dev.php/user/login”生成 URL,因为此类路由不存在。” Firefox 中的 url 是这样的:“http://app.com/app_dev.php/user/login_check”

任何想法,我都快疯了。我认为这是因为引用已存储用户/登录名,当它转到登录检查时再次转到用户/登录名。

Ziu*_*min 5

您正在尝试使用实际网址而不是路线名称生成路线。试试这个代码:

if ($user->getProfileComplete() != 1) {
    $url = $this->router->generate('fos_user_profile_edit');
} else {
    $referer = $request->headers->get('referer');
    if ($referer == NULL) {
        $url = $this->router->generate('My_customurl');
    } else {
        $url = $referer;
    }
}

return new RedirectResponse($url);
Run Code Online (Sandbox Code Playgroud)