Symfony2+Twig:对经过身份验证的用户和匿名用户使用模板

发布于 2024-12-06 12:16:03 字数 594 浏览 0 评论 0原文

我是 symfony 和 twig 的新手,对安全性、防火墙和模板有些头疼。

我想做的是有一个显示顶栏的“基本”模板。如果用户未登录,我希望此顶部栏显示“您尚未登录”,如果用户已登录,则显示“欢迎用户 U”消息。 因此,我

{% if is_granted('IS_AUTHENTICATED_FULLY') %}

在“基本”模板中放置了一个来区分登录用户和匿名用户,但我在安全上下文令牌方面遇到了问题。

我的公共路径(不受防火墙保护)是:

/myapp/ 
/myapp/home 
/myapp/about 
/myapp/help

后来有一些操作路径只能访问经过身份验证的用户:

/myapp/action1
/myapp/action2
...
/myapp/actionN

问题是,一旦用户登录我的“基地”,就会在 actions1, 视图中显示欢迎消息。 ..,actionN,但是当用户转到“主页”或“帮助”页面时,消息是“您尚未登录”。

有人有类似情况吗?你是怎么解决的?您的路由器和安全文件是如何配置的?

I'm new to symfony and twig and I have some headache with security, firewalls and templates.

What I'm trying to do is to have a "base" template that shows a topbar. I would like this top bar show a "You are not logged in" if the user is not logged and a "Welcome user U" message if the user is logged.
Because this I put an

{% if is_granted('IS_AUTHENTICATED_FULLY') %}

in the "base" template to differentiate between logged and anonymous users but I have problems about security context tokens.

My public paths (not secured by firewall) are:

/myapp/ 
/myapp/home 
/myapp/about 
/myapp/help

and later there are some paths for actions only can access authenticated users:

/myapp/action1
/myapp/action2
...
/myapp/actionN

The problem is, once a user is logged in my "base" show the welcome message in the view of actions1, ..., actionN but when user goed to "home" or "help" pages the message is "you are not logged in".

Some has a similar situacion? how did you solve it? how are your router and security files configured?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

飘落散花 2024-12-13 12:16:03

防火墙不共享安全上下文。因此,当操作不在防火墙后面时,您无法访问用户信息。尝试将整个应用程序置于防火墙后面(并允许匿名用户):

firewalls:
    secured_area:
        pattern: ^/
        anonymous: ~
        form_login:
            check_path: /login_check
            login_path: /login
        logout:
            path:   /logout
            target: /
access_control:
    - { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/action, roles: ROLE_USER }

确保匿名用户可以访问login_path。

The firewall doesn't share the security context. So when a action is not behind the firewall you can't acces the user info. Try placing the entire app behind the firewall (and allow anonymous users):

firewalls:
    secured_area:
        pattern: ^/
        anonymous: ~
        form_login:
            check_path: /login_check
            login_path: /login
        logout:
            path:   /logout
            target: /
access_control:
    - { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/action, roles: ROLE_USER }

Make sure the login_path can be accessed by anonymous users.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文