微信公众号开发--接收与回复消息(Java)

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

               

第一步:配置微信公众号

进入微信公众平台地址

这里写图片描述

进入基本配置

这里写图片描述

这里需要填写的有服务器的URL、Token两个地方

URL

是一个公网地址,只能接收80端口的(http默认端口)

http://公网地址/项目名称/请求路径

Token

在校验的时候需要用到,随便输入一个字符串就可以了

开发者提交信息后,微信服务器将发送GET请求到填写的服务器地址URL上,GET请求携带四个参数 
参数 描述 
signature 微信加密签名,signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数。 
timestamp 时间戳 
nonce 随机数 
echostr 随机字符串

所以需要编写相应的Controller(采用spring MVC)

    /**     * 验证微信服务器     *      * @param response     * @param signature     * @param timestamp     * @param nonce     * @param echostr     */    @RequestMapping(value = "/wechat", method = RequestMethod.GET)    public void wechatService(PrintWriter out, HttpServletResponse response,            @RequestParam(value = "signature", required = false) String signature, @RequestParam String timestamp,            @RequestParam String nonce, @RequestParam String echostr) {        if (CheckUtil.checkSignature(signature, timestamp, nonce)) {            out.print(echostr);        }    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

CheckUtil.Java

package com.wechat.utils;import java.util.Arrays;/** *  * 校验工具类 * 开发者通过检验signature对请求进行校验(下面有校验方式)。若确认此次GET请求来自微信服务器,请原样返回echostr参数内容,则接入生效, * 成为开发者成功,否则接入失败。 *  * @author GWCheng * */public class CheckUtil {
        //配置微信公众号时填写的Token    private static final String token = "chenggaowei";    public static boolean checkSignature(String signature, String timestamp, String nonce) {        // 拼接字符串        String[] arr = new String[] { token, timestamp, nonce };        // 排序        Arrays.sort(arr);        // 生成字符串        StringBuffer content = new StringBuffer();        for (int i = 0; i < arr.length; i++) {            content.append(arr[i]);        }        // SHA1加密        String tmp = DecriptUtil.SHA1(content.toString());        return tmp.equals(signature);    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34

DecriptUtil.java

import java.io.UnsupportedEncodingException;import java.security.InvalidKeyException;import java.security.MessageDigest;import java.security.NoSuchAlgorithmException;import java.security.SecureRandom;import javax.crypto.BadPaddingException;import javax.crypto.Cipher;import javax.crypto.IllegalBlockSizeException;import javax.crypto.KeyGenerator;import javax.crypto.NoSuchPaddingException;import javax.crypto.SecretKey;import javax.crypto.spec.SecretKeySpec;/** * 各种加密解密 * */public class DecriptUtil {
        public static String SHA1(String decript) {        try {            MessageDigest digest = java.security.MessageDigest                    .getInstance("SHA-1");            digest.update(decript.getBytes());            byte messageDigest[] = digest.digest();            // Create Hex 
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值