Skip to content

kkwwang/sensitive

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

79 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

OSCS Status

说明

项目依赖springMVC仅对RestController接口有效,即返回JSON格式的接口
使用说明及demo仅编写springboot,使用springmvc框架请参考配置
支持springboot版本1.4.0.RELEASE-2.7.0
使用springmvc需自行注册bean,版本支持请自行验证

插件已推送至maven中央库

<dependency>
   <groupId>com.kiilin</groupId>
   <artifactId>sensitive-spring-boot-starter</artifactId>
   <version>latest_version</version>
</dependency>

使用方法

  • 添加AOP依赖
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-aop</artifactId>
    </dependency>

- 启动类添加启用注解

package com.kiilin.sensitive.demo;

import com.kiilin.sensitive.core.annotation.EnableSensitive;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@EnableSensitive
public class DemoApplication {
      public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}
  • 增加自动装配,默认启用脱敏,增加系统环境变量SENSITIVE_ENABLE=false或springboot配置sensitive.enable=false关闭

  • 在需要脱敏的实体的字段上添加注解

// 使用自定义规则
@SensitiveInfo(pattern = "(.{2})(.*)", targetChar = "$1**")
// 使用预定义规则
@SensitiveInfo(SensitiveType.EMAIL)
// 若两种规则混用,自定义规则优先级更高
  • 在需要脱敏的Controller方法或类上添加@Sensitive注解,方法上表示该接口需要脱敏,类表示该类所有方法均需脱敏
POST http://localhost:8080/test
Content-Type: application/json
sensitive-placeholder: (^_^) // 增加隐藏小彩蛋;请求头中携带sensitive-placeholder参数可以替换默认的*占位符

{
  "email": "kiilin@kiilin.com",
  "idCard": "6222222222222222",
  "name": "云声",
  "phone": "18888888888",
  "username": "kiilin"
}

### 
http://localhost:8080/test

HTTP/1.1 200 
Content-Type: application/json
Transfer-Encoding: chunked
Date: Wed, 02 Nov 2022 03:31:36 GMT
Keep-Alive: timeout=60
Connection: keep-alive

{
  "email": "kii(^_^)(^_^)(^_^)@kiilin.com",
  "name": "云(^_^)",
  "username": "ki(^_^)(^_^)",
  "phone": "188(^_^)(^_^)(^_^)(^_^)8888",
  "idCard": "622(^_^)(^_^)(^_^)(^_^)(^_^)(^_^)(^_^)(^_^)(^_^)2222"
}

Response code: 200; Time: 3ms (3 ms); Content length: 185 bytes (185 B)

测试地址
在线测试地址