配置接口 jsp 代码示例

 <%@ page language="java" contentType="text/html;charset=UTF-8"%>

<%@ page import="java.util.Iterator,java.util.TreeMap,java.security.MessageDigest"%>

<%

try {

String signature = request.getParameter("signature");

String echostr = request.getParameter("echostr");

 

String mySignature = encryptBySHA1(appendParams(request));

 

if (mySignature.equals(signature)) {

response.getWriter().print(echostr);

response.getWriter().flush();

response.getWriter().close();

}

} catch (Exception e) {

e.printStackTrace();

}

%>

 

<%!private String appendParams(HttpServletRequest request) {

TreeMap<String, String> treeMap = new TreeMap<String, String>();

 

String timestamp = request.getParameter("timestamp");

String nonce = request.getParameter("nonce");

 

treeMap.put(timestamp, "");

treeMap.put(nonce, "");

treeMap.put("weixin", "");

 

StringBuffer result = new StringBuffer();

Iterator<String> it = treeMap.keySet().iterator();

 

while (it.hasNext()) {

result.append(it.next());

}

 

return result.toString();

}%>

 

<%!private String encryptBySHA1(String src) {

byte[] bytes = null;

StringBuffer result = new StringBuffer();

 

try {

MessageDigest md = MessageDigest.getInstance("SHA");

md.update(src.getBytes("utf-8"));

bytes = md.digest();

 

for (int i = 0; i < bytes.length; i++) {

result.append(Integer.toHexString(

(0x000000ff & bytes[i]) | 0xffffff00).substring(6));

}

} catch (Exception ex) {

return null;

}

 

return result.toString();

}%>

相关内容推荐

返回
顶部