3 Star 13 Fork 7

eternalstone / SensitiveBye

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
Apache-2.0

SensitiveBye

MavenCentral Hex.pm

1、简介

一款专注于解决数据脱敏的Java工具包, 能帮助您快速解决项目中的脱敏需求,支持对接口字段、java对象字段和json序列化字段脱敏;常见日志框架(logback,log4j2)输出内容脱敏;基于mybatis拦截器实现的数据库脱敏;敏感词条、Spring配置文件等内容进行自定义格式数据脱敏,使用简单方便、易于扩展。详细开发文档


2、功能概述

  1. java版本基准:jdk1.8

  2. 支持Restful接口字段脱敏,java对象字段脱敏,支持jackson和fastjson序列化字段脱敏

  3. 支持基于mybatis的数据库字段加解密脱敏

  4. 支持常用日志框架输出脱敏,例如logback,log4j2

  5. 支持SpringBoot配置文件配置项脱敏

  6. 内置基于 AhoCorasickDoubleArrayTrie 实现的敏感词库


3、使用

3.1 导入

3.1.1 SpringBoot项目导入
<dependency>
  <groupId>io.github.eternalstone</groupId>
  <artifactId>sensitivebye-spring-boot-starter</artifactId>
  <version>1.0.4</version>
</dependency>
3.1.2 SpringMVC或其他java项目带入
<dependency>
  <groupId>io.github.eternalstone</groupId>
  <artifactId>sensitivebye-core</artifactId>
  <version>1.0.4</version>
</dependency>

​ 包导不下来需要添加以下maven中央仓库:

<repositories>
   <repository>
      <id>maven-central</id>
      <name>Central Repository</name>
      <url>https://repo1.maven.apache.org/maven2</url>
   </repository>
</repositories>

3.2 配置

​ 在SpringBoot项目中,在Application启动类上面加入@EnableGlobalSensitiveBye注解用来开启SensitiveBye自动装配。@EnableGlobalSensitiveBye注解可视为SensitiveBye所有功能是否生效的总开关。

@EnableGlobalSensitiveBye
@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

​ SensitiveBye集成了以下配置

sensitive-bye:
  field:
    enabled: true #默认为true, 开启字段脱敏开关
  log:
    enabled: false #默认为false, 开启日志脱敏开关
  mybatis:
    enabled: false #默认为false, 开启mybatis数据库脱敏开关

​ 当开启对应开关时,需要导入相关的依赖,例如,开启log开关需要依赖logback或者log4j2相关的maven坐标,开启mybatis开关需要依赖mybatis或者基于mybatis开发的框架的maven坐标。

3.3 字段脱敏

SensitiveBye字段脱敏的组件是SensitiveFieldProvider,SpringBoot引入starter包配合@EnableGlobalSensitiveBye注解将此组件自动注入,其他java项目引入core包则需要初始化此组件:

@Bean
public SensitiveFieldProvider sensitiveFieldProvider(){
    return SensitiveFieldProvider.instance();
}

​ 在需要脱敏java对象字段上注解@SensitiveBye,填入对应的脱敏规则即可:

@SensitiveBye(strategy = SensitiveType.MOBILE)
private String mobile;
3.3.1 接口字段脱敏

​ SpringMVC的接口序列化是基于jackson实现的,SensitiveBye已完成对jackson序列化的脱敏,所有进行以上配置后接口字段即可自动脱敏。

3.3.2 json序列化脱敏
  • jackson序列化脱敏

    ObjectMapper mapper = new ObjectMapper();
    LOGGER.info("jackson序列化脱敏:{}", mapper.writeValueAsString(user));
  • fastjson序列化脱敏

     //fastjson序列化, 需要添加一个fastjson的值过滤器,SensitiveBye已经内置实现了SensitiveByeFilter
    LOGGER.info("fastjson序列化脱敏:{}", JSONObject.toJSONString(user, SensitiveByeFilter.instance()));	
3.3.3 java对象脱敏
SensitiveFieldProvider.instance().handle(SensitiveType.MOBILE, "13100001111", "*")
3.3.4 自定义字段脱敏策略

​ Spring项目的自定义字段脱敏策略可以直接Bean一个CustomeFieldStrategy对象:

@Bean
public CustomeFieldStrategy customeFieldStrategy(){
    CustomeFieldStrategy strategy = new CustomeFieldStrategy();
    //自定义策略key=test, var1表示原始值,var2表示脱敏符号, 后面的表达式即是自定义脱敏逻辑
    strategy.add("test", (var1, var2)-> var1.concat(var2));
    return strategy;
}

​ 其他java项目需要给SensitiveFieldProvider设置自定义策略:

CustomeFieldStrategy strategy = new CustomeFieldStrategy();
strategy.add("test", (var1, var2)-> var1.concat(var2));
SensitiveFieldProvider instance = SensitiveFieldProvider.instance();
instance.setCustomeStrategy(strategy);

​ 添加的'test'自定义策略直接在注解中使用即可:@SensitiveBye("test")

3.4 日志脱敏

SensitiveBye日志脱敏的组件是SensitiveLogProvider,SpringBoot项目配置sensitive-bye.log.enabled=true自动注入此组件,其他java项目需要初始化此组件:

@Bean
public SensitiveLogProvider sensitiveFieldProvider(){
    SensitiveLogProvider sensitiveLogProvider = SensitiveLogProvider.instance();
    //如果存在自定义策略,可以设置一个SensitiveRule对象
    sensitiveLogProvider.setSensitiveRule();
    return sensitiveLogProvider
}
3.4.1 logback日志脱敏

​ 在logback.xml中添加如下配置即可:

<conversionRule conversionWord="msg" converterClass ="LogbackSensitiveConverter"/>
3.4.2 log4j2日志脱敏

​ 在log4j2-spring.xml中,原日志内容格式为 %msg,需要将其替换为%sdmsg。例如:

<appenders>
  <console name="STDOUT" target="SYSTEM_OUT">
    <patternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level ---- [%thread] %logger Line:%-3L - %sdmsg%n" />
  </console>
</appenders>
3.4.3 自定义日志脱敏规则

​ SensitiveBye集成的默认日志脱敏规则见枚举类:LoggerRule。

​ 如需添加或删除或自定义脱敏规则,实现ISensitiveLogRule接口的custome(Map<String, SensitiveLogRuleWrapper> ruleMap)方法即可,例如:

@Component
public class CustomeLogRule implements ISensitiveLogRule {
  @Override
  public void custome(Map<String, SensitiveLogRuleWrapper> ruleMap) {
    SensitiveLogRuleWrapper wrapper = new SensitiveLogRuleWrapper();
    //规则名称
    wrapper.setName("wechat");
    //规则前缀匹配词
    wrapper.setKeys(new HashSet<String>(){{
      add("微信");
      add("wechat");
    }});
    //规则匹配词与匹配值之间的分隔符
    wrapper.setSeparators(new HashSet<String>(){{
      add("=");
      add(":");
      add("\\[");
    }});
    //正则表达式
    wrapper.setPattern(Pattern.compile("([a-zA-Z]{1})([-_a-zA-Z0-9]{5,19}+$)"));
    //替换表达式,注意需要带上匹配词和分隔符的占位符 $1表示keys, $2表示分隔符,后续就是对内容的拆分和替换
    wrapper.setReplacement("$1$2$3*******");
    //新增规则
    ruleMap.put(wrapper.getName(), wrapper);
    //或者移除默认规则
    ruleMap.remove(LoggerRule.BANK_CARD.name().toLowerCase());
  }
}

3.5 基于mybatis拦截器的数据库字段脱敏

SensitiveBye的mybatis脱敏组件是MybatisSensitiveInterceptor,它是基于Mybatis拦截器实现的。SpringBoot项目配置sensitive-bye.mybatis.enabled=true自动注入此组件,其他java项目需要初始化此组件:

@Bean
public MybatisSensitiveInterceptor mybatisSensitiveInterceptor() {
	return new MybatisSensitiveInterceptor();
}

​ mybatis数据库字段脱敏用到了两个核心注解@EnableCipher@CipherField:

//@EnableCipher作用于Mapper接口的方法上,标注入参是加密还是解密,返回值是加密还是解密
@Mapper
public interface UserMapper {
    @EnableCipher(parameter = CipherType.ENCRYPT)
    int insertAndReturnId(User user);
    
    @EnableCipher(result = CipherType.DECRYPT)
    User selectById(@Param("id") Integer id);
}

//@CipherField作用于对象字段上,标注此字段需要加解密,并且指定加解密算法,加解密算法需要实现ICipherAlgorithm接口
public class User
    @CipherField(PasswordAlgorithm.class)
    private String password;
	@CipherField(MobileAlgorithm.class)
    private String mobile;
}

​ 1.@SensitiveBye注解和@CipherField注解虽然都是标注在对象属性上的,但是两个注解的作用互不影响,可以叠加使用,例如手机号从数据库密文查出来解密成明文,再用@SensitiveBye(strategy = SensitiveType.MOBILE)将明文手机号打上掩码。

​ 2.如果项目中存在多个Mybatis拦截器,需要指定拦截器的执行顺序,可以写个配置类:

@Configuration
public class MybatisConfig {
    @Bean
    public ConfigurationCustomizer mybatisConfigurationCustomizer() {
       return new ConfigurationCustomizer() {
           @Override
           public void customize(Configuration configuration) {
                configuration.addInterceptor(new MybatisInterceptor());
           }
       };
    }
}

3.6 其他工具使用

3.6.1 敏感词库组件

SensitiveBye的敏感词组件是SensitiveWordProvider,默认不自动注入,需要使用的时候初始化即可:

@Bean
public SensitiveWordProvider sensitiveWordProvider(){
    return new SensitiveWordProvider();
}

​ SensitiveWordProvider提供了一个有参构造器,用于以不同的方式获取词库,SensitiveBye内置了两种方式:

  • SensitiveWordSourceFromResource (获取resource目录下的sensitive.txt文件, 可自定义文件名)
  • SensitiveWordSourceFromUrl(传入一个url,从网络获取词库文件)

​ 你可以通过实现ISensitiveWordSource接口的loadSource()自定义获取词库的方式。

​ SensitiveWordProvider提供了三个方法:

//handle方法用于将传入的字符串中的敏感词替换成输入的符号
String handle(String word, String symbol);
//contain方法用于检测传入的字符串中包含的敏感词组
List<String> contain(String word);
//reload方法用于重新载入词库
void reload();
3.6.2 SpringBoot配置文件静态脱敏工具类

SensitiveBye实现了对SpringBoot的配置文件相关的配置项进行打掩码的工具SensitiveFileUtil, 支持对yml, yaml, properties三种配置文件,它提供了以下几个方法:

//将source路径的配置文件进行配置项脱敏后输出到target目录
public static void sensitiveByeToFile(String source, String target);

//将source路径的配置文件进行配置项脱敏后输出到target目录,可传入handler自定义实现对配置项自定义操作
public static void sensitiveByeToFile(String source, String target, IFileHandler handler);

//将source路径的配置文件进行配置项脱敏后输出成字符串
public static String sensitiveByeToString(String source);

//将source路径的配置文件进行配置项脱敏后输出成字符串,可传入handler自定义实现对配置项自定义操作
public static String sensitiveByeToString(String source, IFileHandler handler);

​ SensitiveFileUtil对配置项脱敏的处理器是SensitiveFileHandler,它是默认的实现,你可以继承AbstractFileHandler类实现doFilter()对配置项进行操作:

public class SensitiveCustomeFilterHandler extends AbstractFileHandler {
    @Override
    public void doFilter(LinkedHashMap<String, Object> param) {
        //删除test配置项
        param.remove("test");
    }
}

​ 你可以将自定义的handler加入SensitiveFileHandler的后续执行链中,也可以直接传递自定义handler跳过SensitiveBye的SensitiveFileHandler的实现

SensitiveFileHandler handler = new SensitiveFileHandler();
handler.setNextHandler(new SensitiveCustomeFilterHandler());
String s2 = SensitiveFileUtil.sensitiveByeToString(source, handler);


4.引文

https://pagehelper.github.io/docs/interceptor/


联系方式

  1. 邮箱联系: senstivebye@163.com,欢迎通过此邮件讨论与SensitiveBye相关的一切。
Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright SensitiveBye eternalstone Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

简介

SensitiveBye是一款专注于解决数据脱敏的Java和SpringBoot工具包, 能帮助您快速解决项目中的脱敏需求,支持对象字段,接口字段,数据库字段脱敏,json序列化脱敏,日志打印脱敏、敏感词条脱敏、Spring配置文件脱敏等功能 展开 收起
Java
Apache-2.0
取消

贡献者

全部

近期动态

加载更多
不能加载更多了
Java
1
https://gitee.com/eternalstone/SensitiveBye.git
git@gitee.com:eternalstone/SensitiveBye.git
eternalstone
SensitiveBye
SensitiveBye
master

搜索帮助