specshaper / encrypt-bundle
此包提供了一种通过属性加密值的服务。
Requires
- php: >=7.4|>=8
- ext-mbstring: *
- ext-openssl: *
- ext-sodium: *
- doctrine/annotations: ^1.8|^2.0
- doctrine/doctrine-bundle: ^2.5
- doctrine/orm: ^2.11
- symfony/console: ^5.4|^6.0|^7.0
- symfony/framework-bundle: ^5.4|^6.0|^7.0
- symfony/monolog-bundle: ^3.7
Requires (Dev)
- symfony/phpunit-bridge: ^6.0
This package is auto-updated.
Last update: 2024-09-04 09:56:15 UTC
README
一个用于使用 OpenSSL 和 Doctrine 生命周期事件处理参数编码和解码的包。
功能包括
- V4 将是 Symfony 7 PHP 8.2
- V3 是 Symfony 5.4|6 PHP 8。由于我将我的项目迁移到 symfony 7,因此将不会在此 v3.2 之后维护。
- V2 是 Symfony 5,不再维护。
- v1 是 Symfony 3.4,不再维护。
- 使用 OpenSSL
- 使用生命周期事件
功能路线图
- 创建一个工厂方法,以扩展不同的加密器
- 创建一个 twig 函数以解密编码的值
- 扩展参数以允许选择编码方法
- 创建 CLI 命令以加密和解密整个数据库
- 通过包处理 DateTime 数据类型。
许可
此包受 MIT 许可证保护。请参阅包中的完整许可证。
Resources/meta/LICENSE
关于
EncryptBundle 已编写用于 Parolla Plugins 和 Parolla 网站以编码用户私人数据。该包在更大的 gdpr-bundle 中扩展。
报告问题或功能请求
问题和功能请求在 Github 问题跟踪器 中跟踪。
报告错误时,最好在基于 Symfony 标准版 的基本项目中重现它,以便包的开发者可以通过简单地克隆它并遵循一些步骤来重现问题。
安装
步骤 1:从包安装
打开命令行控制台,进入您的项目目录,并执行以下命令以下载此包的最新开发版本
$ composer require specshaper/encrypt-bundle dev-master
此命令要求您全局安装 Composer,如 Composer 文档的 安装章节 中所述。
步骤 2:启用包
该食谱将在 config/packages/spec_shaper_encrypt.yaml 下创建一个包配置文件。
如果需要,可以通过将其添加到项目 config/bundles.php 文件中注册的包列表中来启用该包
<?php return [ ... SpecShaper\EncryptBundle\SpecShaperEncryptBundle::class => ['all' => true], ];
步骤 2:配置包
使用包中提供的命令生成一个 256 位密钥。
$ bin/console encrypt:genkey
将密钥复制到您的 .env 文件中。
###> encrypt-bundle ###
SPEC_SHAPER_ENCRYPT_KEY= change_me!
###< encrypt-bundle ###
将密钥解析在包 yaml 文件中。
# app/config/packages/spec_shaper_encrypt.yaml spec_shaper_encrypt: encrypt_key: '%env(SPEC_SHAPER_ENCRYPT_KEY)%' is_disabled: false # Turn this to true to disable the encryption. connections: # Optional, define the connection name(s) for the subscriber to listen to. - 'default' - 'tenant' subscriber_class: App\Subscriber\MyCustomSubscriber # Optional to override the bundle Doctrine event subscriber. encryptor_class: App\Encryptors\MyCustomEncryptor # Optional to override the bundle OpenSslEncryptor. annotation_classes: # Optional to override the default annotation/Attribute object. - App\Annotation\MyAttribute
您可以通过将 'is_disabled' 选项设置为 true 来禁用加密。如果任何值包含 <ENC> 后缀,则仍会继续解密。
您可以通过使用 'subscriber_class' 选项来指定您的自定义订阅者,以扩展 EncryptBundle 的默认订阅者并覆盖其方法。
如果您想定义自己的注解/属性,则可以使用此功能通过将注解类名添加到 'annotation_classes' 选项数组中来触发加密。
您可以通过使用可选的 encryptorClass 选项传递您自己的加密器服务类名。
备用 EncryptKeyEvent
可以通过派发的监听器设置EncryptKey,这将覆盖任何由.env或param.yml定义的密钥。创建一个针对EncryptKeyEvents::LOAD_KEY事件的监听器,并在该点设置您的加密密钥。
第3步:创建实体
在实体中添加Encrypted属性类。
<?php ... use SpecShaper\EncryptBundle\Annotations\Encrypted;
将#[Encrypted]属性添加到您想要加密的属性中。
注意,参数中的旧注解'@Encrypted'已弃用,将在下一个主要更新中停止使用。
<?php /** * A PPS number is always 7 numbers followed by either one or two letters. * * @ORM\Column(type="string") */ #[Encrypted] protected string $taxNumber; /** * True if the user is self employed. * * @ORM\Column(type="string", nullable=true) */ #[Encrypted] protected ?bool $isSelfEmployed; /** * Date of birth * * @Encrypted * Note that the above Encrypted property is a legacy annotation, and while * it still is supported, it will be deprecated in favour of Attributes. * * @ORM\Column(type="string", nullable=true) */ protected ?String $dob;
在加密字段时,需要将列类型设置为字符串。
您的getter和setter也可能需要声明类型。
例如,布尔值应返回声明为bool的结果,或使用三元方法返回bool。
<?php /** * Get isSelfEmployed * * @return boolean */ public function isSelfEmployed(): bool { return $this->isSelfEmployed; } /** * Get isSelfEmployed * * @return boolean */ public function isSelfEmployed(): bool { return ($this->isSelfEmployed == 1 ? true: false); }
对于DateTime参数,将日期存储为字符串,并使用getter和setter进行转换。
如果您在具有DateType表单类型的表单中使用参数,可能还需要创建一个DataTransformer。
第4步:通用使用
该组件包含一个DoctrineEncryptSubscriber。该监听器捕获doctrine事件onLoad、onFlush和postFlush。
onLoad事件监听器将在加载时解密实体参数。这意味着您的表单和表单字段已经解密。
onFlush和postFlush事件监听器将检查是否启用了加密,并在数据进入数据库之前加密数据。
因此,在正常的CRUD操作中,您不需要在控制器中进行任何加密或解密数据的操作。
第5步:在服务和控制器中解密
您当然可以在类中随时通过自动装配或定义服务定义中的注入来注入EncryptorInterface服务。
<?php use SpecShaper\EncryptBundle\Encryptors\EncryptorInterface; ... /** * @var SpecShaper\EncryptBundle\Encryptors\EncryptorInterface; */ private $encryptor; ... // Inject the Encryptor from the service container at class construction public function __construct(EncryptorInterface $encryptor) { $this->encryptor = $encryptor; } // Inject the Encryptor in controller actions. public function editAction(EncryptorInterface $encryptor) { ... // An example encrypted value, you would get this from your database query. $encryptedValue = "3DDOXwqZAEEDPJDK8/LI4wDsftqaNCN2kkyt8+QWr8E=<ENC>"; $decrypted = $encryptor->decrypt($encryptedValue); ... }
或者,您可以派发EncryptEvent。
<?php ... use SpecShaper\EncryptBundle\Event\EncryptEvent; use SpecShaper\EncryptBundle\Event\EncryptEvents; use Symfony\Component\EventDispatcher\EventDispatcherInterface; ... public function indexAction(EventDispatcherInterface $dispatcher) { ... // An example encrypted value, you would get this from your database query. $event = new EncryptEvent("3DDOXwqZAEEDPJDK8/LI4wDsftqaNCN2kkyt8+QWr8E=<ENC>"); $dispatcher->dispatch(EncryptEvents::DECRYPT, $event); $decrypted = $event->getValue(); }
第5步:在模板中解密
如果您使用返回数组结果的select查询来查询存储库,则doctrine onLoad事件监听器不会解密任何加密值。
在这种情况下,在渲染时使用twig过滤器来解密您的值。
{{ employee.bankAccountNumber | decrypt }}
命令
您已经看到了生成加密密钥的命令
$ bin/console encrypt:genkey
您可以使用以下命令解密/加密整个数据库
$ bin/console encrypt:database decrypt connection
所需的参数应该是decrypt或encrypt。
如果您的应用程序使用多个连接,则可以定义数据库连接。