繁体   English   中英

Symfony2如何使所有会话属性无效

[英]Symfony2 how to invalidate all the session attributes

在我的Symfony2项目中,我有一个logout按钮,该按钮会重定向到索引页面,但是当我单击“ Login按钮时,它会直接连接而无需询问userpassword

我如何验证所有session attributes ,所以如果我再次login ,它应该询问我userpassword

这是我的logout操作:

public function logoutAction(Request $request)
    {    
        $this->get('security.context')->setToken(null);
        $this->get('request')->getSession()->invalidate();

        $url = $this->generateUrl('my_route');
        $response = new RedirectResponse($url);
        $response->headers->clearCookie('PHPSESSID');
        $response->send();
        return $response;
    }   

这是security.yml

security:

    encoders:
        Envivio\UserBundle\Entity\User: sha512

    role_hierarchy:
        ROLE_USER:        ROLE_USER
        ROLE_ADMIN:       ROLE_USER
        ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH ]

    providers:        
        mine:
            id: ib_user.oauth_user_provider

    firewalls:
        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false        

        main:
            anonymous: true
            pattern: ^/
            oauth:
                resource_owners:
                    salesforce: "/login/check-salesforce"
                login_path:   /login
                #use_forward:  false
                failure_path: /login
                default_target_path: /upload
                oauth_user_provider:
                    service: ib_user.oauth_user_provider                
            remember_me:
                key:     "%secret%"


    access_control:
        - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }       
        - { path: ^/admin, role: ROLE_ADMIN }

你可以试试 :

$this->get('session')->clear();

要么

$session = $this->get('session');
$ses_vars = $session->all();
foreach ($var as $key => $value) {
    $session->remove($key);
}

这也可能是cookie的问题,请在response-> headers中检查您的cookie,然后尝试使用该函数清除它:

http://api.symfony.com/2.3/Symfony/Component/HttpFoundation/ResponseHeaderBag.html#method_clearCookie

编辑:

由于您已经启用了我的记忆,因此您也需要清除该cookie,否则您下一个请求将重新验证用户身份,因此可以为cookie指定一个名称:

  #security.yml

    remember_me:
             name: "%session.remember_me.name%"

在parameters.yml中添加cookie的名称

parameters:
    session.remember_me.name: EXTSESS

然后在您的控制器注销操作中:

$response = $this->redirectToRoute('homepageroute'); //redirect prepare the route to redirect
$this->get('security.context')->setToken(null);
$this->get('request')->getSession()->invalidate();
$remember_sess_name = $this->container->getParameter('session.remember_me.name');

$response->headers->clearCookie($remember_sess_name);

return $response;

它现在应该可以工作。 如果不是,那么ALT + F4:D

您不需要手动进行。 Symfony2防火墙可以执行以下注销操作:

 # app/config/security.yml security: firewalls: secured_area: # ... logout: path: /logout target: / # ... 

接下来,您需要为此URL创建一个路由(而不是控制器):

 # app/config/routing.yml logout: path: /logout 

就是这样! 通过将用户发送到/ logout(或将路径配置为任意路径),Symfony将取消对当前用户的身份验证。

一旦用户注销,他们将被重定向到上面的目标参数定义的任何路径(例如,主页)。

链接到相关文档: http : //symfony.com/doc/current/book/security.html#logout

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM