Symfony 2.8 - 如何为任何URL配置防火墙?

时间:2016-05-04 22:05:20

标签: php symfony security routes firewall

每当我故意 - 尝试自定义错误页面 - 尝试访问未定义的路由时,服务器会响应500错误。日志说:

  

request.CRITICAL:处理异常时抛出异常(Symfony \ Component \ Security \ Core \ Exception \ AuthenticationCredentialsNotFoundException:令牌存储不包含身份验证令牌。一个可能的原因可能是没有为此配置防火墙网址

在NotFoundException之后抛出此异常,因此出现500错误。 因此,我试图弄清楚如何为任何URL配置防火墙,尤其是对于已经由防火墙处理过的所有人,以便实际可以找到凭据。我来到了 UserBundle / Resources / config / security.yml

chrome.runtime.sendMessage

我的主安全文件中导入了所有内容,其中包括:

security:
encoders:
    FOS\UserBundle\Model\UserInterface: sha512

providers:
    fos_userbundle:
        id: fos_user.user_provider.username

firewalls:
    dev:
        pattern: ^/(_(profiler|wdt))/
        security: false
    public:
        pattern:                    ^/(contact/faq)$
        anonymous:                  true
    secure:
        pattern:                    ^/
        form_login:
            provider:               fos_userbundle
            csrf_token_generator:   security.csrf.token_manager
            login_path:             fos_user_security_login
            check_path:             fos_user_security_check
            use_forward:            false
            failure_path:           null
            default_target_path:    /
            remember_me:            true
        logout:
            path:                   fos_user_security_logout
            target:                 /
        anonymous:                  true
        remember_me:
            secret:                 %secret%
            name:                   whatev
            lifetime:               31536000
            path:                   /
            remember_me_parameter:  _remember_me
            secure:                 true
            always_remember_me:     true
    default:
        anonymous: true

这是app / Resources / TwigBundle / views / Exception下的error.html.twig:

imports:
- { resource: "@UserBundle/Resources/config/security.yml" }

security:
    role_hierarchy:
        ROLE_ADMIN:       ROLE_USER
        ROLE_SUPER_ADMIN: ROLE_ADMIN

access_control:
    - { path: ^/, roles: IS_AUTHENTICATED_ANONYMOUSLY } # my try to match all routes...
    - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/administration/, role: ROLE_ADMIN }
    - { path: ^/user$, role: IS_AUTHENTICATED_FULLY }

有关如何进行的任何线索?

非常感谢。

1 个答案:

答案 0 :(得分:1)

正如Federico所指出的那样,问题来自于试图执行的事件监听器

public function add(Request $request)
{
    if($this->securityContext->isGranted('IS_AUTHENTICATED_FULLY')) {
        /* do stuff considering the user is logged in.
        ** This is wrong ; we can end up here while having a logged out user.
        */

当然,在考虑它时,它似乎是愚蠢的。通过确保您确实可以在安全上下文中调用isGranted()来解决整个问题。要检查这一点,您必须验证:

  1. 安全上下文令牌不是空的;
  2. 此令牌的用户是您的用户实体的实例(用户实际已登录)。
  3. 这会将上述方法更改为:

    public function add(Request $request)
    {
        if($this->securityContext->getToken() === null)
            return false;
    
        if(!$this->securityContext->getToken()->getUser() instanceof User)
            return false;
    
        if($this->securityContext->isGranted('IS_AUTHENTICATED_FULLY')) {
            // do stuff considering the user is logged in.