栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

微信服务号新手入门讲解、第一章(可信服务器的认证)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

微信服务号新手入门讲解、第一章(可信服务器的认证)

	首先建议大家不要使用第三方封装好的工具包去进行公众号的开发,原因如下:1 封装好的看不见源码,对于新手而言如隔靴搔痒,出现问题找不到缘由,你不知道怎么回事 ;2 本身就是新手,不了解其中的一些思路,不利于自身学习;所以若要进行公众号方面的开发工作,强烈建议大家自己去根据官方文档自行开发,对于新入门的各位老铁一定要对文档熟悉,说白了,熟悉了文档,你就会开发了,不会开发的说明文档不够熟悉。附官方文档一份:[公众号官方开发文档](https://developers.weixin.qq.com/doc/offiaccount/Getting_Started/Overview.html)
	准备工具:
	官方提供的web开发工具:https://developers.weixin.qq.com/miniprogram/dev/devtools/download.html
	其作用是代替手机访问公众号进行开发测试,使用方式类似于浏览器
	
	在线接口调试:https://mp.weixin.qq.com/debug?token=521186875&lang=zh_CN

	开发测试号申请:https://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login&token=521186875&lang=zh_CN

    透外网工具:https://natapp.cn/ (需要自己注册)
		
   申请测试号登陆如图一,appID和appsecret是微信服务器自动生成的,代表你的测试服务号是唯一的,直接用就可以。URL是你校验可信应用的地址,token和域名填自己的信息,域名可用natapp工具来生成。

图一
图二:此处域名与图一域名一致。(不要忘记改)

配置完毕后上代码开始验证服务器,

controller

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.dom4j.documentException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
 


@Controller
@RequestMapping("pendingItems")
public class TestCallbackController {

	@RequestMapping(value = "/callBack", method = RequestMethod.GET)
    public void Test1(HttpServletRequest request, HttpServletResponse response) throws IOException {
    	// 微信加密签名  
        String signature = request.getParameter("signature");  
        // 时间戳  
        String timestamp = request.getParameter("timestamp");  
        // 随机数  
        String nonce = request.getParameter("nonce");  
        // 随机字符串  
        String echostr = request.getParameter("echostr");  
    		
        PrintWriter out = response.getWriter();  
        // 通过检验signature对请求进行校验,若校验成功则原样返回echostr,表示接入成功,否则接入失败  
        if (SignUtil.checkSignature(signature, timestamp, nonce)) {  
            out.print(echostr);  
        }  
        out.close();  
        out = null;  
    }
     
    
}

工具类SignUtil

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

public class SignUtil {
	
	// 与接口配置信息中的Token要一致  (此处的token就是你填入图一位置的token)
    private static String token = AppProperties.readAppProperties().getProperty("wxqy.token");
  
      
    public static boolean checkSignature(String signature, String timestamp, String nonce) {  
        String[] arr = new String[] { token, timestamp, nonce };  
        // 将token、timestamp、nonce三个参数进行字典序排序  
        Arrays.sort(arr);  
        StringBuilder content = new StringBuilder();  
        for (int i = 0; i < arr.length; i++) {  
            content.append(arr[i]);  
        }  
        MessageDigest md = null;  
        String tmpStr = null;  
  
        try {  
            md = MessageDigest.getInstance("SHA-1");  
            // 将三个参数字符串拼接成一个字符串进行sha1加密  
            byte[] digest = md.digest(content.toString().getBytes());  
            tmpStr = byteToStr(digest);  
        } catch (NoSuchAlgorithmException e) {  
            e.printStackTrace();  
        }  
  
        content = null;  
        // 将sha1加密后的字符串可与signature对比,标识该请求来源于微信  
        return tmpStr != null ? tmpStr.equals(signature.toUpperCase()) : false;  
    }  
  
      
    private static String byteToStr(byte[] byteArray) {  
        String strDigest = "";  
        for (int i = 0; i < byteArray.length; i++) {  
            strDigest += byteToHexStr(byteArray[i]);  
        }  
        return strDigest;  
    }  
  
      
    private static String byteToHexStr(byte mByte) {  
        char[] Digit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };  
        char[] tempArr = new char[2];  
        tempArr[0] = Digit[(mByte >>> 4) & 0x0F];  
        tempArr[1] = Digit[mByte & 0x0F];  
  
        String s = new String(tempArr);  
        return s;  
    } 
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/591682.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号