Check_path 不在 symfony 防火墙后面,如何纠正这个问题?

发布于 2024-12-09 20:14:05 字数 3119 浏览 1 评论 0原文

我正在尝试针对 symfony2 防火墙进行身份验证,这是我的安全配置

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:
            users:
                user:  { password: user, roles: [ 'ROLE_USER' ] }
                admin: { password: admin, roles: [ 'ROLE_ADMIN' ]}
        #main:
            #entity: { class: Surgeworks\CoreBundle\Entity\User, property: username}
    firewalls:
        public:
           pattern: .*
           security: false
           anonymous: true
           form_login:
                check_path: /{_locale}/admin/logincheck
        login:
           pattern: ^/{_locale}/admin/login$
           security: false
           anonymous:  ~
        dev:
           pattern:  ^/(_(profiler|wdt)|css|images|js)/
           security: false
           anonymous:  ~
        secured_area :
            provider:   in_memory
            pattern: ^/{_locale}/admin/.*
            form_login:
                check_path: /{_locale}/admin/logincheck
                login_path: /{_locale}/admin/login
            logout:
                path : /{_locale}/admin/logout
                target : /{_locale}/admin/
            remember_me:
               key:      aSecretKey
               lifetime: 3600
               path:     /admin/
               domain:   ~ # Defaults to the current domain from $_SERVER 
    access_control:
        - { path: ^/{_locale}/admin/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/{_locale}/, roles: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/ar/admin/logincheck, roles: ROLE_ADMIN }
        - { path: ^/(ar|en|fr)/admin/, roles: ROLE_ADMIN }

,这是我在 DaghoSiteBundle/Resources/config/routing.yml 中的路由:

_admin:
    pattern: /admin/
    defaults: { _controller: DaghoSiteBundle:Login:login , _locale : ar }
    requirements:
     _locale: (ar|en|fr)
login:
    pattern: /admin/login
    defaults : { _controller: DaghoSiteBundle:Login:login , _locale : ar }
    requirements:
        _locale: (ar|en|fr)
logincheck:
    pattern: /admin/logincheck
    #defaults: { _controller: DaghoSiteBundle:Login:logincheck , _locale: ar }
    #requirements:
        #_locale: (ar|en|fr)
logout:
    pattern: /admin/logout

我无法通过这些设置登录, 它总是抛出异常

无法找到路径“/ar/admin/logincheck”的控制器。或许 您忘记在路由配置中添加匹配的路由

,即使已经设置了路由 check_path 页面 >>>我将能够查看 check_path 而无需重定向到登录页面..

/en/admin >> login page 
/en/logincheck >> display the logincheck template (i.e  /en/logincheck isn't behind firewall )

如何修复或调试此问题,请告知

更新: 抱歉,我可能忘记写下我已经在我的包中添加了 {_locale} 前缀,如下所示 在我的routing.yml中

DaghoSiteBundle:
    resource: "@DaghoSiteBundle/Resources/config/routing.yml"
    prefix:   /{_locale}
    requirements:
        _locale: ar|en|fr
    defaults: { _locale: ar }

I am trying to authenticate against symfony2 firewall , here is my security config

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:
            users:
                user:  { password: user, roles: [ 'ROLE_USER' ] }
                admin: { password: admin, roles: [ 'ROLE_ADMIN' ]}
        #main:
            #entity: { class: Surgeworks\CoreBundle\Entity\User, property: username}
    firewalls:
        public:
           pattern: .*
           security: false
           anonymous: true
           form_login:
                check_path: /{_locale}/admin/logincheck
        login:
           pattern: ^/{_locale}/admin/login$
           security: false
           anonymous:  ~
        dev:
           pattern:  ^/(_(profiler|wdt)|css|images|js)/
           security: false
           anonymous:  ~
        secured_area :
            provider:   in_memory
            pattern: ^/{_locale}/admin/.*
            form_login:
                check_path: /{_locale}/admin/logincheck
                login_path: /{_locale}/admin/login
            logout:
                path : /{_locale}/admin/logout
                target : /{_locale}/admin/
            remember_me:
               key:      aSecretKey
               lifetime: 3600
               path:     /admin/
               domain:   ~ # Defaults to the current domain from $_SERVER 
    access_control:
        - { path: ^/{_locale}/admin/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/{_locale}/, roles: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/ar/admin/logincheck, roles: ROLE_ADMIN }
        - { path: ^/(ar|en|fr)/admin/, roles: ROLE_ADMIN }

and here is my routing in DaghoSiteBundle/Resources/config/routing.yml:

_admin:
    pattern: /admin/
    defaults: { _controller: DaghoSiteBundle:Login:login , _locale : ar }
    requirements:
     _locale: (ar|en|fr)
login:
    pattern: /admin/login
    defaults : { _controller: DaghoSiteBundle:Login:login , _locale : ar }
    requirements:
        _locale: (ar|en|fr)
logincheck:
    pattern: /admin/logincheck
    #defaults: { _controller: DaghoSiteBundle:Login:logincheck , _locale: ar }
    #requirements:
        #_locale: (ar|en|fr)
logout:
    pattern: /admin/logout

I can't login through these setting ,
it always throw an exception

Unable to find the controller for path "/ar/admin/logincheck". Maybe
you forgot to add the matching route in your routing configuration

and even if had setup the route check_path page >> i would be able to view the check_path without being redirected to login page ..

/en/admin >> login page 
/en/logincheck >> display the logincheck template (i.e  /en/logincheck isn't behind firewall )

how to fix , or debug this issue , please advise

UPDATE:
sorry I might forget to write that i had prefixed my bundle with the {_locale} like this
in my routing.yml

DaghoSiteBundle:
    resource: "@DaghoSiteBundle/Resources/config/routing.yml"
    prefix:   /{_locale}
    requirements:
        _locale: ar|en|fr
    defaults: { _locale: ar }

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

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

发布评论

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

评论(3

十雾 2024-12-16 20:14:05

我认为您必须更改路线模式以包含您的 _locale 参数。例如,您应该使用 pattern: /admin/logincheck 而不是 pattern: {_locale}/admin/logincheck

您还可以使用控制台命令来调试路由 <代码>应用程序/控制台路由器:调试。

我不能 100% 确定这可以解决您的问题,但我希望它可以帮助您收集有关您的问题的更多信息。

I think you must change your route patterns to include your _locale parameter. Instead of, for example, pattern: /admin/logincheck you should use pattern: {_locale}/admin/logincheck

You can also debug your routes by using a console command app/console router:debug.

I am not 100% sure this fixes your problem, but I hope it helps you gather more info about your issue.

花辞树 2024-12-16 20:14:05

你为什么不尝试一下

logincheck:
    pattern: /admin/login_check
    #defaults: { _controller: DaghoSiteBundle:Login:logincheck , _locale: ar }
    #requirements:
        #_locale: (ar|en|fr)

Why dont you try

logincheck:
    pattern: /admin/login_check
    #defaults: { _controller: DaghoSiteBundle:Login:logincheck , _locale: ar }
    #requirements:
        #_locale: (ar|en|fr)
独自←快乐 2024-12-16 20:14:05

这解决了我的问题,但我的情况很简单,没有 {_locale} 功能,但也许它会对你有所帮助。

来自 Symf2 书籍的安全章节:
常见陷阱 #3:确保 /login_check 位于防火墙后面。

在 security.yml 文件中,您的 check_path 路由看起来像是 /{_locale}/admin/logincheck,并且您的安全路径是以下任何内容^/{_locale}/admin/.* 后面
所以这似乎很好。
您可能想尝试删除 .* (您真的需要它吗?)

并在访问控制部分的文件下方提供特定条目以确保 check_path 需要身份验证:
- { 路径:^/ar/admin/logincheck,角色:ROLE_ADMIN }
也许尝试指定这个:
- { 路径:^/{_locale}/admin/logincheck,角色:ROLE_ADMIN }

但实际上,我想知道,您甚至可以在 security.yml 模式值中使用 {placeholders} 吗? 我知道您可以在路由文件中使用,但我不确定是否可以security yml 的工作方式相同吗?我没有看到它在 symf2 主书中的安全或路由章节的任何示例中使用?

This solved my problem, but my situation is simple and doesn't have the {_locale} functionality, but maybe it will help you anyway.

From the Security chapter of the Symf2 Book:
Common Pitfalls #3: Be sure /login_check is behind a firewall.

In your security.yml file it looks like your check_path route is /{_locale}/admin/logincheck, and your secured path is anything that's behind ^/{_locale}/admin/.*
so that seems to be good.
You might want to try removing the .* (do you really need it?)

and further down the file in the access control section you provide a specific entry to make sure the check_path requires authentication:
- { path: ^/ar/admin/logincheck, roles: ROLE_ADMIN }
Maybe try specifying this instead:
- { path: ^/{_locale}/admin/logincheck, roles: ROLE_ADMIN }

But actually, I was wondering, can you even use {placeholders} in the security.yml pattern values? I know you can in the routing file, but I'm not sure if the security yml works the same way? I don't see it being used in any examples in the main symf2 book in the security or routing chapters?

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