🎨 代码完善

This commit is contained in:
李寻欢 2022-08-26 10:50:38 +08:00
parent 5f650fe975
commit b0e2a0086a
3 changed files with 34 additions and 5 deletions

View File

@ -1,9 +1,12 @@
## 脱敏 ## 脱敏 Desensitization
### 作用 ### 作用 Effect
利用反射处理结构体,处理带`sen`标签的字段(规则: `规则名称,占位符`,如:`phone,*`),将其脱敏。支持自定义处理函数 利用反射处理结构体,处理带`sen`标签的字段(规则: `规则名称,占位符`,如:`phone,*`),将其脱敏。支持自定义处理函数
### 定义结构体示例 Use reflection to process the structure, process the fields with the `sen` tag (rule: `rule name, placeholder`, such as: `phone,*`), and desensitize it. Support custom handlers
### 定义结构体示例 Example
```go ```go
package main package main
@ -16,17 +19,30 @@ type User struct {
Name string `json:"name"` Name string `json:"name"`
Age int `json:"age"` Age int `json:"age"`
Phone string `json:"phone" sen:"phone,*"` Phone string `json:"phone" sen:"phone,*"`
Email string `json:"email" sen:"email,*"`
} }
data := User{ data := User{
Name: "lixh", Name: "lixh",
Age: 18, Age: 18,
Phone: "13888888888", Phone: "13888888888",
Email: "lixh@gmail.com",
} }
// 添加自定义处理函数
// Add custom handler
sensitive.AddHandler("email", func(src, p string) string {
// 将@符号后面的替换为*
// Replace the after @ sign with *
idx := strings.Index(src, "@")
dst := src[:idx+1] + strings.Repeat(p, utf8.RuneCountInString(src)-idx-1)
return dst
})
if err := sensitive.Desensitize(data); err != nil { if err := sensitive.Desensitize(data); err != nil {
fmt.Println(err) fmt.Println(err)
} }
bs, _ := json.Marshal(response) bs, _ := json.Marshal(response)
log.Printf("脱敏后的数据: %v", string(bs)) log.Printf("after processing data: %v", string(bs))
``` ```

View File

@ -1,6 +1,6 @@
package sensitive package sensitive
import "sensitive/handle" import "github.com/lixh00/sensitive/handle"
// RuleHandler 脱敏规则处理接口 // RuleHandler 脱敏规则处理接口
type RuleHandler func(src, placeholder string) (dst string) type RuleHandler func(src, placeholder string) (dst string)

View File

@ -4,7 +4,9 @@ import (
"encoding/json" "encoding/json"
"log" "log"
"net/http" "net/http"
"strings"
"testing" "testing"
"unicode/utf8"
) )
// Response 返回值 // Response 返回值
@ -29,6 +31,7 @@ type Data struct {
Name string `json:"name"` Name string `json:"name"`
Phone string `json:"phone" sen:"phone,*"` // 使用脱敏标签,规则为手机号,替换占位字符为* Phone string `json:"phone" sen:"phone,*"` // 使用脱敏标签,规则为手机号,替换占位字符为*
IdNumber string `json:"idNumber" sen:"idNumber,#"` // 使用脱敏标签,规则为身份证号码,替换占位字符为* IdNumber string `json:"idNumber" sen:"idNumber,#"` // 使用脱敏标签,规则为身份证号码,替换占位字符为*
Email string `json:"email" sen:"email,*"` // 使用脱敏标签,规则为邮箱,替换占位字符为*
} }
// 模拟测试一下 // 模拟测试一下
@ -38,11 +41,13 @@ func TestDeal(t *testing.T) {
Name: "张三", Name: "张三",
Phone: "13800138000", Phone: "13800138000",
IdNumber: "420102199010101010", IdNumber: "420102199010101010",
Email: "zhangsan@gmail.com",
}, { }, {
Id: "234", Id: "234",
Name: "李四", Name: "李四",
Phone: "13800138001", Phone: "13800138001",
IdNumber: "420102199010101011", IdNumber: "420102199010101011",
Email: "lisi@gmail.com",
}} }}
pageData := PageData{ pageData := PageData{
@ -69,6 +74,14 @@ func TestDeal(t *testing.T) {
//bs, _ = json.Marshal(response) //bs, _ = json.Marshal(response)
//log.Printf("假设是管理员,需要跳过处理,脱敏后的数据: %v", string(bs)) //log.Printf("假设是管理员,需要跳过处理,脱敏后的数据: %v", string(bs))
AddHandler("email", func(src, p string) string {
// 将@符号后面的替换为*
idx := strings.Index(src, "@")
dst := src[:idx+1] + strings.Repeat(p, utf8.RuneCountInString(src)-idx-1)
return dst
})
if err := Desensitization(response, false); err != nil { if err := Desensitization(response, false); err != nil {
log.Println(err) log.Println(err)
} }