http请求MD5参数验证_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > http请求MD5参数验证

http请求MD5参数验证

 2016/9/3 5:35:27  guoyulong005  程序员俱乐部  我要评论(0)
  • 摘要:publicstaticbooleancheck(HttpServletRequestrequest){Map<String,String[]>map=request.getParameterMap();Set<Entry<String,String[]>>set=map.entrySet();Iterator<Entry<String,String[]>>it=set.iterator();Stringsign="";Map<
  • 标签:HTTP请求 HTTP
public static boolean check(HttpServletRequest request){
Map<String, String[]> map = request.getParameterMap(); 
        Set<Entry<String, String[]>> set = map.entrySet(); 
        Iterator<Entry<String, String[]>> it = set.iterator(); 
        String sign = "";
        Map<String,String> pras = new HashMap<String, String>();
        List<String> keys = new ArrayList<String>();
        while (it.hasNext()) { 
            Entry<String, String[]> 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<keys.size();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;
}
发表评论
用户名: 匿名