coosos/ip-filter-bundle

Symfony 4 & 5 Ip Filter Bundle

4.1.0 2019-12-25 17:05 UTC

This package is auto-updated.

Last update: 2024-09-26 04:02:30 UTC


README

注意:此项目是从 Spomky-Labs/IpFilterBundle 分支出来的,因为它已被弃用。

Build Status

描述

此包可以帮助您通过使用 IP地址IP地址范围 来限制应用程序的访问。

它支持IPv4和IPv6地址以及多个环境。

例如,您可以在devtest环境中允许从192.168.1.1192.168.1.100的地址范围访问,并拒绝其他所有访问。

请注意,与例如由.htaccess文件提供的类似功能相比,此包在性能方面表现不佳。

此包需要或使用

策略

请注意,授权IP地址的优先级高于未授权IP地址。例如,如果范围192.168.1.10192.168.1.100未授权的,而192.168.1.20授权的,那么将允许192.168.1.20访问。

安装

安装是一个简单的4步过程

  • 下载包
  • 启用包
  • 创建模型类
  • 配置应用程序

##第1步:安装包##

安装此包的首选方式是依赖于Composer

composer require "coosos/ip-filter-bundle" "~4.1"

##第2步:启用包##

config/bundles.php中启用包

<?php
// config/bundles.php

return [
    // .....
    Coosos\IpFilterBundle\CoososIpFilterBundle::class => ['all' => true]
    // .....
];

##第3步:创建IP类##

此包需要一个数据库来持久化过滤后的IP地址。

您首先的工作是创建应用程序的Ip类。这些类可以看起来和表现方式如何:添加任何有用的属性或方法。

在下文中,您将看到您的类应该如何查看的示例。

###Ip类:###

<?php
// src/Entity/Ip.php

namespace App\Entity;

use Coosos\IpFilterBundle\Entity\Ip as BaseIp;
use Doctrine\ORM\Mapping as ORM;

/**
 * Ip
 *
 * @ORM\Table(name="ips")
 */
class Ip extends BaseIp
{
    /**
     * @var integer $id
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    public function getId()
    {
        return $this->id;
    }
}

##第4步:配置应用程序##

设置您的类和管理员

# config/packages/config.yml
sl_ip_filter:
    ip_class: App\Entity\Ip

如果您有自己的管理员,您可以使用它们。它们只需要实现Coosos\IpFilterBundle\Model\IpManagerInterface

# app/config/config.yml
sl_ip_filter:
    ...
    ip_manager: my.custom.ip.entity_manager

###安全策略###

为了使此包生效,您需要更改默认的访问决策策略,默认情况下,如果任何投票者授予访问权限,则授予访问权限。

您还需要在您的网站后面放置防火墙规则。

# config/packages/security.yml
security:
    access_decision_manager:
        strategy: unanimous

firewalls: 
    my_site:
        pattern: ^/
        anonymous: ~

access_control:
    - { path: ^/, roles: IS_AUTHENTICATED_ANONYMOUSLY }

如何

小示例

如何在devtest环境中允许192.168.1.10访问并拒绝其他所有访问?

<?php

use Coosos\IpFilterBundle\Model\IpManagerInterface;

/** @var IpManagerInterface $ipManager */
$ipManager = $this->container->get('sl_ip_filter.ip_manager'); //Use this line, even if you use a custom IP manager

//Create your IP
$ip = $ipManager->createIp();
$ip->setStartIp('192.168.1.10') // Use only setStartIp() if it's a simple IP
   ->setEnvironment(['dev', 'test']) // You can also use 'prod'
   ->setAuthorized(false);

$ipManager->saveIp($ip);

// If you need to block or authorized an IP range, you must also specify an end ip with the setEndIp() method
$ip = $ipManager->createIp();
$ip->setStartIp('10.30.0.0')
   ->setEndIp('10.30.0.255')
   ->setEnvironment(['dev', 'test'])
   ->setAuthorized(true);

$ipManager->saveIp($ip);

网络支持

可以使用范围对象来支持网络。您只需要获取第一个和最后一个IP地址。此包提供范围计算器,因此您可以使用它轻松扩展范围实体。

<?php

use Coosos\IpFilterBundle\Model\IpManagerInterface;

/** @var IpManagerInterface $ipManager */
$ipManager = $this->container->get('sl_ip_filter.ip_manager');

//All addresses (IPv4)
$range1 = $ipManager->hydrateModelWithIp('0.0.0.0/0');
$range1->setEnvironment(['dev', 'test'])
       ->setAuthorized(false);

$ipManager->saveIp($range1);

//My local network (IPv4)
$range2 = $ipManager->hydrateModelWithIp('192.168.0.0/16');
$range2->setEnvironment(['dev', 'test'])
       ->setAuthorized(true);

$ipManager->saveIp($range2);

//Another local network (IPv6)
$range3 = $ipManager->hydrateModelWithIp('fe80::/64');
$range3->setEnvironment(['dev', 'test'])
       ->setAuthorized(true);

$ipManager->saveIp($range3);