codiantnew / dbaesencrypt
Laravel MySql AES 加密/解密
dev-main
2021-02-26 07:30 UTC
Requires
- php: >=5.4.0
- illuminate/config: >=5.4
- illuminate/database: >=5.4
- illuminate/support: >=5.4
This package is auto-updated.
Last update: 2024-09-29 05:22:32 UTC
README
Laravel MySql AES 加密/解密
Laravel 7.x 数据库加密在 mysql 侧,使用原生 mysql 函数 AES_DECRYPT 和 AES_ENCRYPT
自动加密和解密模型中签名字段/列
可以使用 Eloquent/Model 的所有功能
您可以在加密列之间执行操作 "=>, <',' BETWEEN ',' LIKE '
1.通过 Composer 安装包
$ composer require codiantnew/dbaesencrypt
2.配置提供商
如果您使用的是 Laravel 5.4 或更早版本,您需要在 config/app.php 中添加并注释一行
'providers' => array( // Illuminate\Database\DatabaseServiceProvider::class, Codiant\\DBAESEncrypt\\Database\\DatabaseServiceProviderEncrypt::class )
3.更新您的 Eloquent 模型
拥有加密列的模型应该从 ModelEncrypt 继承
namespace App\Models; use Codiant\DBAESEncrypt\Database\Eloquent; class Product extends ModelEncrypt { /** * The table associated with the model. * * @var string */ protected $table = 'tb_product'; /** * The attributes that are encrypted. * * @var array */ protected $fillableEncrypt = [ 'name' ]; /** * The attributes that are mass assignable. * * @var array */ protected $fillable = [ 'name', 'description', ]; }
4.创建支持加密列的表
它向 Schema 添加了新功能,您可以在迁移中使用
Schema::create('tb_persons', function (Blueprint $table) { // here you do all columns supported by the schema builder $table->increments('id')->unsigned; $table->string('description', 250); $table ->unsignedInteger('created_by')->nullable(); $table ->unsignedInteger('updated_by')->nullable(); }); // once the table is created use a raw query to ALTER it and add the BLOB, MEDIUMBLOB or LONGBLOB DB::statement("ALTER TABLE tb_persons ADD name MEDIUMBLOB after id");
});
5.在 .env 和配置文件中设置加密密钥
Set in .env file APP_AESENCRYPT_KEY=yourencryptedkey Set in config/services.php 'aesEncrypt' => [ 'key' => env('APP_AESENCRYPT_KEY'), ]
6.添加 BaseUser 类以解决可验证模型问题
在 app/BaseUser.php 中创建 BaseUser 类以解决可验证模型问题。在您的 Authenticatable 模型中扩展 BaseUser 类。
namespace App; use Illuminate\Auth\Authenticatable; use Illuminate\Auth\MustVerifyEmail; use Illuminate\Database\Eloquent\Model; use Illuminate\Auth\Passwords\CanResetPassword; use Illuminate\Foundation\Auth\Access\Authorizable; use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract; use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract; use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract; use Codiant\DBAESEncrypt\Database\Eloquent; class BaseUser extends Eloquent\ModelEncrypt implements AuthenticatableContract, AuthorizableContract, CanResetPasswordContract { use Authenticatable, Authorizable, CanResetPassword, MustVerifyEmail; }
重要提示
1. DB query will not work with encrypted column.
2. Union will not work with paginate method. with union you can use simplePaginate method.
3. On encrypted column searching will work case sensitive. To resolve this problem use the MySQL lowercase method.
4. Don't use encrypted columns for making model relations.
5. Use all columns of the encrypted table in fillable array. You can select only columns that are available in fillable array in your select query.
6. Don't use asterisk in select method with encrypted table (ex. ->select('users.*')).
Sometimes you want to use an asterisk with join and some other condition. For that condition, you have to overwrite encrypted columns.
Example : I have encrypt name column in users table.
->select(DB::raw('users.*'),'name as name')