[英]How does symfony2 redirect to requested page after login
假設我的路由/booking/(.*)受security.yml中的防火牆配置保護,並且它需要“ ROLE_USER ”,當用戶嘗試訪問前面帶有 “ / booking / ”的任何路由時,app會將用戶重定向到登錄驗證頁面。
所以我的問題是,在用戶提供他的憑據並獲得身份驗證后,Symfony 2如何能夠將用戶重定向回用戶請求的頁面/路由,或者Symfony 2存儲該路由的位置是將其存儲在某個會話中還是別的地方。
我們可以訪問它嗎?
這是可能的,您可以在會話中訪問引用鏈接(如果use_referer
設置為true
,則使用該鏈接)。
舉例來說,如果你有一個success_handler
您的服務form_login
(在你的防火牆配置)重定向基於某些標准(通常角色)的用戶,但你想將用戶重定向到引薦鏈接,如果它被設置 ,你可以訪問引用鏈接像這樣:
$key = '_security.main.target_path'; #where "main" is your firewall name
//check if the referrer session key has been set
if ($this->container->get('session')->has($key)) {
//set the url based on the link they were trying to access before being authenticated
$url = $this->container->get('session')->get($key);
//remove the session key
$this->container->get('session')->remove($key);
}
//if the referrer key was never set, redirect to a default route
else{
$url = $this->router->generate('member_home');
}
return new RedirectResponse($url);
使用標頭獲取引用者(即$request->headers->get('referer')
)在這種情況下不起作用,因為它將始終返回登錄鏈接。
感謝Roman Marintsenko和Ryan Weaver的博客
Carrie Kendall的解決方案有效,謝謝!
這是Symfony中的完整實現:
services.yml:
login_handler:
class: Project\BaseBundle\Service\loginHandler
arguments: ['@router', '@doctrine.orm.entity_manager', '@service_container']
在Project \\ BaseBundle \\ Service \\ loginHandler中:
namespace Project\BaseBundle\Service;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
use Symfony\Component\Routing\RouterInterface;
use Doctrine\ORM\EntityManager;
class loginHandler implements AuthenticationSuccessHandlerInterface {
private $router;
private $container;
private static $key;
public function __construct(RouterInterface $router, EntityManager $em, $container) {
self::$key = '_security.secured_area.target_path';
$this->router = $router;
$this->em = $em;
$this->session = $container->get('session');
}
public function onAuthenticationSuccess( Request $request, TokenInterface $token ) {
$user_entity = $token->getUser();
if( !$user_entity->getChangePassword() ) {
$route = $this->router->generate('BaseBundle_home_page');
} else {
$this->session->getFlashBag()->add('error', 'Your password must be changed now');
$route = $this->router->generate('BaseBundle_account_page');
}
//check if the referer session key has been set
if ($this->session->has( self::$key )) {
//set the url based on the link they were trying to access before being authenticated
$route = $this->session->get( self::$key );
//remove the session key
$this->session->remove( self::$key );
//if the referer key was never set, redirect to a default route
} else{
$url = $this->generateUrl('BaseBundle_home_page');
return new RedirectResponse($route);
}
return new RedirectResponse($route);
}
}
我也有這個問題。 我使用了security.yml並在使用的注冊操作中:
$redirectUrl = $request->getSession()->get('_security.account.target_path');
請享用。
Symfony使用HTTP Referer標頭將用戶重定向回他們來自的頁面..即引用者
您可以使用security.yml中的安全配置use_referer: true
來設置它, 詳情請參見此處
您可以使用以下命令從控制器訪問referer標頭:
$referer = $request->headers->get('referer');
請注意標題拼寫錯誤,其引用(一個r)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.