在hwioauthbundle中重定向到请求页面

时间:2014-08-01 14:32:31

标签: php symfony redirect fosuserbundle hwioauthbundle

假设我有一个页面http://example.com/page1

我在这里登录了facebook链接。

另外在另一页,第2页,我已经在facebook上设置了登录链接。

我想要的是,点击第1页上的facebook链接登录后,它应该重定向到page1,然后点击第2页的登录链接,它应该重定向到Page2。

现在,它总是重定向到'example.com /'。

我在Symfony2中使用Hwioauthbundle与fosuserbundle集成

1 个答案:

答案 0 :(得分:7)

1)创建身份验证处理程序

<?php

namespace Company\Bundle\Handler;

use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
use Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Router;

class SecurityHandler implements AuthenticationSuccessHandlerInterface, AuthenticationFailureHandlerInterface
{

    private $router;

    public function __construct(Router $router)
    {
        $this->router = $router;
    }

    public function onAuthenticationSuccess(Request $request, TokenInterface $token)
    {
        $referer = $request->headers->get('referer');
        if (empty($referer)) {
            return new RedirectResponse($this->router->generate('homepage'));
        } else {
            return new RedirectResponse($referer);
        }
    }

    public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
    {
        // Edit it to meet your requeriments
        $request->getSession()->set('login_error', $error);
        return new \Symfony\Component\HttpFoundation\RedirectResponse($this->router->generate('login_route'));
    }

}

2)将其注册为服务

# src/Company/Bundle/Resources/config/services.yml
security_handler:
    class: Company\Bundle\Handler\SecurityHandler
    arguments:  [@router]

3)配置HWIO以将此服务用作处理程序

# app/config/security.yml
firewalls:
# ....
you_firewall:
    oauth:
        # ....
        resource_owners:
            # ....
        success_handler:    security_handler
        failure_handler:    security_handler # optional