Php symfony@Security和redirect\u url

Php symfony@Security和redirect\u url,php,symfony,annotations,fosuserbundle,Php,Symfony,Annotations,Fosuserbundle,可以使用 /** * @Security("has_role('ROLE_USER')") */ public function indexAction() { // ... } 并定义登录后重定向的重定向url 这是保安部 firewalls: main: pattern: ^/ form_login: target_path_p

可以使用

    /**
     * @Security("has_role('ROLE_USER')")
     */
    public function indexAction()
    {
        // ...
    }
并定义登录后重定向的重定向url

这是保安部

firewalls:
        main:
            pattern: ^/
            form_login:
                target_path_parameter: redirect_url
我不能使用referer,因为它是ajax请求

所以现在我用

if(!$user = $this->getUser()) {
            return $this->redirect($this->generateUrl('fos_user_security_login', array(
                'redirect_url' => $request->server->get('HTTP_REFERER')
            )));
        }

是否可以在安全批注中执行此操作?

您可以定义登录成功处理程序:

form_login:
    success_handler: some.service.id
创建一个实现
\Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface
的类,并使
onAuthenticationSuccess()
方法将
\Symfony\Component\HttpFoundation\RedirectResponse
返回到所需的任何URL

通过这种方式,您可以拥有任何自定义逻辑来确定所需的目标URL

例如,您可以使用
\Symfony\Component\HttpFoundation\Request::isXmlHttpRequest()
方法来确定它是否是Ajax请求,并相应地采取行动