复杂业务场景下的规则与数据碰撞(可用于漏洞规则匹配/复杂条件判断)

# 场景描述
直接看代码,可能不知所云,我先铺垫一下

复杂业务场景下,经常会有复杂条件判断,比如多个and或者or的条件组合,可以采用如下这种方式

我这里是需要获取到所有不匹配的数据,如果不需要就把注释掉的跳出代码放开即可



public static void main(String[] args) throws IOException {
        String rule = "{ " +
                " \"condition\": \"and\", " +
                " \"children\": [{ " +
                " \"condition\": \">=\", " +
                " \"name\": \"xueli\", " +
                " \"value\": \"3\" " +
                " }, { " +
                " \"condition\": \"=\", " +
                " \"name\": \"zhiji\", " +
                " \"value\": \"6\" " +
                " }, { " +
                " \"condition\": \">=\", " +
                " \"name\": \"gongzuonianxian\", " +
                " \"value\": \"1\" " +
                " }, { " +
                " \"condition\": \"or\", " +
                " \"children\": [{ " +
                "  \"condition\": \">=\", " +
                "  \"name\": \"zhicheng\", " +
                "  \"value\": \"2\" " +
                " }, { " +
                "  \"condition\": \">=\", " +
                "  \"name\": \"jinengjianding\", " +
                "  \"value\": \"2\" " +
                " }] " +
                " }] " +
                "}";

        JSONObject rules = JSONObject.parseObject(rule);
        Map<String, Object> data = new HashMap<>();
        data.put("xueli","3");
        data.put("zhiji","6");
        data.put("gongzuonianxian","1");
        data.put("zhicheng","2");
        data.put("jinengjianding","2");
        Boolean status = makeData(rules, data);
        System.out.println("============>" + status);
    }

// 1、示意方法
    public static Boolean makeData(JSONObject rule, Map<String, Object> data) {
        String condition = rule.getString("condition");
        JSONArray children = rule.getJSONArray("children");
        boolean result;

        if ("and".equals(condition)) {
            result = true;
            for (int i = 0; i < children.size(); i++) {
                result &= makeData(children.getJSONObject(i), data);
                /*if (!result) {
                    break;
                }*/
            }
        } else if ("or".equals(condition)) {
            result = false;
            for (int i = 0; i < children.size(); i++) {
                result |= makeData(children.getJSONObject(i), data);
                /*if (result) {
                    break;
                }*/
            }
        } else {
            String name = rule.getString("name");
            String value = rule.getString("value");
            int dataVal = Integer.parseInt(data.get(name).toString());
            int ruleVal = Integer.parseInt(value);
            if (">=".equals(condition)) {
                result = dataVal >= ruleVal;
                if(!result){
                    System.out.println(name);
                }
            } else if ("=".equals(condition)) {
                result = dataVal == ruleVal;
                if(!result){
                    System.out.println(name);
                }
            } else {
                throw new IllegalArgumentException("Unsupported condition: " + condition);
            }
        }
        return result;
    }




// 2、更全一点的方法
public static Boolean makeData(JSONObject rule, Map<String, Object> data) {
        String condition = rule.getString("condition");
        JSONArray children = rule.getJSONArray("children");
        boolean result;

        if ("and".equals(condition)) {
            result = true;
            for (int i = 0; i < children.size(); i++) {
                result &= evaluateConditions(children.getJSONObject(i), data);
                if (!result) {
//                    break;
                }
            }
        } else if ("or".equals(condition)) {
            result = false;
            for (int i = 0; i < children.size(); i++) {
                result |= evaluateConditions(children.getJSONObject(i), data);
                if (result) {
//                    break;
                }
            }
        } else {
            String name = rule.getString("name");
            String value = rule.getString("value");
            Object dataVal = data.get(name);
            if (dataVal != null) {
                if ("ge".equals(condition)) {
                    result = Integer.parseInt(dataVal.toString()) >= Integer.parseInt(value);
                } else if ("eq".equals(condition)) {
                    result = Integer.parseInt(dataVal.toString()) == Integer.parseInt(value);
                } else if ("contains".equals(condition)) {
                    result = value.toString().contains(String.valueOf(dataVal));
                } else {
                    throw new IllegalArgumentException("Unsupported condition: " + condition);
                }
                if (!result) {
                    System.out.println(name + " expected: " + value + ", but was: " + dataVal);
                }
            } else {
                System.out.println("Value for " + name + " not found in data");
                result = false;
            }
        }
        return result;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值