JAVA 后端返回数据过滤不需要的字段 萌新记录

之前做项目的时候,基本都是查到一个对象或者一个集合就抛给前端,也没注意过敏感数据泄露的问题,最近经人提醒,开始考虑怎么解决。

这里贴一篇很不错的博文

java接口数据json过滤字段方法整理

但是项目用的是fastjson,按照博文方法过滤的话有点麻烦,并且我的返回值是经过包装的JSONObject,会带上status、message等信息,并且过滤字段不确定,可能这个接口需要过滤它,另一个接口又必须使用它,所以博文里的方法不适合我的项目。于是又疯狂百度,终于找到了一个感觉还不错的

SimplePropertyPreFilter filter = new SimplePropertyPreFilter(TTown.class, "id","townname");  
String result = JSONObject.toJSONString(townList,filter);   

马上Copy到项目中尝试,结果发现这个过滤是把需要的字段名填里面,可一般都是需要的比较多,那有没有办法过滤不需要的呢?

当时继续百度,但是没找到什么特别好的办法,突然脑袋灵光一闪,对着SimplePropertyPreFilter按了Ctrl+鼠标左键,发现它的构造方法是将传入的参数add进includes(一眼看出是包含的意思)集合中的

于是把里面的代码Copy出来,自定义一个类,将includes换成excludes,测试,成功~~

这样,只要我返回的时候带上过滤字段的类 以及 需过滤的字段名就行了。

贴上代码

package com.alibaba.fastjson.serializer;

import java.util.HashSet;
import java.util.Set;

/*
 * Copyright 1999-2101 Alibaba Group.
 *
 * 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.
 */
public class SimplePropertyPreFilter implements PropertyPreFilter {

    private final Class<?>    clazz;
    private final Set<String> includes = new HashSet<String>();
    private final Set<String> excludes = new HashSet<String>();

    public SimplePropertyPreFilter(String... properties){
        this(null, properties);
    }

    public SimplePropertyPreFilter(Class<?> clazz, String... properties){
        super();
        this.clazz = clazz;
        for (String item : properties) {
            if (item != null) {
                this.includes.add(item);
            }
        }
    }

    public Class<?> getClazz() {
        return clazz;
    }

    public Set<String> getIncludes() {
        return includes;
    }

    public Set<String> getExcludes() {
        return excludes;
    }

    public boolean apply(JSONSerializer serializer, Object source, String name) {
        if (source == null) {
            return true;
        }

        if (clazz != null && !clazz.isInstance(source)) {
            return true;
        }

        if (this.excludes.contains(name)) {
            return false;
        }

        if (includes.size() == 0 || includes.contains(name)) {
            return true;
        }

        return false;
    }

}

可用到后来,发现这样还是无法满足项目需求,当我返回一个map,map里面有几个不同的对象时,就无法转换了。当时想尽各种方法在原来的代码基础上做修改,发现越改越复杂,而且很难实现。偶然之间,我看到了这个

就想试试如果不传指定类是种什么样的结果

测试后发现,如果不传指定类的话,只要你写的字段名是json里面有的,它就会给你过滤掉,这是符合我项目需求的,于是就这么用上了。

然后在这里发一篇博文当作笔记,我是萌新,如果这个方法有什么缺陷,或者有什么更加好的办法,麻烦路过的大神多多指教。

猜你喜欢

转载自blog.csdn.net/weixin_42612454/article/details/84636042