什么是防止未登录用户访问特定页面的正确方法

时间:2014-10-29 11:03:45

标签: php security symfony

我的网站上有以下结构:

/登录页面,当有人访问该网站时,他们会自动进入此页面。它不需要登录。

当有人登录时,他们会进入/ game / welcome页面。 从那里他们可以访问/游戏/帐户和此类页面。

现在,当我直接进入/游戏/欢迎时,如果没有登录,我可以访问此页面。我该如何防止这种情况?

这是我的security.yml文件:

# you can read more about security in the related section of the documentation
# http://symfony.com/doc/current/book/security.html
security:
    # http://symfony.com/doc/current/book/security.html#encoding-the-user-s-password
    encoders:
        Login\LoginBundle\Entity\User: sha512
            #algorithm: sha1
            #iterations: 1
            #encode_as_base64: true
        #Login\Loginbundle\Entity\User: sha512

    # http://symfony.com/doc/current/book/security.html#hierarchical-roles
    role_hierarchy:
        ROLE_ADMIN:       ROLE_USER
        ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]

    # http://symfony.com/doc/current/book/security.html#where-do-users-come-from-user-providers
    providers:
        user:
            entity:
                class: Login\LoginBundle\Entity\User
                property: username
        #in_memory:
            #memory:
                #users:
                    #user:  { password: userpass, roles: [ 'ROLE_USER' ] }
                    #admin: { password: adminpass, roles: [ 'ROLE_ADMIN' ] }

    # the main part of the security, where you can set up firewalls
    # for specific sections of your app
    firewalls:
        secured_area:
            pattern:   ^/
            anonymous: ~
            form_login:
                login_path: login
                check_path: login_check
    access_control:
    - { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }

4 个答案:

答案 0 :(得分:1)

访问控制(security:access_control)是这里的关键词。

- { path: ^/game/welcome, role: ROLE_USER }

这要求用户拥有ROLE_USER(您的登录用户应根据您的yaml拥有)来访问此路由

更多信息:http://symfony.com/doc/current/book/security.html#access-controls-authorization

答案 1 :(得分:1)

如果您想要保护大多数网址,最好保护所有网址,然后添加例外。请记住,ROLE_USER会自动授予所有登录用户。

# add exceptions before the general rule
access_control:
    - { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
    # some more exceptions ...
    - { path: ^/, role: ROLE_USER } # all other URLS need login

答案 2 :(得分:1)

只是这段代码:

- { path: ^/game/*, role: ROLE_USER }

应该够了。这可以防止未登录的用户到达

^/game/*

答案 3 :(得分:1)

通常,检查ROLE_USER应该足够了,但检查角色IS_AUTHENTICATED_FULLY可能更安全,如果要与匿名用户区分,安全组件会自动为经过身份验证的用户设置该角色。

与其他答案建议一样,不要像在security.yml中设置access_control一样,我建议改为保护各个控制器。

这样做的好处是在更改路由URL模式时不会无意中禁用安全性,或者在正则表达式中出错,我发现这种情况很多。

使用SensioFrameworkExtraBundle,您可以使用注释保护控制器:

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class DemoController extends Controller
{
    /**
     * @Security("has_role('IS_AUTHENTICATED_FULLY')")
     */
    public function indexAction()
    {
        // ...
    }
}

如果您不喜欢注释,可以按如下方式检查控制器代码(扩展默认的Controller类时):

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class DemoController extends Controller
{
    public function indexAction()
    {
        if (false === $this->get('security.context')->isGranted('IS_AUTHENTICATED_FULLY')) {
            throw $this->createAccessDeniedException('Unable to access this page!');
        }

        // ...
    }
}