Java 微信公众号开发(一) 介入微信

开发微信公众号在没有正式的公众平台账号时,我们可以使用测试平台账号———

测试平台申请地址:https://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login

进入之后我们会看见 此时 appID、appsecret都有了,url是我们成为开发者与微信进行的一次握手配置(url其实就是我们项目中你controller的访问地址,token是我们自己填写的,可在后台进行判断的),这里的url可以用ngrok来做映射,这样开发起来比较方便,ngrok配置(点击查看)ngrok下载地址(点击下载)

我们可以在开发者文档中看见

这里查看详细配置;期间我们要说道微信会给我们的url通过get方式传递4个参数

下面我们来看一下controller怎么编写

package com.website.wechat.controller;

import java.io.IOException;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

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

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

@Controller
@RequestMapping(value="weixin")
public class WeiXinController {
	
	private static final char[] HEX_DIGITS = { '0', '1', '2', '3', '4', '5',
		'6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };

	@RequestMapping(value="getWeiXinMethod",method=RequestMethod.GET)
	@ResponseBody
	public void getWeiXinMethod(HttpServletRequest request, HttpServletResponse response) throws IOException{
		boolean validate = validate(request);
		if (validate) {
			response.getWriter().write(request.getParameter("echostr"));
			response.getWriter().close();
		}
		
	}
	
	private boolean validate(HttpServletRequest req) throws IOException {
		String signature = req.getParameter("signature");//微信加密签名
		String timestamp = req.getParameter("timestamp");//时间戳
		String nonce = req.getParameter("nonce");//随机数
		List<String> list = new ArrayList<String>();
		list.add("chenchen");
		list.add(timestamp);
		list.add(nonce);
		Collections.sort(list);//字典排序
		String s = "";
		for (int i = 0; i < list.size(); i++) {
			s += (String) list.get(i);
		}
		if (encode("SHA1", s).equalsIgnoreCase(signature)) {
			return true;
		} else {
			return false;
		}
	}

	public static String encode(String algorithm, String str) {
		if (str == null) {
			return null;
		}
		try {
			//Java自带的加密类
			MessageDigest messageDigest = MessageDigest.getInstance(algorithm);
			//转为byte
			messageDigest.update(str.getBytes());
			return getFormattedText(messageDigest.digest());
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	private static String getFormattedText(byte[] bytes) {
		int len = bytes.length;
		StringBuilder buf = new StringBuilder(len * 2);
		// 把密文转换成十六进制的字符串形式
		for (int j = 0; j < len; j++) {
			buf.append(HEX_DIGITS[(bytes[j] >> 4) & 0x0f]);
			buf.append(HEX_DIGITS[bytes[j] & 0x0f]);
		}
		return buf.toString();
	}
}
此时验证和调用已经没问题,成功介入微信,成为一名开发者

  • 6
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值