Skip to content

Commit

Permalink
[Feature] add for new
Browse files Browse the repository at this point in the history
  • Loading branch information
houbb committed Feb 2, 2020
1 parent 0cca260 commit c42e2a9
Show file tree
Hide file tree
Showing 14 changed files with 408 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language: java
jdk:
- oraclejdk8
- openjdk8
install: mvn install -DskipTests=true -Dmaven.javadoc.skip=true
script: mvn test
after_success:
Expand Down
51 changes: 48 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

用户也可以基于自己的实际需要,自定义注解。

[![Build Status](https://travis-ci.com/houbb/sensitive.svg?branch=master)](https://travis-ci.com/houbb/sensitive)
[![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.github.houbb/sensitive/badge.svg)](http://mvnrepository.com/artifact/com.github.houbb/sensitive)

> [变更日志](doc/CHANGE_LOG.md)
Expand Down Expand Up @@ -44,9 +45,9 @@

6. 支持基于 FastJSON 直接生成脱敏后的 json

## v0.0.8 新特性
## v0.0.9 变更

添加类反射 cache,初步提升性能。
新增脱敏引导类

# 快速开始

Expand All @@ -62,10 +63,21 @@ Maven 3.x
<dependency>
<groupId>com.github.houbb</groupId>
<artifactId>sensitive-core</artifactId>
<version>0.0.8</version>
<version>0.0.9</version>
</dependency>
```

## 核心 api 简介

`SensitiveUtil` 工具类的核心方法列表如下:

| 序号 | 方法 | 参数 | 结果 | 说明 |
|:---|:---|:---|:---|:---|
| 1 | desCopy() | 目标对象 | 深度拷贝脱敏对象 | 适应性更强 |
| 2 | desJson() | 目标对象 | 脱敏对象 json | 性能较好 |
| 3 | desCopyCollection() | 目标对象集合 | 深度拷贝脱敏对象集合 | |
| 4 | desJsonCollection() | 目标对象集合 | 脱敏对象 json 集合 | |

## 定义对象

- User.java
Expand Down Expand Up @@ -705,6 +717,39 @@ List<String> sensitiveJsonList = SensitiveUtil.desJsonCollection(userList);
Assert.assertEquals("[{\"email\":\"123**@qq.com\",\"idCard\":\"123456**********34\",\"phone\":\"188****8888\",\"username\":\"脱*君\"}, {\"email\":\"123**@qq.com\",\"idCard\":\"123456**********34\",\"phone\":\"188****8888\",\"username\":\"集**试\"}]", sensitiveJsonList.toString());
```

# 脱敏引导类

为了配置的灵活性,引入了引导类。

## 核心 api 简介

`SensitiveBs` 引导类的核心方法列表如下:

| 序号 | 方法 | 参数 | 结果 | 说明 |
|:---|:---|:---|:---|:---|
| 1 | desCopy() | 目标对象 | 深度拷贝脱敏对象 | 适应性更强 |
| 2 | desJson() | 目标对象 | 脱敏对象 json | 性能较好 |

## 使用示例

使用方式和工具类一致,示意如下:

```java
SensitiveBs.newInstance().desCopy(user);
```

## 配置深度拷贝实现

默认的使用 FastJson 进行对象的深度拷贝,等价于:

```java
SensitiveBs.newInstance()
.deepCopy(DeepCopies.json())
.desJson(user);
```

后期准备引入其他深度拷贝替代基于 json 的深度拷贝,提升性能。

# 需求 & BUGS

> [issues](https://github.com/houbb/sensitive/issues)
Expand Down
9 changes: 8 additions & 1 deletion doc/CHANGE_LOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,11 @@

| 序号 | 变更类型 | 说明 | 时间 | 备注 |
|:---|:---|:---|:---|:--|
| 1 | O | 添加类字段反射 cache | 2019-12-24 20:07:45 | 初步优化性能 |
| 1 | O | 添加类字段反射 cache | 2019-12-24 20:07:45 | 初步优化性能 |

# release_0.0.9

| 序号 | 变更类型 | 说明 | 时间 | 备注 |
|:---|:---|:---|:---|:--|
| 1 | A | 新增 Bs 引导类对应的深度拷贝 | 2020-2-2 18:18:50 | 文档优化 |
| 2 | P | 新增性能 benchmark | 2020-2-2 18:18:50 | |
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.github.houbb.sensitive.api;

/**
* 深度拷贝接口
* @author binbin.hou
* @since 0.0.9
* @param <T> 泛型
*/
public interface IDeepCopy {

/**
* 深度拷贝
* @param object 原始对象
* @return 结果
* @since 0.0.9
*/
<T> T deepCopy(T object);

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,19 @@ public interface ISensitive<T> {
* 1. 为什么这么设计?
* 不能因为脱敏,就导致代码中的对象被改变。否则代码逻辑会出现问题。
* @param object 原始对象
* @param config 配置信息
* @return 脱敏后的新对象
*/
T desCopy(final T object);
T desCopy(final T object, final ISensitiveConfig config);

/**
* 返回脱敏后的 json
* 1. 避免 desCopy 造成的对象新建的性能浪费
* @param object 对象
* @param config 配置信息
* @return json
* @since 0.0.6
*/
String desJson(final T object);
String desJson(final T object, final ISensitiveConfig config);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.github.houbb.sensitive.api;

/**
* 脱敏配置接口
* @author binbin.hou
* @since 0.0.9
*/
public interface ISensitiveConfig {

/**
* 深度拷贝
* @return 深度拷贝
* @since 0.0.9
*/
IDeepCopy deepCopy();

}

Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@
import com.github.houbb.sensitive.annotation.SensitiveEntry;
import com.github.houbb.sensitive.annotation.metadata.SensitiveCondition;
import com.github.houbb.sensitive.annotation.metadata.SensitiveStrategy;
import com.github.houbb.sensitive.api.ICondition;
import com.github.houbb.sensitive.api.ISensitive;
import com.github.houbb.sensitive.api.IStrategy;
import com.github.houbb.sensitive.api.*;
import com.github.houbb.sensitive.api.impl.SensitiveStrategyBuiltIn;
import com.github.houbb.sensitive.core.api.context.SensitiveContext;
import com.github.houbb.sensitive.core.exception.SensitiveRuntimeException;
Expand Down Expand Up @@ -44,21 +42,22 @@ public class SensitiveService<T> implements ISensitive<T> {

@Override
@SuppressWarnings({"unchecked", "rawtypes"})
public T desCopy(T object) {
public T desCopy(T object, final ISensitiveConfig config) {
//1. 初始化对象
final Class clazz = object.getClass();
final SensitiveContext context = new SensitiveContext();

//2. 深度复制对象
final T copyObject = BeanUtil.deepCopy(object);
final IDeepCopy deepCopy = config.deepCopy();
final T copyObject = deepCopy.deepCopy(object);

//3. 处理
handleClassField(context, copyObject, clazz);
return copyObject;
}

@Override
public String desJson(T object) {
public String desJson(final T object, final ISensitiveConfig config) {
if(ObjectUtil.isNull(object)) {
return JSON.toJSONString(object);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.github.houbb.sensitive.core.api;

import com.github.houbb.heaven.support.instance.impl.Instances;
import com.github.houbb.heaven.util.guava.Guavas;
import com.github.houbb.heaven.util.util.CollectionUtil;
import com.github.houbb.sensitive.core.bs.SensitiveBs;

import java.util.Collection;
import java.util.List;
Expand All @@ -29,8 +29,7 @@ private SensitiveUtil(){}
*/
@SuppressWarnings({"unchecked", "rawtypes"})
public static <T> T desCopy(T object) {
return (T) Instances.singleton(SensitiveService.class)
.desCopy(object);
return SensitiveBs.newInstance().desCopy(object);
}

/**
Expand All @@ -42,8 +41,7 @@ public static <T> T desCopy(T object) {
*/
@SuppressWarnings({"unchecked", "rawtypes"})
public static String desJson(Object object) {
return Instances.singleton(SensitiveService.class)
.desJson(object);
return SensitiveBs.newInstance().desJson(object);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package com.github.houbb.sensitive.core.bs;

import com.github.houbb.heaven.support.instance.impl.Instances;
import com.github.houbb.heaven.util.common.ArgUtil;
import com.github.houbb.sensitive.api.IDeepCopy;
import com.github.houbb.sensitive.api.ISensitive;
import com.github.houbb.sensitive.api.ISensitiveConfig;
import com.github.houbb.sensitive.core.api.SensitiveService;
import com.github.houbb.sensitive.core.support.config.DefaultSensitiveConfig;
import com.github.houbb.sensitive.core.support.deepcopy.FastJsonDeepCopy;

/**
* 脱敏引导类
* @author binbin.hou
* @since 0.0.9
*/
public final class SensitiveBs {

private SensitiveBs(){}

/**
* 深度拷贝
* @since 0.0.9
*/
private IDeepCopy deepCopy = Instances.singleton(FastJsonDeepCopy.class);

/**
* 脱敏实现
* @since 0.0.9
*/
private ISensitive sensitive = Instances.singleton(SensitiveService.class);

/**
* 新建实例
* @since 0.0.9
* @return 引导类实例
*/
public static SensitiveBs newInstance() {
return new SensitiveBs();
}

/**
* 设置深度拷贝实现
* @param deepCopy 深度拷贝实现类
* @return this
* @since 0.0.9
*/
public SensitiveBs deepCopy(IDeepCopy deepCopy) {
ArgUtil.notNull(deepCopy, "deepCopy");

this.deepCopy = deepCopy;
return this;
}

/**
* 脱敏对象
*
* 每次都创建一个新的对象,避免线程问题
* 可以使用 {@link ThreadLocal} 简单优化。
* @param object 原始对象
* @param <T> 泛型
* @return 脱敏后的对象
* @since 0.0.4 以前用的是单例。建议使用 spring 等容器管理 ISensitive 实现。
*/
@SuppressWarnings({"unchecked", "rawtypes"})
public <T> T desCopy(T object) {
ISensitiveConfig config = buildConfig();
return (T) sensitive.desCopy(object, config);
}

/**
* 返回脱敏后的对象 json
* null 对象,返回字符串 "null"
* @param object 对象
* @return 结果 json
* @since 0.0.9
*/
@SuppressWarnings({"unchecked", "rawtypes"})
public String desJson(Object object) {
ISensitiveConfig config = buildConfig();
return sensitive.desJson(object, config);
}

/**
* 构建上下文
* @return 配置
* @since 0.0.9
*/
private ISensitiveConfig buildConfig() {
return DefaultSensitiveConfig.newInstance()
.deepCopy(deepCopy);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.github.houbb.sensitive.core.support.config;

import com.github.houbb.sensitive.api.IDeepCopy;
import com.github.houbb.sensitive.api.ISensitiveConfig;

/**
* 默认脱敏配置实现
* @author binbin.hou
* @since 0.0.9
*/
public class DefaultSensitiveConfig implements ISensitiveConfig {

/**
* 深度拷贝实现
* @since 0.0.9
*/
private IDeepCopy deepCopy;

/**
* 新建对象实例
* @since 0.0.9
* @return 实例
*/
public static DefaultSensitiveConfig newInstance() {
return new DefaultSensitiveConfig();
}

public DefaultSensitiveConfig deepCopy(IDeepCopy deepCopy) {
this.deepCopy = deepCopy;
return this;
}

@Override
public IDeepCopy deepCopy() {
return deepCopy;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.github.houbb.sensitive.core.support.deepcopy;

import com.github.houbb.heaven.support.instance.impl.Instances;
import com.github.houbb.sensitive.api.IDeepCopy;
import com.github.houbb.sensitive.core.util.BeanUtil;

/**
* 深度拷贝工具类
* @author binbin.hou
* @since 0.0.9
*/
public final class DeepCopies {

/**
* 基于 json 的深度拷贝
* @return 深度拷贝实现
* @since 0.0.9
*/
public static IDeepCopy json() {
return Instances.singleton(FastJsonDeepCopy.class);
}

}
Loading

0 comments on commit c42e2a9

Please sign in to comment.