m6web/firewall-bundle

此包已被废弃,不再维护。没有建议的替代包。

提供控制器和操作IP过滤功能的包

安装: 299,513

依赖项: 2

建议者: 0

安全: 0

星标: 23

关注者: 57

分支: 11

开放问题: 2

类型:symfony-bundle

v3.0.0 2019-11-18 14:08 UTC

README

此扩展包为您的Symfony应用程序提供IP过滤功能。
它使用Firewall组件并提供服务和注释配置。

要将此扩展包集成到Symfony 3或Symfony 4应用程序中,请使用至少v3.0.0版本。

安装

在您的composer.json中添加此行

{
    "require": {
        "m6web/firewall-bundle": "dev-master"
    }
}

更新您的供应商

composer update m6web/firewall-bundle

注册

class AppKernel extends \Symfony\Component\HttpKernel\Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            new M6Web\Bundle\FirewallBundle\M6WebFirewallBundle(),
        );
    }
}

使用

配置

m6web_firewall:
    lists:             	                   # Define some IP lists into the Firewall Provider
        self: 				                    # Define a list named "self"
            - '127.0.0.1' 			                # IPV4
            - '::1'      	 	        	        # IPV6 short notation
        lan:     			                    # Define a list named "lan"
            - '192.168.0.*' 		                # IPV4 with Wildcard (* = all)
            - '192.168.0.0/24' 		                # IPV4 with CIDR Mask
            - '192.168.0.0/255.255.255.0' 	        # IPV4 with Subnet Mask
    configs: 				               # Define some pre-defined configurations into the Firewall Provider
        default: 				                # Define a configuration named "default"
            default_state: true 		            # Default returned value (default: true)
            throw_error: true 		                # Throw an exception for rejected users (default: true)
            error_code: 403 		                # Exception status code (default: 403)
            error_message: 'Forbidden' 	            # Exception message (default: Forbidden)
            lists: 			                        # Lists access state
                self: true 			                    # "self" list records will be allowed by the firewall
                lan: false 			                    # "lan" list records will be rejected by the firewall
            entries: 			                    # Define custom IP's access state
                '192.168.0.10': true 	                # "192.168.0.10" will be allowed
                '192.168.0.20': false 	                # "192.168.0.20" will be rejected

全局注释

use M6Web\Bundle\FirewallBundle\Annotation\Firewall;

/**
 * @Firewall(
 *      config="default",
 *      actions={
 *          'myFirstAction'
 *      },
 *      default_state=true,
 *      lists={
 *          'default': true
 *      },
 *      entries={
 *          '192.168.0.50': false
 *      },
 *      throw_error: false,
 *      callback="myFirewallResponseHandler",
 *      error_message: 'Forbiden',
 *      error_code: 403
 * )
 */
  • config参数设置要使用哪些预定义配置
  • actions参数设置哪些控制器操作受保护(在类注释的情况下)

所有默认设置参数都可以通过注释覆盖。

类注释

use M6Web\Bundle\FirewallBundle\Annotation\Firewall;

/**
 * @Firewall(
 *      config="default",
 *      actions={
 *          'myFirstAction'
 *      }
 * )
 */
class MyBundleController extends Controller
{
    public function myFirstAction()
    {
    }

    public function mySecondAction()
    {
    }
}
  • myFirstActiondefault预定义配置的保护。
    在这种情况下,我们可以设置一个(或多个)用于许多操作的firewall。

方法注释

use M6Web\Bundle\FirewallBundle\Annotation\Firewall;

class MyBundleController extends Controller
{
    /**
     * @Firewall(
     *      config="default"
     * )
     */
    public function myFirstAction()
    {
    }

    /**
     * @Firewall(
     *      default_state=true,
     *      lists={
     *           'lan': false
     *      },
     *      entries={
     *          '20.30.40.50': false
     *      }
     * )
     */
    public function mySecondAction()
    {
    }
}
  • myFirstAction使用其自己的firewall和default预定义配置
  • mySecondAction使用其自己的firewall和自定义配置

路径配置

m6web_firewall:
    patterns:                             # define some routing pattern to filter
        api:
            config: default                         # config associed to the path
            path: /api                              # path to filter
  • config参数设置要使用哪些预定义配置
  • path参数设置哪些路径受保护。

运行测试

$ php composer.phar install --dev
$ ./vendor/bin/atoum -d Tests/

致谢

Cytron团队开发,M6 Web
atoum进行测试。

许可

FirewallBundle遵循MIT许可证