Symfony PHP 退出登录不起作用

3
我有一个在另一个项目中运行良好的注销功能,但出于某种原因,在我目前正在处理的项目中无法正常工作。看起来它只是刷新页面。我查看了Symfony的官方文档https://symfony.com/doc/current/security.html,但没有找到解决方法。希望你们能帮助我。
更新:Security.yml:
# To get started with security, check out the documentation:
# https://symfony.com/doc/current/security.html
security:
    providers:
        in_memory:
            memory:
                users:
                    beheerder:
                        password: admin
                        roles: 'ROLE_BEHEERDER'

    access_control:
        - { path: '^/beheerder/*', roles: [ROLE_BEHEERDER] }

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


    firewalls:
        # disables authentication for assets and the profiler, adapt it according to your needs
        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false

        main:
            anonymous: 
            # activate different ways to authenticate

            # https://symfony.com/doc/current/security.html#a-configuring-how-your-users-will-authenticate
            http_basic: ~

            # https://symfony.com/doc/current/security/form_login_setup.html
            #form_login: ~
            logout:
                path: security_logout
                target: /

控制器:
<?php

namespace AppBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\ExpressionLanguage\Expression;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;


class DefaultController extends Controller
{

    //Functie om naar de homepagina te gaan met een redirect naar de homepagina van de gebruiker.

    /**
     * @Route("/", name="homepage")
     */
    public function indexAction(Request $request, AuthorizationCheckerInterface $authorizationChecker)
    {
        if ($authorizationChecker->isGranted(new Expression('"ROLE_BEHEERDER" in roles')))
        {
            return $this->redirectToRoute('beheerder');
        }
        else
        {
            return $this->render('default/index.html.twig');
        }
    }

    /**
     * @Route("/beheerder", name="beheerder")
     */
    public function beheerder(Request $request)
    {
        return new Response($this->renderView('beheerder/index.html.twig'));
    }

    /**
     * @Route("/logout", name="security_logout")
     */
    public function logoutAction(Request $request)
    {
        return new Response($this->renderView('logout.html.twig'), 401);
    }


}

注销Twig:
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8" />
        <title>{% block title %}Overzicht{% endblock %}</title>
        <link rel="icon" type="image/x-icon" href="{{ asset('favicon.ico') }}" />
    </head>
    <body>
        <p>Redirecting back....</p>
        <script>
            document.cookie = 'PHPSESSID=; Path=/; expires=Thu, 01 Jan 1970 00:00:01 GMT;';
            window.location.href = '{{ url('homepage') }}';
        </script>
    </body>
</html>

编辑:我正在使用Symfony 3.4。当我转到/logout页面时,它似乎只是刷新页面。我可以看到它进入了注销函数,但用户不会被注销。

请提供更多细节,包括Symfony版本以及在登出过程中发生了什么。 - user6830821
@MohammadTrabelsi 修改了问题。 - undefined
logout:被定义了两次。你应该删除logout: true - undefined
@A.L 已经尝试过了,但仍然无法正常工作。 - undefined
我知道现在有点晚了,但是我不建议用你的母语(荷兰语)来编写参数和值。 - undefined
4个回答

4
来自Symfony安全文档:https://symfony.com/doc/3.4/security.html#logging-out 请注意,使用http-basic身份验证防火墙时,没有真正的退出方式:唯一的退出方式是让浏览器停止在每个请求中发送您的用户名和密码。清除浏览器缓存或重新启动浏览器通常有所帮助。一些Web开发工具在这里也可能有所帮助。
由于您正在使用http-basic,因此清除cookie将不起作用。因此,如果要使用该代码,则需要实现不同的身份验证并停止使用http-basic。

0
你还没有设置防火墙。
main:
        anonymous: ~

应该看起来像 main: anonymous: ~ secured_arena: pattern: ^/beheerder

有了这个,就表示每个人都可以访问 "main" 防火墙,你应该限制访问区域

当你拥有它后,只需向防火墙添加以下几行

 logout:
            path:   /logout
            target: /

定义/注销路由,这一步你已经完成了。Symfony会自动执行注销操作。
你还需要指定验证器(authenticator)和检查路径(checkpath),请参考https://symfony.com/doc/current/security/custom_password_authenticator.html

0

app/config/security.yml

security:
    # editor fold [...]
    firewalls:
        # editor fold [...]
        main:
            # editor fold [...]
            # add logout into the security firewall
            logout:
                path: security_logout
                target: /
    # editor fold [...]
    access_control:
        - { path: '^/beheerder/*', roles: [ROLE_BEHEERDER] }
        # Not needed
        # - { path: '^/logout', roles: [IS_AUTHENTICATED_ANONYMOUSLY] }

app/config/routing.yml

# editor fold [...]
# add logout path into main routing file
security_logout:
    path: /logout

Twig 视图

<!-- logout link -->
<a href="{{ path('security_logout') }}">Logout</a>

谢谢你的回答,但是它没有起作用。我编辑了问题,也许我实现得有些错误? - undefined
@MrJ. 你在控制器中移除了 /logout 吗?其实不需要... 登出路由应该放在 app/config/routing.yml 文件中,没有其他需要添加的内容... Symfony 会自动处理它 - undefined

0

在你定义登出路径时,访问控制应该是 IS_AUTHENTICATED_ANONYMOUSLY,这是错误的。

请移除 - { path: '^/logout', roles: [IS_AUTHENTICATED_ANONYMOUSLY] }

或者

修改为 - { path: '^/logout', roles: [ROLE_BEHEERDER] }


感谢您的反馈,已经进行了修改。 - undefined
将以下与编程相关的内容从英文翻译成中文。只返回翻译后的文本:- 在访问控制中添加此行代码:{ path: '^/logout', roles: [ROLE_BEHEERDER] },然后进行检查。 - undefined
我试过了,但没有起作用。但这不应该是问题,因为没有它,每个人都可以注销。 - undefined
如果您不介意的话,请配置fos-user bundle以获得最佳的登录功能。 - undefined
目前只有“beheerder”,但我将来会扩展它。 - undefined
你在使用fos吗? - undefined

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