WeChat public account server configuration verification implementation

To set the development mode in the WeChat official account, it is necessary to call the background server for verification of pseudo-code implementation:
 
WeChat official account server configuration verification implementation

/**
* WeChat server configuration verification method
 *
 * @param request
 * @param response
 * @return
 */
 private String access(HttpServletRequest request, HttpServletResponse response) {
    String signature = request.getParameter("signature");
    String timestamp = request.getParameter("timestamp");
    String nonce = request.getParameter("nonce");
    String echostr = request.getParameter( "echostr" );
     logger .info( "echostr:{}" requested by WeChat , echostr);
     // Sort the three parameters of token, timestamp and nonce in lexicographic order
 List<String> paramList = new ArrayList<String>();
    paramList.add(token);
    paramList.add(timestamp);
    paramList.add(nonce);
    SortUtils.arrayListSortByDict ( paramList );
     logger .info( "The three parameters of timestamp, nonce, token are sorted lexicographically: {}" , JSON. toJSONString (paramList));
     // Concatenate the three parameter strings into a string Perform sha1 encryption
 String result = "" ;
     for (String str : paramList) {
        result += str;
    }
    String resultPass = DigestUtils.sha1Hex ( result);
     // The encrypted string can be compared with signature to identify that the request comes from WeChat
 if (resultPass.equals(signature)) {
         logger .info( "Returned echostr:{}" , echostr);
         return echostr;
    }
    logger .info(String. format ( "Wechat request verification failed! token: [%s], signature of this server: [%s], wechat signature: [%s]" , token , resultPass, signature));
     return "" ;
}


public class SortUtils {
    /**
     * Sort by dictionary
     * 
     * @param sortList
 */
 public static void arrayListSortByDict(List<String> sortList) {
      Collections.sort(sortList, new RealizeComparator());
   }
}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326415914&siteId=291194637