Home > WeChat Applet > WeChat Development > Detailed explanation of the steps to start the development mode of Java WeChat enterprise account development

Detailed explanation of the steps to start the development mode of Java WeChat enterprise account development

高洛峰
Release: 2017-03-15 17:49:07
Original
2505 people have browsed it

This article mainly introduces in detail how to open the development mode of java WeChat enterprise account development. Interested friends can refer to it

First of all, the development mode of WeChat enterprise account is divided into: Edit mode (normal mode) and development mode (callback mode), in edit mode, you can only do simple custom menu and automatic reply messages, if you want to realize other functions, you must turn on the developer mode.

1. Message processing process in edit mode and development mode

1. In edit mode, all business processes are configured on the WeChat server and processed by it

Detailed explanation of the steps to start the development mode of Java WeChat enterprise account development

2. Development mode, the message is processed through a third-party server, and finally the message is sent to the user through the WeChat server

Detailed explanation of the steps to start the development mode of Java WeChat enterprise account development

Development mode can handle more messages than editing mode, so you must turn on development mode first to develop more functions.

2. Turning on development mode

In callback mode, enterprises can not only actively call the enterprise numberinterface, but also receive messages from users or event. The received information uses XML data format, UTF8 encoding, and is encrypted in AES.

1. After turning on callback mode, configure the parameters as follows:

Detailed explanation of the steps to start the development mode of Java WeChat enterprise account development

where url is the servlet to be accessed, token and EncodingAESKey are obtained randomly, but they must be consistent with those in the project.

2. Verify the validity of the URL

When you submit the above information, the enterprise account will send a GET request to the filled-in URL. The GET request carries four parameters. The enterprise is in You need to do urldecode processing when getting it, otherwise the verification will not be successful.

Detailed explanation of the steps to start the development mode of Java WeChat enterprise account development

3. Code

CoreServlet1 class

public class CoreServlet1 extends HttpServlet {
 private static final long serialVersionUID = 4440739483644821986L;
 String sToken = "weixinCourse";
 String sCorpID = "wxe510946434680dab";
 String sEncodingAESKey = "DjlyZxgKiWRESIW2VnV9dSr7HsS7usWDfnwA8Q1ove1";
 
 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 
  WXBizMsgCrypt wxcpt;
 
   try {
 wxcpt = new WXBizMsgCrypt(sToken, sEncodingAESKey, sCorpID); 
 
 String sVerifyMsgSig = request.getParameter("msg_signature");
 
 String sVerifyTimeStamp = request.getParameter("timestamp"); 
 
 String sVerifyNonce = request.getParameter("nonce"); 
 
 String sVerifyEchoStr = request.getParameter("echostr");  
 String sEchoStr;
 
 sEchoStr = wxcpt.VerifyURL(sVerifyMsgSig, sVerifyTimeStamp,
   sVerifyNonce, sVerifyEchoStr);
 System.out.println("verifyurl echostr: " + sEchoStr);
 PrintWriter out = response.getWriter();
 out.print(sEchoStr); 
 out.close();
 out = null;
 
 } catch (AesException e1) {
 
 e1.printStackTrace();
 }
 
  }
}
Copy after login

Tool class :

 /**
 * 对公众平台发送给公众账号的消息加解密示例代码.
 * 
 * @copyright Copyright (c) 1998-2014 Tencent Inc.
 */

// ------------------------------------------------------------------------

/**
 * 针对org.apache.commons.codec.binary.Base64,
 * 需要导入架包commons-codec-1.9(或commons-codec-1.8等其他版本)
 * 官方下载地址://m.sbmmt.com/
 */
package com.qq.weixin.mp.aes;

import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.Random;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base64;

/**
 * 提供接收和推送给公众平台消息的加解密接口(UTF8编码的字符串).
 * <ol>
 * <li>第三方回复加密消息给公众平台</li>
 * <li>第三方收到公众平台发送的消息,验证消息的安全性,并对消息进行解密。</li>
 * </ol>
 * 说明:异常java.security.InvalidKeyException:illegal Key Size的解决方案
 * <ol>
 * <li>在官方网站下载JCE无限制权限策略文件(JDK7的下载地址:
 *   //m.sbmmt.com/;/li>
 * <li>下载后解压,可以看到local_policy.jar和US_export_policy.jar以及readme.txt</li>
 * <li>如果安装了JRE,将两个jar文件放到%JRE_HOME%\lib\security目录下覆盖原来的文件</li>
 * <li>如果安装了JDK,将两个jar文件放到%JDK_HOME%\jre\lib\security目录下覆盖原来文件</li>
 * </ol>
 */
public class WXBizMsgCrypt {
 static Charset CHARSET = Charset.forName("utf-8");
 Base64 base64 = new Base64();
 byte[] aesKey;
 String token;
 String corpId;

 /**
 * 构造函数
 * @param token 公众平台上,开发者设置的token
 * @param encodingAesKey 公众平台上,开发者设置的EncodingAESKey
 * @param corpId 企业的corpid
 * 
 * @throws AesException 执行失败,请查看该异常的错误码和具体的错误信息
 */
 public WXBizMsgCrypt(String token, String encodingAesKey, String corpId) throws AesException {
 if (encodingAesKey.length() != 43) {
  throw new AesException(AesException.IllegalAesKey);
 }

 this.token = token;
 this.corpId = corpId;
 aesKey = Base64.decodeBase64(encodingAesKey + "=");
 }

 
 

 /**
 * 对密文进行解密.
 * 
 * @param text 需要解密的密文
 * @return 解密得到的明文
 * @throws AesException aes解密失败
 */
 String decrypt(String text) throws AesException {
 byte[] original;
 try {
  // 设置解密模式为AES的CBC模式
  Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
  SecretKeySpec key_spec = new SecretKeySpec(aesKey, "AES");
  IvParameterSpec iv = new IvParameterSpec(Arrays.copyOfRange(aesKey, 0, 16));
  cipher.init(Cipher.DECRYPT_MODE, key_spec, iv);

  // 使用BASE64对密文进行解码
  byte[] encrypted = Base64.decodeBase64(text);

  // 解密
  original = cipher.doFinal(encrypted);
 } catch (Exception e) {
  e.printStackTrace();
  throw new AesException(AesException.DecryptAESError);
 }

 String xmlContent, from_corpid;
 try {
  // 去除补位字符
  byte[] bytes = PKCS7Encoder.decode(original);

  // 分离16位随机字符串,网络字节序和corpId
  byte[] networkOrder = Arrays.copyOfRange(bytes, 16, 20);

  int xmlLength = recoverNetworkBytesOrder(networkOrder);

  xmlContent = new String(Arrays.copyOfRange(bytes, 20, 20 + xmlLength), CHARSET);
  from_corpid = new String(Arrays.copyOfRange(bytes, 20 + xmlLength, bytes.length),
   CHARSET);
 } catch (Exception e) {
  e.printStackTrace();
  throw new AesException(AesException.IllegalBuffer);
 }

 // corpid不相同的情况
 if (!from_corpid.equals(corpId)) {
  throw new AesException(AesException.ValidateCorpidError);
 }
 return xmlContent;

 }


 /**
 * 验证URL
 * @param msgSignature 签名串,对应URL参数的msg_signature
 * @param timeStamp 时间戳,对应URL参数的timestamp
 * @param nonce 随机串,对应URL参数的nonce
 * @param echoStr 随机串,对应URL参数的echostr
 * 
 * @return 解密之后的echostr
 * @throws AesException 执行失败,请查看该异常的错误码和具体的错误信息
 */
 public String VerifyURL(String msgSignature, String timeStamp, String nonce, String echoStr)
  throws AesException {
 String signature = SHA1.getSHA1(token, timeStamp, nonce, echoStr);

 if (!signature.equals(msgSignature)) {
  throw new AesException(AesException.ValidateSignatureError);
 }

 String result = decrypt(echoStr);
 return result;
 }

}
 /**
 * 对公众平台发送给公众账号的消息加解密示例代码.
 * 
 * @copyright Copyright (c) 1998-2014 Tencent Inc.
 */

// ------------------------------------------------------------------------

package com.qq.weixin.mp.aes;

import java.security.MessageDigest;
import java.util.Arrays;

/**
 * SHA1 class
 *
 * 计算公众平台的消息签名接口.
 */
class SHA1 {

 /**
 * 用SHA1算法生成安全签名
 * @param token 票据
 * @param timestamp 时间戳
 * @param nonce 随机字符串
 * @param encrypt 密文
 * @return 安全签名
 * @throws AesException 
 */
 public static String getSHA1(String token, String timestamp, String nonce, String encrypt) throws AesException
   {
 try {
  String[] array = new String[] { token, timestamp, nonce, encrypt };
  StringBuffer sb = new StringBuffer();
  // 字符串排序
  Arrays.sort(array);
  for (int i = 0; i < 4; i++) {
  sb.append(array[i]);
  }
  String str = sb.toString();
  // SHA1签名生成
  MessageDigest md = MessageDigest.getInstance("SHA-1");
  md.update(str.getBytes());
  byte[] digest = md.digest();

  StringBuffer hexstr = new StringBuffer();
  String shaHex = "";
  for (int i = 0; i < digest.length; i++) {
  shaHex = Integer.toHexString(digest[i] & 0xFF);
  if (shaHex.length() < 2) {
   hexstr.append(0);
  }
  hexstr.append(shaHex);
  }
  return hexstr.toString();
 } catch (Exception e) {
  e.printStackTrace();
  throw new AesException(AesException.ComputeSignatureError);
 }
 }
}

 class PKCS7Encoder {
 static Charset CHARSET = Charset.forName("utf-8");
 static int BLOCK_SIZE = 32;
/**
 * 删除解密后明文的补位字符
 * 
 * @param decrypted 解密后的明文
 * @return 删除补位字符后的明文
 */
 static byte[] decode(byte[] decrypted) {
 int pad = (int) decrypted[decrypted.length - 1];
 if (pad < 1 || pad > 32) {
  pad = 0;
 }
 return Arrays.copyOfRange(decrypted, 0, decrypted.length - pad);
 }
}
Copy after login

3. Summary
The enterprise verifies the request through the parameter msg_signature. If the GET is confirmed If the request comes from an enterprise account, then the enterprise application decrypts the echostr parameter and returns the echostr plaintext as it is (no quotes), then the access verification takes effect and the callback mode can be turned on. After it is turned on, some functions will be implemented one after another, so stay tuned!


The above is the detailed content of Detailed explanation of the steps to start the development mode of Java WeChat enterprise account development. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template