An open API service indexing awesome lists of open source software.

https://github.com/maxlcoder/laravel-desensitization

接口脱敏中间件
https://github.com/maxlcoder/laravel-desensitization

Last synced: 1 day ago
JSON representation

接口脱敏中间件

Awesome Lists containing this project

README

          

# Laravel Desensitization Middleware

[![Latest Version on Packagist](https://img.shields.io/packagist/v/maxlcoder/laravel-desensitization.svg?style=flat-square)](https://packagist.org/packages/maxlcoder/laravel-desensitization)
[![Total Downloads](https://img.shields.io/packagist/dt/maxlcoder/laravel-desensitization.svg?style=flat-square)](https://packagist.org/packages/maxlcoder/laravel-desensitization)
![GitHub Actions](https://github.com/maxlcoder/laravel-desensitization/actions/workflows/main.yml/badge.svg)

[English](#english) | [简体中文](#简体中文)

---

## English

A middleware for desensitizing API response data. It is configuration-driven and supports highly customizable processing functions (methods).

### Installation

```bash
composer require maxlcoder/laravel-desensitization
```

### Configuration

Publish configuration:

```bash
php artisan vendor:publish --provider="Maxlcoder\LaravelDesensitization\LaravelDesensitizationServiceProvider"
```

#### Config Fields

- `functions`: Global helper function mapping. Example: `'mobile' => 'desensitiseMobile'` means fields with `type=mobile` in `uris` will use this helper.
- `class`: Global custom handler class config. `name` is the full class path (string), and `functions` maps field type to class method.
- Priority: `functions` has higher priority. If not found, it falls back to `class`. If both are missing, no processing is applied and an error log is recorded.
- `uris`: Defines APIs and field paths to process with corresponding types. The middleware parses response structure and iterates fields. Use `*` for array items.

```php
[
'functions' => [
'mobile' => 'desensitise_mobile',
'name' => 'desensitise_name'
],
'class' => [
'name' => 'App\Lib\Desensitization',
'functions' => [
'mobile' => 'desensitiseMobile',
],
],
'uris' => [
'admin/admins' => [
['key' => 'data.data.*.mobile', 'type' => 'mobile'],
['key' => 'data.data.*.name', 'type' => 'name'],
],
],
];
```

### Usage

Register middleware in `Kernel.php`:

```php
protected $routeMiddleware = [
// ...
'desensitization' => \Maxlcoder\LaravelDesensitization\Http\Middleware\Desensitization::class,
];
```

### Example

#### 1) Original Response

```json
{
"code": 200,
"msg": "success",
"data": {
"mobile": "18900000001",
"contacts": [
{
"name": "王组闲",
"mobile": "18900000002"
}
]
}
}
```

#### 2) Config

Edit `config/laravel-desensitization.php`:

```php
[

],
'class' => [
'name' => 'App\Lib\Desensitization',
'functions' => [
'name' => 'desensitiseRealName',
'mobile' => 'desensitiseMobile'
],
],
'uris' => [
'admin/admins' => [
['key' => 'data.mobile', 'type' => 'mobile'],
['key' => 'data.contacts.*.name', 'type' => 'name'],
['key' => 'data.contacts.*.mobile', 'type' => 'mobile'],
],
],
];
```

Custom handler class `Desensitization.php` example:

```php
'desensitiseMobile'`,表示对 `uris` 中 `type=mobile` 的字段调用该函数。
- `class`:全局自定义处理类配置。`name` 为类全路径字符串,`functions` 为类型与类方法的映射。
- 优先级:优先使用 `functions`;若不存在再使用 `class`;若都未配置则不处理并记录 error 日志。
- `uris`:指定需要处理的接口及字段路径和处理类型。系统会解析返回结构并迭代处理,数组使用 `*` 表示。

```php
[
'functions' => [
'mobile' => 'desensitise_mobile',
'name' => 'desensitise_name'
],
'class' => [
'name' => 'App\Lib\Desensitization',
'functions' => [
'mobile' => 'desensitiseMobile',
],
],
'uris' => [
'admin/admins' => [
['key' => 'data.data.*.mobile', 'type' => 'mobile'],
['key' => 'data.data.*.name', 'type' => 'name'],
],
],
];
```

### 使用方法

在 `Kernel.php` 中注册中间件:

```php
protected $routeMiddleware = [
// ...
'desensitization' => \Maxlcoder\LaravelDesensitization\Http\Middleware\Desensitization::class,
];
```

### 示例

#### 1) 处理前返回

```json
{
"code": 200,
"msg": "success",
"data": {
"mobile": "18900000001",
"contacts": [
{
"name": "王组闲",
"mobile": "18900000002"
}
]
}
}
```

#### 2) 配置

修改 `config/laravel-desensitization.php`:

```php
[

],
'class' => [
'name' => 'App\Lib\Desensitization',
'functions' => [
'name' => 'desensitiseRealName',
'mobile' => 'desensitiseMobile'
],
],
'uris' => [
'admin/admins' => [
['key' => 'data.mobile', 'type' => 'mobile'],
['key' => 'data.contacts.*.name', 'type' => 'name'],
['key' => 'data.contacts.*.mobile', 'type' => 'mobile'],
],
],
];
```

自定义处理类 `Desensitization.php` 示例:

```php