2013-03-20 3 views
0

내가 만료 세션이 로그 아웃됩니다하지만 난 구성 파일 security.yml을 남겨 세션의 여기세션이 만료되면 어떻게 강제로 사용자 로그 아웃을 할 수 있습니까?

namespace mio\mioBundle; 

use Symfony\Component\HttpFoundation\Response; 
use Symfony\Component\HttpFoundation\RedirectResponse; 
use Symfony\Component\HttpKernel\HttpKernelInterface; 
use Symfony\Component\HttpKernel\Event\GetResponseEvent; 
use Symfony\Bundle\FrameworkBundle\Routing\Router; 
use Symfony\Component\Routing\RouterInterface; 
use Symfony\Component\Security\Core\SecurityContext; 
use Symfony\Component\HttpFoundation\Request; 

class RequestListener{ 

    protected $router; 
    protected $security; 

    public function __construct(RouterInterface $router, SecurityContext $security) 
    { 
     $this->router = $router; 
     $this->security = $security; 
    } 

    public function onKernelRequest(GetResponseEvent $event) 
    { 
     echo $event->getRequest()->getSession()->('timeout'); 
    } 
} 

인사를하는 시간을 액세스 할 수없는 경우 사용자를 강제로 노력하고있어.

security: 

    firewalls: 
     frontend: 
      pattern: ^/ 
      anonymous: ~ 
      form_login: 
       login_path: /login 
       check_path: /login_check 
       default_target_path: /index 
       success_handler: authentication_handler 
      logout: 
       path: /logout 
       target: /login 
       success_handler: authentication_handler 
      security: true 
      remember_me: 
       key:  loksea 
       lifetime: 1800 
       path: /
      access_denied_handler: accessdenied_handler 
      #primero deben de ir los usuarios anonimos si no se entra en loop redirect 
    access_control: 
     - { path: /login, roles: IS_AUTHENTICATED_ANONYMOUSLY } 
     - { path: ^/pruebita, roles: IS_AUTHENTICATED_ANONYMOUSLY } 
     - { path: ^/js, roles: IS_AUTHENTICATED_ANONYMOUSLY } 
     - { path: ^/admin, roles: ROLE_A } 
     - { path: ^/nuevoinforme, roles: ROLE_M } 
     - { path: ^/, roles: IS_AUTHENTICATED_REMEMBERED } 

    providers: 
     user_db: 
      entity: { class: mio\mioBundle\Entity\Empleado, property: username } 
    role_hierarchy: 
     ROLE_M: ROLE_U 
     ROLE_A: ROLE_U 

    encoders: 
     mio\mioBundle\Entity\Empleado: { algorithm: sha1 } 
     Symfony\Component\Security\Core\User\User: plaintext 

세션이 끝나면 다시 로그인하라는 메시지가 표시되지만 사용자 로그 아웃은 필요하지 않습니다. 로그 아웃을 저장할 청취자가 있습니다.

public function onLogoutSuccess(Request $request){ 
     $empleado = $this->security->getToken()->getUser(); 
     $log = new Log(); 
     $log->setFechalog(new \DateTime('now')); 
     $log->setTipo("Salida"); 
     $log->setEmpleado($empleado); 
     $this->em->persist($log); 
     $this->em->flush(); 
} 

세션이 종료되면이 방법을 사용 하시겠습니까? 감사.

+0

아마도 [중복 질문] (http://stackoverflow.com/questions/15504525/symfony2-login-remember-me)입니다. –

+0

@ThomasPotaire 나는 그들에게 똑같은 해결책을 공유 할 수도 있지만 완전히 다른 질문을하고 싶습니다. –

답변

-2

security.yml 구성 파일에서이 동작을 구성해야하며 자동으로 작동해야합니다. 내가 옳다 경우

자보세요이

+0

당신은 정말 가난합니다. 예를 들어 구성을 포함 할 수 있습니까? – j0k

0

당신은 "onLogoutSuccess"사용자가 로그 아웃 할 때 당신의 방법을 실행해야합니까, 말해? 로그 아웃 프로세스가 제대로 작동합니다. 맞습니까?

명시 적으로 로그 아웃하려면 세션 객체의 "clear()"메소드를 시도해 보셨습니까?

0

나는 동일한 문제가 있었지만 사용자가 최대 유휴 시간에 도달했을 때 CredentialsExpiredException을 발생시키는 수신기를 생성 할 수있었습니다.
너무 오래 유휴 상태 인 사용자는 로그인/로그 아웃 페이지로 리디렉션됩니다 (상황에 따라 로그 아웃 대상을보고 "/ 로그인").
이것은 문제를 해결 한 방법입니다.

namespace mio\mioBundle; 

use Symfony\Component\HttpKernel\Event\GetResponseEvent; 
use Symfony\Component\DependencyInjection\Container; 
use Symfony\Component\Security\Core\Exception\CredentialsExpiredException; 

class RequestListener{ 

    protected $container; 

    public function __construct(Container $container) 
    { 
     $this->container = $container; 
    } 

    public function onKernelRequest(GetResponseEvent $event) 
    { 
     $session = $this->container->get('session'); 
     $maxTime = 5*60; //5 minutes is the maximum lifetime 

     // Get the current idle time and compare it with the max allowed time 
     if (time() - $session->getMetadataBag()->getLastUsed() > $maxTime) { 
      //Invalidate the current session and throw an exception 
      $session->invalidate(); 
      throw new CredentialsExpiredException(); 
     } 
    } 
} 

이 그것을해야합니다. 추가 질문이 있으면 알려 주시기 바랍니다.

+1

위의 코드에서 라우터와 보안이 필요한 이유는 무엇입니까? –

+0

죄송합니다 생성자를 올바른 코드로 변경하는 것을 잊었습니다. 헤즈 업에 감사드립니다. –

+0

Furhter 최적화는 생성자에서 $ container 대신 $ session을 설정합니다. –

관련 문제