栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

spring Aop实现表名脱敏

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

spring Aop实现表名脱敏

针对表的修改和查询,分别采取不同的脱敏方法:

一、配置依赖

pom.xml


  org.aspectj
  aspectjtools
  1.9.7


  org.springframework
  spring-aspects
  5.3.20

二、编写代码

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface EncryptTable {

    
    String tableName();

    
    EncryptType type() default  EncryptType.All;

    
    boolean batch() default false;
}
@Component
public class EncryptTableSwitchConfig {

     private static String writeTables;

     private static String readTables;

     public static boolean getWriteSW(String tableName){

         if (writeTables.contains(","+tableName+",")){
             return true;
         }
         return false;
     }

    public static boolean getReadSW(String tableName){
        if (readTables.contains(","+tableName+",")) {
            return true;
        }
        return false;
    }

    @Value("${encrypt.table.switch.writeTables:}")
    public void setWriteTables(String writeTables){
        EncryptTableSwitchConfig.writeTables = writeTables;
    }

    @Value("${encrypt.table.switch.readTables:}")
    public void setReadTables(){
        EncryptTableSwitchConfig.readTables = readTables;
    }
}
@Component
@Aspect
public class EncryptTableAspect {

    @Value("${encrypt.table:N}")
    private String sw;

    @Before("@annotation(encryptTable)")
    public void before(JoinPoint joinPoint, EncryptTable encryptTable){
        if (!"Y".equals(sw)) {
            return;
        }

        final String tableName = encryptTable.tableName();
        final EncryptType type = encryptTable.type();
        final boolean batch = encryptTable.batch();

        switch (type) {
            case WRITE:
                if (!EncryptTableSwitchConfig.getWriteSW(tableName)){
                    return;
                }
                break;
            case READ:
                if (!EncryptTableSwitchConfig.getReadSW(tableName)) {
                    return;
                }
                break;
            default:
                if (!EncryptTableSwitchConfig.getReadSW(tableName)
                    && !EncryptTableSwitchConfig.getWriteSW(tableName)) {
                    return;
                }
        }
        if (batch) {
            Switch.batch();
        } else {
            Switch.selected();
        }
    }

    @Value("${encrypt.table.method:,updateUser,insertUser,}")
    private String netsMethod;

    @Pointcut("target(com.xxx.mapper.UserMapper)")
    private void netsUser(){

    }

    
    @Before("netsUser()")
    public void before(JoinPoint jp){
        MethodSignature signature = (MethodSignature)jp.getSignature();
        final Method method = signature.getMethod();
        if (netsMethod.contains(","+method+",") && EncryptTableSwitchConfig.getWriteSW("tab_user")){
            Switc.selected();
        }

    }

}
public enum EncryptType {

    WRITE, READ, All;
}

xxxMapper.java

@EncryptTable(tableName="tab_user", type=EncryptType.READ, batch=false)
List getUsersByIds(@param(ids) List ids);

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/978945.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号