29

ログインの成功には、パラメーターがありますuse_referer: true。ログインの失敗には しかfailure_pathありませんが、これは私が探しているものではありません。

2番目のこと:それを実行してエラーメッセージを渡す方法は?

3 つ目: ログアウト後にリファラーに戻る方法は?

4

1 に答える 1

66

私はそれを解決しました。

解決策があります: Symfony 2 で login_check 後にリダイレクトを無効にする方法

ここに私の問題を解決するコードがあります:

<?php

namespace Acme\MainBundle\Handler;

use Symfony\Component\Security\Http\Logout\LogoutSuccessHandlerInterface;
use Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;

class AuthenticationHandler implements AuthenticationFailureHandlerInterface, LogoutSuccessHandlerInterface
{
    public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
    {       
        $referer = $request->headers->get('referer');       
        $request->getSession()->setFlash('error', $exception->getMessage());

        return new RedirectResponse($referer);
    }

    public function onLogoutSuccess(Request $request) 
    {
        $referer = $request->headers->get('referer');
        return new RedirectResponse($referer);
    }
}

イベントを処理するには、次のように security.yml に追加します。

form_login:
    check_path: /access/login_check
    login_path: /
    use_referer: true
    failure_handler: authentication_handler  
logout:
    path:   /access/logout
    target: /
    success_handler: authentication_handler 

そして config.yml に:

services:
    authentication_handler:
        class: Acme\MainBundle\Handler\AuthenticationHandler
于 2012-01-30T14:44:30.563 に答える