【问题标题】:Symfony 2 LogoutSymfony 2 注销
【发布时间】:2013-09-18 11:07:05
【问题描述】:

这是我的第一个 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

当我创建指向“注销”的链接时,我确实会被重定向到“/”,这没关系。但是用户仍然是经过身份验证的,这意味着实际的注销不起作用。

【问题讨论】:

    标签: php symfony


    【解决方案1】:

    它不适用于 HTTP 基本身份验证,因为浏览器会记住您的凭据并在每个请求中发送它们。您在服务器端对此无能为力。

    我相信你最终会切换到form based login。注销功能将按照您应有的方式工作。

    【讨论】:

      【解决方案2】:

      只需在 security.yml 中使用它

      logout:
            path:   /logout
            invalidate_session: false
      

      【讨论】:

        【解决方案3】:

        如果你像我一样是 symfony 的新手并且无法让其他注销解决方案正常工作(我想我错过了一些配置 subtilities), 有一个非学术但实用的解决方案:

        当您使用 form based login 时,您只需将未定义的 loginpassword 发送到 'login_check' 路由。

        ex : 登录='*' 密码=''

        在模板中有一个按钮:

        <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>
        

        【讨论】:

          猜你喜欢
          • 2012-01-17
          • 1970-01-01
          • 1970-01-01
          • 2014-05-21
          • 2016-02-16
          • 1970-01-01
          • 2018-07-27
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多