Symfony 2 登出

6

这是我的第一个Symfony 2应用程序,我正在尝试登出当前登录的用户。

这是我的app/config/security.yml文件:

security:
encoders:
    Symfony\Component\Security\Core\User\User: plaintext

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

providers:
    in_memory:
        memory:
            users:
                user0:  { password: user0, roles: [ 'ROLE_ADMIN' ] }
                user1:  { password: user1, roles: [ 'ROLE_SUPER_ADMIN' ] }

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

    login:
        pattern:  ^/demo/secured/login$
        security: false

    secured_area:
        pattern: ^/
        logout: ~
        anonymous: ~
        http_basic:
            realm: "Secured Area"

access_control:
    - { path: ^/question/*, roles: ROLE_ADMIN }
    - { path: ^/questiongroup/*, roles: ROLE_ADMIN }
    - { path: ^/answer/*, roles: ROLE_ADMIN }
    - { path: ^/newslettertemplate/*, roles: ROLE_ADMIN }
    - { path: ^/customer/*, roles: ROLE_SUPER_ADMIN }
    - { path: ^/statistics/*, roles: ROLE_SUPER_ADMIN }

我已按照Symfony安全文档中的描述,在routing.yml中创建了注销登录的条目:

logout:
    path:   /logout

当我创建一个指向“注销”链接时,我确实被重定向到“/”,这是可以的。但用户仍然被认证,这意味着实际的注销操作没有生效。

3个回答

11

由于浏览器记住您的凭据并将其随每个请求发送,因此它不适用于HTTP基本身份验证。服务器端无法对此进行任何处理。

我相信最终您会转向基于表单的登录。当您这样做时,注销功能将按预期工作。


5

只需在security.yml文件中使用此代码:

logout:
      path:   /logout
      invalidate_session: false

0

如果像我一样,你是Symfony的新手,并且无法让其他注销解决方案起作用(我想我错过了一些配置细节),那么有一个非学术但功能性的解决方案:

当你使用基于表单的登录时,只需将未定义的loginpassword发送到“login_check”路由。

例如:login='*' password=''。

在模板中添加一个按钮:

<form action="{{ url('login_check') }}" method="post">
    <input type="text" name="_username" value="*" style="display:none;" />
    <input type="password" name="_password" style="display:none;" />
    <button type="submit">log out</button>
</form>

通过从控制器呈现“注销”模板:
<script>
    window.addEventListener("load",function(){
        document.getElementById("logout_form").submit();
    });
</script>
<form action="{{ url('login_check') }}" method="post" id="logout_form">
    <input type="text" name="_username" value="*" style="display:none;" />
    <input type="password" name="_password" style="display:none;" />
</form>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接