http请求MD5参数验证

public static boolean check(HttpServletRequest request){
Map map = request.getParameterMap();
Set> set = map.entrySet();
Iterator> it = set.iterator();
String sign = "";
Map pras = new HashMap();
List keys = new ArrayList();
while (it.hasNext()) {
Entry entry = it.next();
String key = entry.getKey().trim();
String value = entry.getValue()[0];
if(!"sign".equals(key)){
if(value==null) value = "";
keys.add(key);
pras.put(key, value.trim());
}
if("sign".equals(key)) sign = value;
}
Collections.sort(keys);
StringBuffer bf = new StringBuffer();
for(int i=0;i bf.append(keys.get(i)).append("=");
try {
bf.append(URLEncoder.encode(pras.get(keys.get(i)),"utf-8")+"&");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
bf.append("key=").append(Global.getConfig("httpMD5Key").trim());
logger.info("待签名字符串:"+bf.toString()+" sign:"+sign + " MD5:"+MD5.GetMD5Code(bf.toString()));
if(sign.equals(MD5.GetMD5Code(bf.toString()))){
return true;
}
return false;
}

你可能感兴趣的:(http请求MD5参数验证)