Symfony2+Twig:对经过身份验证的用户和匿名用户使用模板
我是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
防火墙不共享安全上下文。因此,当操作不在防火墙后面时,您无法访问用户信息。尝试将整个应用程序置于防火墙后面(并允许匿名用户):
确保匿名用户可以访问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):
Make sure the login_path can be accessed by anonymous users.