zoukankan      html  css  js  c++  java
  • 可配置的用户行为拦截原型

    1.引用 aviator

    <dependency>
              <groupId>com.googlecode.aviator</groupId>
              <artifactId>aviator</artifactId>
              <version>4.1.2</version>
          </dependency>

    2.

    import com.google.common.base.Strings;
    import com.google.common.collect.ImmutableMap;
    
    import com.googlecode.aviator.AviatorEvaluator;
    
    import java.util.Arrays;
    import java.util.HashMap;
    import java.util.HashSet;
    import java.util.List;
    import java.util.Map;
    import java.util.Random;
    import java.util.Set;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    import java.util.stream.Collectors;
    
    
    public class CustomAuth {
        private static Map<RequestScene, String> mapping = ImmutableMap.<RequestScene, String>builder()
                //小于3天的注册,而且没有投资过,大概率是 骗子,要求验证
                .put(RequestScene.Withdraw, "{注册天数}<3 && {投资次数}==0")
                .build();
    
        public static boolean isInvalidateRequest(int customerId, RequestScene scene) {
            return AuthFactory.process(customerId, mapping.get(scene));
        }
    
        public static void main(String[] args) {
            boolean isInvalidateRequest = isInvalidateRequest(10001, RequestScene.Withdraw);
            System.out.println("客户10001行为:" + (isInvalidateRequest ? "非法" : "合法"));
            isInvalidateRequest = isInvalidateRequest(10002, RequestScene.Withdraw);
            System.out.println("客户10002行为:" + (isInvalidateRequest ? "非法" : "合法"));
            isInvalidateRequest = isInvalidateRequest(10003, RequestScene.Withdraw);
            System.out.println("客户10003行为:" + (isInvalidateRequest ? "非法" : "合法"));
        }
    }
    
    class AuthFactory {
        private static Map<String, GetDatable> mapping = ImmutableMap.<String, GetDatable>builder()
                //这里可以修改成自动register
                .put("{注册天数}", new GetRegDays())
                .put("{投资次数}", new GetInvestTimes())
                .build();
        /**
         * 过滤 变量 的正则
         */
        public static final Pattern DATA_PATTERN = Pattern.compile("\{[a-zA-Z0-9\u4e00-\u9fa5]+\}");
    
        public static boolean process(int customerId, String expr) {
            Set<String> dataSet = new HashSet<>();
            Matcher matcher = DATA_PATTERN.matcher(expr);
            while (matcher.find()) {
                String dataSources = matcher.group(0);
                dataSet.add(dataSources);
            }
            Set<GetDatable> getDataSet = dataSet.stream().map(p -> mapping.get(p)).collect(Collectors.toSet());
    
            List<Map<String, Object>> collect = getDataSet.stream().parallel().map(
                    p -> p.get(customerId)).collect(Collectors.toList());
    
            Map<String, Object> result = new HashMap<>();
            for (Map<String, Object> item : collect) {
                result.putAll(item);
            }
    
            String resolveExpress = expr;
            for (String item : dataSet) {
                resolveExpress = resolveExpress.replace(item, result.get(item).toString());
            }
    
            return (Boolean) AviatorEvaluator.execute(resolveExpress);
        }
    }
    
    enum RequestScene {
        Login,
        Withdraw
    }
    
    interface GetDatable {
        Map<String, Object> get(int customerId);
    }
    
    class GetRegDays implements GetDatable {
        @Override
        public Map<String, Object> get(int customerId) {
            int days = 1;
            if (customerId == 10001) {
                days = 1;
            } else if (customerId == 10002) {
                days = 2;
            } else if (customerId == 10003) {
                days = 3;
            }
    
            return ImmutableMap.of("{注册天数}", days,"{开户天数}",1);
        }
    }
    
    class GetInvestTimes implements GetDatable {
        @Override
        public Map<String, Object> get(int customerId) {
           /* Random random = new Random();
            int i = random.nextInt(3);*/
    
            int times = 1;
            if (customerId == 10001) {
                times = 0;
            } else if (customerId == 10002) {
                times = 1;
            } else if (customerId == 10003) {
                times = 0;
            }
    
            return ImmutableMap.of("{投资次数}", times);
        }
    }
    客户10001行为:非法
    客户10002行为:合法
    客户10003行为:合法
  • 相关阅读:
    #Leetcode# 451. Sort Characters By Frequency
    #Leetcode# 148. Sort List
    PAT 甲级 1138 Postorder Traversal
    PAT 甲级 1141 PAT Ranking of Institutions
    PAT 甲级 1142 Maximal Clique
    PAT 甲级 1146 Topological Order
    PAT 甲级 1143 Lowest Common Ancestor
    #Leetcode# 347. Top K Frequent Elements
    牛客寒假算法基础集训营4
    PAT 1147 Heaps
  • 原文地址:https://www.cnblogs.com/zhshlimi/p/10496928.html
Copyright © 2011-2022 走看看