包含通配符的equals

简介:

1场景描述

网站要实现自动化测试,但是界面上有图形验证码,所以没法自动化.

如何解决呢?

我专门为测试同事写了一个接口,用于返回图形验证码明文,比如

 我返回的就是PFXa

 

但是这个接口不能暴露在线上,否则,你懂的....

所以我增加了一个白名单,白名单中是允许访问的ip.目前白名单只有内网ip,所以就算接口暴露在线上,别人也没法访问.

但是白名单中会有通配符,比如192.168.1.*  ,因为电脑关机之后ip可能会变化

Java代码   收藏代码
  1. private static String[] whiteList = new String[]{"127.0.0.1""11.1.213.200""11.1.251.166",  "11.11.62.*"};  

在具体业务之前,会先校验ip是否在白名单中

Java代码   收藏代码
  1. if (StringUtil.isContains2(whiteList, ip) == -1) {  
  2.             map.put(KEY_RESULT, false);  
  3.             map.put(KEY_PARAM, "ip not in white list:" + ip);  
  4.             return JSONExtension.getJsonP(map, callback);  
  5.         }  

  

 

2,使用正则表达式

isContains2 方法

功能:判断当前访问的ip是否在白名单中,不在,则直接报错

实现如下

Java代码   收藏代码
  1. public static int isContains2(String[] strArray, String j) {  
  2.         int index = Constant.NEGATIVE_ONE;  
  3.         if (isNullOrEmpty(strArray)) {  
  4.             return index;  
  5.         }  
  6.         int length2 = strArray.length;  
  7.         for (int ii = 0; ii < length2; ii++) {  
  8.             String i = strArray[ii];  
  9.             if (i == j || (equalsWildcard(j, i))) {  
  10.                 index = ii;  
  11.                 break;  
  12.             }  
  13.         }  
  14.         return index;  
  15.     }  

 

equalsWildcard方法如下:

Java代码   收藏代码
  1. /*** 
  2.      *  
  3.      * @param source 
  4.      * @param regex : 含有通配符,通配符只有一个:*.<br> 
  5.      * *表示任何字符,不限个数 
  6.      * @return 
  7.      */  
  8.     public static boolean equalsWildcard(String source,String regex){  
  9.         regex=regex.replace(".""\\.");  
  10.         regex=regex.replace("*""(.*)");//加上小括号,方便查看  
  11. //      System.out.println(regex);  
  12.         Pattern p = Pattern.compile("^"+regex+"$", Pattern.CASE_INSENSITIVE);  
  13.         Matcher m = p.matcher(source);  
  14.         return m.find();  
  15.     }  

 对方法equalsWildcard()的单元测试

Java代码   收藏代码
  1. @Test  
  2.     public void test_equal(){  
  3.         String source="ab.d.c";  
  4. //      System.out.println(regex);  
  5.         org.junit.Assert.assertFalse(RegexUtil.equalsWildcard(source, ".b.d.c"));  
  6.         org.junit.Assert.assertFalse(RegexUtil.equalsWildcard(source, "a..d.c"));  
  7.         org.junit.Assert.assertFalse(RegexUtil.equalsWildcard(source, "a.*.d.c"));  
  8.         org.junit.Assert.assertFalse(RegexUtil.equalsWildcard(source, "abad.c"));  
  9.         org.junit.Assert.assertFalse(RegexUtil.equalsWildcard(source, "a*ad.c"));  
  10.         org.junit.Assert.assertFalse(RegexUtil.equalsWildcard(source, "ab.d.."));  
  11.         org.junit.Assert.assertTrue(RegexUtil.equalsWildcard(source, "ab.d.c"));  
  12.         org.junit.Assert.assertTrue(RegexUtil.equalsWildcard(source, source));  
  13.         org.junit.Assert.assertTrue(RegexUtil.equalsWildcard(source, "a*.d.c"));  
  14.         org.junit.Assert.assertTrue(RegexUtil.equalsWildcard(source, "ab.*.c"));  
  15.         org.junit.Assert.assertTrue(RegexUtil.equalsWildcard(source, "ab*.c"));  
  16.         org.junit.Assert.assertTrue(RegexUtil.equalsWildcard(source, "ab*.d*c"));  
  17.   
  18.     }  
  19.       

 

equalsWildcard说明:

有两个参数:

第一个参数:要比较的字符串,没有通配符,不是正则表达式.比如访问的ip;

第二个参数:包含通配符,目前只有一个通配符:* ,匹配若干个字符

相关文章
|
19天前
|
XML 应用服务中间件 Apache
通配符的匹配很全面, 但无法找到元素 ‘context:component-scan‘ 的声明。
通配符的匹配很全面, 但无法找到元素 ‘context:component-scan‘ 的声明。
|
6月前
解决cvc-complex-type.2.4.c: 通配符的匹配很全面, 但无法找到元素 ‘context:property-placeholder‘ 的声明的问题~
解决cvc-complex-type.2.4.c: 通配符的匹配很全面, 但无法找到元素 ‘context:property-placeholder‘ 的声明的问题~
|
4月前
|
Java 索引
正则表达式源码分析--三个常用类--分组、捕获、反向引用--String 类中使用正则表达式的代码示例和图
正则表达式源码分析--三个常用类--分组、捕获、反向引用--String 类中使用正则表达式的代码示例和图
47 0
|
5月前
匹配多个对象
匹配多个对象
23 3
|
10月前
通配符?,*,**区别
通配符?,*,**区别
113 0
|
7月前
使用 ABAP 正则表达式解析 uuid 的值
使用 ABAP 正则表达式解析 uuid 的值
49 0
在字符串方法 search() 中使用正则表达式
在字符串方法 search() 中使用正则表达式
56 0
lodash检查字符串string是否以给定的字符串结尾
lodash检查字符串string是否以给定的字符串结尾
147 0
检查`string`是否以给定的目标字符串结尾
检查`string`是否以给定的目标字符串结尾
74 0
|
XML Dubbo Java
通配符的匹配很全面, 但无法找到元素 'context:component-scan' 的声明
通配符的匹配很全面, 但无法找到元素 'context:component-scan' 的声明