关于数据脱敏的两种解决方案之二基于springmvc json处理时候的数据脱敏

原创
2019/09/03 10:18
阅读数 1.4K

接上一篇 关于数据脱敏的两种解决方案之一基于mybatis Interceptor的脱敏

这里只写基于springmvc json处理类 继承alibaba.fastjson

 

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.serializer.ValueFilter;

import cn.he.annotation.DesensitionType;
import cn.he.annotation.Desensitization;

/**
 *     在fastjson中使用此过滤器进行脱敏操作
 *
 * 在springmvc.xml配置文件中增加
 *
 * @author 傻根她弟
 *
 */
public class ValueDesensitizeFilter implements ValueFilter {

    private Logger logger = LoggerFactory.getLogger(ValueDesensitizeFilter.class);

    @Override
    public Object process(Object object, String name, Object value) {
        if (null == value || !(value instanceof String) || ((String) value).length() == 0) {
            return value;
        }
        try {
            Field field = object.getClass().getDeclaredField(name);
            Desensitization desensitization;
            if (String.class != field.getType() || (desensitization = field.getAnnotation(Desensitization.class)) == null) {
                return value;
            }
            List<String> regular;
            DesensitionType type = desensitization.type();
            switch (type) {
                case CUSTOM:
                    regular = Arrays.asList(desensitization.attach());
                    break;
                case TRUNCATE:
                    regular = truncateRender(desensitization.attach());
                    break;
                default:
                    regular = Arrays.asList(type.getRegular());
            }
            if (regular.size() > 1) {
                String match = regular.get(0);
                String result = regular.get(1);
                if (null != match && result != null && match.length() > 0) {
                    return ((String) value).replaceAll(match, result);
                }
            }
        } catch (NoSuchFieldException e) {
            logger.warn("ValueDesensitizeFilter the class {} has no field {}", object.getClass(), name);
        }
//        logger.info("fastJson desenValue:"+value);
        return value;
    }

    private List<String> truncateRender(String[] attachs) {
        List<String> regular = new ArrayList<>();
        if (null != attachs && attachs.length >1) {
            String rule = attachs[0];
            String size = attachs[1];
            String template, result;
            if ("0".equals(rule)) {
                template = "^(\\S{%s})(\\S+)$";
                result = "$1";
            } else if ("1".equals(rule)) {
                template = "^(\\S+)(\\S{%s})$";
                result = "$2";
            } else {
                return regular;
            }
            try {
                if (Integer.parseInt(size) > 0) {
                    regular.add(0, String.format(template, size));
                    regular.add(1, result);
                }
            } catch (Exception e) {
                logger.warn("ValueDesensitizeFilter truncateRender size {} exception", size);
            }
        }
        return regular;
    }
}

 

//===============以下是springmvc配置======================

<!-- 这个filter就是去处理需要脱敏的属性-->

<bean id="valueDesensitizeFilter" class="cn.he.utils.fastjson.ValueDesensitizeFilter"/>
    
    <mvc:annotation-driven>
        <mvc:message-converters register-defaults="true">
            <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
                <property name="supportedMediaTypes">
                    <list>
<!--                        <value>text/html;charset=UTF-8</value>-->
                        <value>application/json</value>
                    </list>
                </property>
                <property name="features">
                    <list>
                        <value>WriteMapNullValue</value>
                        <value>QuoteFieldNames</value>
                        <value>WriteDateUseDateFormat</value>
                        <!-- 禁用fastjson循环引用检测 -->
                        <value>DisableCircularReferenceDetect</value>
                    </list>
                </property>
                <property name="filters" ref="valueDesensitizeFilter"/><!-- 引用脱敏-->
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>
————————————————
版权声明:本文为CSDN博主「傻根她弟」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/godson_2003/article/details/100160810

展开阅读全文
加载中

作者的其它热门文章

打赏
0
0 收藏
分享
打赏
2 评论
0 收藏
0
分享
返回顶部
顶部