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


What is the proper way to prevent not logged in users to acces specific pages

我的网站上有以下结构:

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

当有人登录时,他们会进入/游戏/欢迎页面。从那时起,他们可以访问/游戏/帐户等页面。

现在,当我直接进入/游戏/欢迎,不登录,我可以访问这个页面。我该如何防止这种情况发生?

这是我的安全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 }

通常,检查ROLE_USER就足够了,尽管检查角色is_AUTHENTICATE_FULLY可能更安全,如果您想区别于匿名用户,安全组件会为经过身份验证的用户自动设置该角色。

与其他答案建议的那样,我建议保护各个控制器,而不是采取在安全性中设置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()
    {
        // ...
    }
}

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

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!');
        }
        // ...
    }
}

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

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

这需要用户拥有ROLE_user(根据您的yaml,您的登录用户应该拥有)来访问此路由

更多信息:http://symfony.com/doc/current/book/security.html#access-控制授权

如果你想保护大多数URL,最好是保护所有URL,然后添加异常。请记住,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

仅此代码:

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

应该足够了。这将阻止未登录的用户访问

^/game/*