JavaWeb接入微信公众号

青旅半醒 2023-07-11 11:12 97阅读 0赞

不啰嗦,直接上代码:

  1. //这里注意要确保微信公众号开发配置时的网址可以访问到这个方法
  2. @RequestMapping(value = "/setwechat")
  3. public void setWechat(HttpServletRequest request,HttpServletResponse response) throws Exception{
  4. response.setCharacterEncoding("utf-8");
  5. request.setCharacterEncoding("utf-8");
  6. String result = "";
  7. /* 不管在微信上进行什么操作,都会有xml报文传到绑定URL上 */
  8. Map res = showParams(request);
  9. Set<Map.Entry<Integer,String>> entrys = res.entrySet();
  10. System.out.println("setwechat微信获取数据如下:");
  11. for(Map.Entry entry:entrys){
  12. String key = (String)entry.getKey();
  13. String value = (String)entry.getValue();
  14. System.out.println("key:"+key+" value:"+value);
  15. }
  16. if(request.getParameter("openid")==null){//接入校验
  17. String timestamp = "", nonce = "", signature = "", echostr = "";
  18. timestamp = request.getParameter("timestamp");
  19. nonce = request.getParameter("nonce");
  20. signature = request.getParameter("signature");
  21. echostr = request.getParameter("echostr");
  22. boolean b = checkSignature(token,signature,timestamp,nonce);
  23. if(b){
  24. result = echostr;
  25. }
  26. }else{//非接入
  27. }
  28. PrintWriter pw = response.getWriter();
  29. pw.print(result);
  30. pw.close();
  31. }
  32. //检查是否合法
  33. public static boolean checkSignature(String token,String signature, String timestamp,
  34. String nonce) {
  35. String[] arr = new String[] { token, timestamp, nonce };
  36. Arrays.sort(arr);
  37. StringBuilder content = new StringBuilder();
  38. for (int i = 0; i < arr.length; i++) {
  39. content.append(arr[i]);
  40. }
  41. MessageDigest md = null;
  42. String tmpStr = null;
  43. try {
  44. md = MessageDigest.getInstance("SHA-1");
  45. byte[] digest = md.digest(content.toString().getBytes());
  46. tmpStr = byteToStr(digest);
  47. } catch (NoSuchAlgorithmException e) {
  48. e.printStackTrace();
  49. }
  50. return tmpStr != null ? tmpStr.equals(signature.toUpperCase()) : false;
  51. }
  52. private static String byteToStr(byte[] digest) {
  53. String strDigest = "";
  54. for (int i = 0; i < digest.length; i++) {
  55. strDigest += byteToHexStr(digest[i]);
  56. }
  57. return strDigest;
  58. }
  59. public static String byteToHexStr(byte b) {
  60. char[] Digit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A',
  61. 'B', 'C', 'D', 'E', 'F' };
  62. char[] tempArr = new char[2];
  63. tempArr[0] = Digit[(b >>> 4) & 0X0F];
  64. tempArr[1] = Digit[b & 0X0F];
  65. String s = new String(tempArr);
  66. return s;
  67. }
  68. //获取所有request里的数据,便于测试
  69. public static Map<String,Object> showParams(HttpServletRequest request) {
  70. Map<String,Object> map = new HashMap<String,Object>();
  71. Enumeration paramNames = request.getParameterNames();
  72. while (paramNames.hasMoreElements()) {
  73. String paramName = (String) paramNames.nextElement();
  74. String[] paramValues = request.getParameterValues(paramName);
  75. if (paramValues.length >0) {
  76. String paramValue = paramValues[0];
  77. if (paramValue.length() != 0) {
  78. map.put(paramName, paramValue);
  79. }
  80. }
  81. }
  82. return map;
  83. }

发表评论

表情:
评论列表 (有 0 条评论,97人围观)

还没有评论,来说两句吧...

相关阅读

    相关 公众接入服务器

    引言 微信公众号可以分享人文趣事,也可以用来传播公司文化,不管我们用来分享什么,总少不了与用户的交互环节,为了提高用户体验,我们可以通过自定义接入服务器的形式来实现更...

    相关 公众开发:账号申请与接入

    > 接入微信公众平台开发需要使用微信公众号,由于正式微信公众号注册具有一定门槛,普通用户很难申请。所以,为了帮助开发者快速了解和上手微信公众号开发,熟悉各个接口的调用,微信还提