概述 快速入门 教程 手册 最佳实践 组件 参考 贡献

发布于 2015-08-27 16:47:02 | 163 次阅读 | 评论: 0 | 来源: 网络整理

When using the Security component, you can create firewalls that match certain request options. In most cases, matching against the URL is sufficient, but in special cases you can further restrict the initialization of a firewall against other options of the request.

注解

You can use any of these restrictions individually or mix them together to get your desired firewall configuration.

Restricting by Pattern

This is the default restriction and restricts a firewall to only be initialized if the request URL matches the configured pattern.

  • YAML
    # app/config/security.yml
    
    # ...
    security:
        firewalls:
            secured_area:
                pattern: ^/admin
                # ...
    
  • XML
    <!-- app/config/security.xml -->
    <?xml version="1.0" encoding="UTF-8"?>
    <srv:container xmlns="http://symfony.com/schema/dic/security"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:srv="http://symfony.com/schema/dic/services"
        xsi:schemaLocation="http://symfony.com/schema/dic/services
            http://symfony.com/schema/dic/services/services-1.0.xsd">
    
        <config>
            <!-- ... -->
            <firewall name="secured_area" pattern="^/admin">
                <!-- ... -->
            </firewall>
        </config>
    </srv:container>
    
  • PHP
    // app/config/security.php
    
    // ...
    $container->loadFromExtension('security', array(
        'firewalls' => array(
            'secured_area' => array(
                'pattern' => '^/admin',
                // ...
            ),
        ),
    ));
    

The pattern is a regular expression. In this example, the firewall will only be activated if the URL starts (due to the ^ regex character) with /admin. If the URL does not match this pattern, the firewall will not be activated and subsequent firewalls will have the opportunity to be matched for this request.

Restricting by Host

If matching against the pattern only is not enough, the request can also be matched against host. When the configuration option host is set, the firewall will be restricted to only initialize if the host from the request matches against the configuration.

  • YAML
    # app/config/security.yml
    
    # ...
    security:
        firewalls:
            secured_area:
                host: ^admin.example.com$
                # ...
    
  • XML
    <!-- app/config/security.xml -->
    <?xml version="1.0" encoding="UTF-8"?>
    <srv:container xmlns="http://symfony.com/schema/dic/security"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:srv="http://symfony.com/schema/dic/services"
        xsi:schemaLocation="http://symfony.com/schema/dic/services
            http://symfony.com/schema/dic/services/services-1.0.xsd">
    
        <config>
            <!-- ... -->
            <firewall name="secured_area" host="^admin.example.com$">
                <!-- ... -->
            </firewall>
        </config>
    </srv:container>
    
  • PHP
    // app/config/security.php
    
    // ...
    $container->loadFromExtension('security', array(
        'firewalls' => array(
            'secured_area' => array(
                'host' => '^admin.example.com$',
                // ...
            ),
        ),
    ));
    

The host (like the pattern) is a regular expression. In this example, the firewall will only be activated if the host is equal exactly (due to the ^ and $ regex characters) to the hostname admin.example.com. If the hostname does not match this pattern, the firewall will not be activated and subsequent firewalls will have the opportunity to be matched for this request.

Restricting by HTTP Methods

The configuration option methods restricts the initialization of the firewall to the provided HTTP methods.

  • YAML
    # app/config/security.yml
    
    # ...
    security:
        firewalls:
            secured_area:
                methods: [GET, POST]
                # ...
    
  • XML
    <!-- app/config/security.xml -->
    <?xml version="1.0" encoding="UTF-8"?>
    <srv:container xmlns="http://symfony.com/schema/dic/security"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:srv="http://symfony.com/schema/dic/services"
        xsi:schemaLocation="http://symfony.com/schema/dic/services
            http://symfony.com/schema/dic/services/services-1.0.xsd">
    
        <config>
            <!-- ... -->
            <firewall name="secured_area" methods="GET,POST">
                <!-- ... -->
            </firewall>
        </config>
    </srv:container>
    
  • PHP
    // app/config/security.php
    
    // ...
    $container->loadFromExtension('security', array(
        'firewalls' => array(
            'secured_area' => array(
                'methods' => array('GET', 'POST'),
                // ...
            ),
        ),
    ));
    

In this example, the firewall will only be activated if the HTTP method of the request is either GET or POST. If the method is not in the array of the allowed methods, the firewall will not be activated and subsequent firewalls will again have the opportunity to be matched for this request.

最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务