HWIOAuthBundle-FOSUserBundle-Symfony 2-登录facebook后重定向到自定义路径
反映了用户使用Facebook帐户登录后出现的以下问题:该问题被重定向到以下路径 如何将其重定向到此路由:HWIOAuthBundle-FOSUserBundle-Symfony 2-登录facebook后重定向到自定义路径,facebook,symfony,oauth,hwioauthbundle,Facebook,Symfony,Oauth,Hwioauthbundle,反映了用户使用Facebook帐户登录后出现的以下问题:该问题被重定向到以下路径 如何将其重定向到此路由:/或更多到此/# 在客户端,我使用主干。您只需将默认目标路径:/which/path/You/want添加到防火墙设置下的oauth部分 oauth: resource_owners: facebook: '/login/check-facebook' google: '/login/check-google' windows: '/l
/
或更多到此/#
在客户端,我使用主干。您只需将
默认目标路径:/which/path/You/want
添加到防火墙设置下的oauth
部分
oauth:
resource_owners:
facebook: '/login/check-facebook'
google: '/login/check-google'
windows: '/login/check-windows'
twitter: '/login/check-twitter'
login_path: /login
failure_path: /login
default_target_path: /whatever/path/you/want
看看使用javascript重定向。将以下内容添加到页面
<script>
// Handle facebook callback
if (window.location.hash && window.location.hash == '#_=_') {
window.location.hash = '';
}
</script>
//处理facebook回调
if(window.location.hash&&window.location.hash='#{
window.location.hash='';
}
根据@Prynz的想法,我们可以通过以下方式进一步创建“重定向到页面用户来源”:
1) 在防火墙中,注意删除以下行:
# security.yml
# ...
logout: true
logout:
path: /logout
target: /
因为我们将自己执行注销,以避免重定向到指定的目标
2) 将@Prynz的解决方案添加到security.yml(或config.yml,具体取决于您的实现)
3) 在路由中,在HWIO导入之前添加一个新的控制器(此处,LoginController
):
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);
}
}
如果需要,可以通过以下方式将用户重定向到当前页面:
添加您的config.yml
hwi_oauth.target_path_parameter: "target_path"
在您的视图中,使用以下内容附加URL:
&target_path=...
请记住,您可以使用当前路由名称
app.request.get('_route')
你得到这个问题的答案了吗?这很有启发。非常感谢。谢谢,很高兴知道这有帮助。要获得更完整的实现,可以查看和。享受吧!我做了一件不同的事情,在第27行的第一个文件中,在else
中,我添加了$this->get('session')->set('referer',$request->headers->get('referer')代码>因为我的登录页面包含指向我的OAuth连接路由的链接。因此,通过在这里设置referer,我得到了真正的referer,否则referer始终是我的OAuth用户的登录路径,因为他们总是通过/login
。我还将该文件中的第65行更改为这一行,因为我在(!strlen($referer)| preg_match(“/login$/”,$referer))
app.request.get('_route')