HWIOAuthBundle - FOSUserBundle - Symfony 2 - 使用 facebook 登录后重定向到自定义路径

2024-03-09

用户使用Facebook账号登录后反映如下问题:即被重定向到以下路由/#_=_

我怎样才能将其重定向到此路由:/或者更多这个/# ?

在客户端,我使用骨干网。


采用@Prynz 的想法,我们可以进一步创建“重定向到用户来自的页面”:

1) 在防火墙中,请注意删除以下行:

# security.yml

# ...
logout: true
logout:
    path: /logout
    target: /

因为我们将自己实现注销以避免重定向到指定的target.

2)将@Prynz的解决方案添加到您的security.yml(或config.yml,具体取决于您的实现)

oauth:
    resource_owners:
        google: "/login/check-google"
        facebook: "/login/check-facebook"
        twitter: "/login/check-twitter"
        sensio_connect: "/login/check-sensio-connect"
    login_path: /login
    failure_path: /login
    default_target_path: /welcome # THIS LINE CONTRIBUTES TO THE MAGIC
    oauth_user_provider:
        service: app.oauth_user_provider

3)在你的路由中,添加一个新的控制器(这里,LoginController) HWIO 导入之前:

fuz_app_login:
    resource: "@FuzAppBundle/Controller/LoginController.php"
    type:     annotation
    prefix:   /

4)创建对应的控制器:

<?php

namespace Fuz\AppBundle\Controller;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;

class LoginController
{

    /**
     * @Route("/login", name="login")
     * @Method({"GET"})
     */
    public function loginAction(Request $request)
    {
        if ($this->getUser())
        {
            // already-logged user accessed /login
            return $this->redirect($request->headers->get('referer'));
        }
        else
        {
            // redirect to the login page
            return $this->forward('HWIOAuthBundle:Connect:connect');
        }
    }

    /**
     * @Route("/logout", name="logout")
     * @Method({"GET"})
     */
    public function logoutAction(Request $request)
    {
        // we do a manual logout just to redirect the user to where he comes from
        $this->container->get('security.context')->setToken(null);
        return $this->redirect($request->headers->get('referer'));
    }

    /**
     * @Route("/connect/{service}", name="connect")
     * @Method({"GET"})
     */
    public function connectAction(Request $request, $service)
    {
        // we overwrite this route to store user's referer in the session
        $this->get('session')->set('referer', $request->headers->get('referer'));
        return $this->forward('HWIOAuthBundle:Connect:redirectToService', array('service' => $service));
    }

    /**
     * @Route("/welcome", name="welcome")
     * @Method({"GET"})
     */
    public function welcomeAction()
    {
        // on login success, we're redirected to this route...
        // time to use the referer we previously stored.
        $referer = $this->get('session')->get('referer');
        if (is_null($referer))
        {
            return new RedirectResponse($this->generateUrl('home'));
        }
        return new RedirectResponse($referer);
    }

}

5)放松。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

HWIOAuthBundle - FOSUserBundle - Symfony 2 - 使用 facebook 登录后重定向到自定义路径 的相关文章

随机推荐