目录
1.准备工作
产生原因:
基本流程:
本地的内网穿透工具用的是花生壳的免费的
代码展示: 基于基本的springboot
结果显示:成功之后,提交按钮就会消失:
1.准备工作
强制条件:开通申请个人微信公众号:
产生原因:
微信原有的功能不足以满足个性化的需求,比如我要访问我个人的服务器的聊天系统和对应的商城系统完成详情的查看和修改,
比如银行的绑定的个人的信用卡绑定和基金以及对应的涉及到动账的一些交易,这些东西仅仅只有微信公众号,肯定是难以满足条件的,所以这个时候需要对接到自己的服务器上来,微信因为是一个入口,所以一定要从微信进入的原因!
基本流程:
进入到开发者文档:
开发者文档:开始开发:测试好申请:
url配置,公众号关注:
接入指南:
提交就是发送请求:
基本验证接收到请求同时返回数据也没有问题,那么就是接入的demo 成功了
本地的内网穿透工具用的是花生壳的免费的
注意对应的端口号要对应: 比如:花生壳配置的是 127.0.0.1:8082 则对应的spring boot项目也需要启动的端口号是:
8082
代码展示: 基于基本的springboot
对应的controller
package cn.itechyou.cms.controller;import org.hibernate.validator.internal.engine.messageinterpolation.parser.ELState;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import org.springframework.web.context.request.RequestAttributes;import org.springframework.web.context.request.RequestContextHolder;import org.springframework.web.context.request.ServletRequestAttributes;import org.yaml.snakeyaml.events.Event;import javax.annotation.Resource;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.IOException;import java.io.PrintWriter;/*** @author liuYC* @ClassName WeixinController* @Description TODO* @date /8/12 9:58*/@RestController@RequestMappingpublic class WeixinController {@ResourceWeixinService weixinService;private static final String TOKEN = "adfadfsda";@RequestMapping("wx")public void test() throws IOException {// 对于的controller方法里面,写以下代码/*** 取出来对应的参数:* 参数描述* signature微信加密签名,signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数。* timestamp时间戳* nonce随机数* echostr随机字符串*/RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) requestAttributes;HttpServletRequest request = servletRequestAttributes.getRequest();HttpServletResponse response = servletRequestAttributes.getResponse();String signature = request.getParameter("signature");String timestamp = request.getParameter("timestamp");String nonce = request.getParameter("nonce");String echostr = request.getParameter("echostr");System.out.println(signature);System.out.println(timestamp);System.out.println(nonce);System.out.println(echostr);if (weixinService.checkSignature(signature, timestamp, nonce, TOKEN)) {System.out.println("success");// 原样返回 :微信公众号服务器受到返回的数据,显示修改成功PrintWriter out = response.getWriter();out.println(echostr);out.flush();out.close();} else {System.out.println("fail");}System.out.println("=========== end ============");}/*** 拿到参数的接入的方法是:* 通过对应的参数的排序之后进行验证,把最后的一个数,赋值特定含义进行返回**/}
加密的部分代码:
package cn.itechyou.cms.controller;import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;import mons.collections.Buffer;import org.ponent;import java.security.MessageDigest;import java.security.NoSuchAlgorithmException;import java.util.Arrays;import static mons.codec.digest.DigestUtils.sha1;/*** @author liuYC* @ClassName WeixinService* @Description TODO* @date /8/12 10:30*/@Componentpublic class WeixinService {public boolean checkSignature(String signature, String timestamp, String nonce, String token) {/*** 1)将 token、timestamp、nonce 三个参数进行字典序排序* 2)将三个参数字符串拼接成一个字符串进行sha1加密* 3)开发者获得加密后的字符串可与 signature 对比,标识该请求来源于微信*/final String[] strings = {token, timestamp, nonce};// 1)将 token、timestamp、nonce 三个参数进行字典序排序Arrays.sort(strings);String str = strings[0] + strings[1] + strings[2];//2)将三个参数字符串拼接成一个字符串进行sha1加密String results = sha11(str);System.out.println("========加密结果==========");System.out.println(results.equals(signature));//return results.equals(signature);}private static String sha11(String s) {final StringBuilder stringBuilder = new StringBuilder();try {char[] chars = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};MessageDigest md = MessageDigest.getInstance("sha1");byte[] bytes = md.digest(s.getBytes());for (byte b : bytes) {// 高四位stringBuilder.append(chars[(b >> 4) & 15]);// 低四位stringBuilder.append(chars[b & 15]);}} catch (NoSuchAlgorithmException e) {e.printStackTrace();}return stringBuilder.toString();}}
结果显示:成功之后,提交按钮就会消失:
参考视频:
/video/BV1nb411P76t?p=5
如果觉得《微信公众号的开发:基于Java版本的服务器(1)》对你有帮助,请点赞、收藏,并留下你的观点哦!