如何寻找微信中的微信官网,微信怎么找官网

首页 > 实用技巧 > 作者:YD1662023-10-26 18:14:07

这里我创建了一个Controller和新建了一个RestApi接口,结合上面截图和下面截图红框部分,你再看看我的URL地址http://r7fkc2.natappfree.cc/weChart/connect 你应该好像明白了点什么,对我们配置了这个接口,那问题来了,为什么我们配置的是这个接口而不是别的接口呢,因为我们配置的这个接口第一次提交保存时候,腾讯微信公众号会给你发送一些消息,你需要对这些消息接收后做出符合要求的逻辑处理并作出正确的回应,腾讯微信公众号才会成功认证他作为唯一的合法URL,否则你是无法腾讯微信服务器形成有效的通讯通道进行通讯的,你现在明白了吧;那么这个接口里的东西可不能乱写,下面我直接贴代码如下:

WechatController.java

package com.xu.wemall.controller.wechat; import com.xu.wemall.commons.utils.CheckUtil; import com.xu.wemall.commons.utils.UploadUtil; import com.xu.wemall.commons.utils.MessageUtil; import io.swagger.annotations.Api; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.PrintWriter; import java.util.Map; /** * 类名称: LoginController * 类描述: 与微信对接登陆验证 * * @author yuanjun * 创建时间:2017年12月5日上午10:52:13 */ @Slf4j @RestController @Api(tags = "接入验证接口") @RequestMapping(value = "/weChart") public class WechatController { @RequestMapping(value = "/connect", method = RequestMethod.GET) public String connect(@RequestParam(value = "signature") String signature, @RequestParam(value = "timestamp") String timestamp, @RequestParam(value = "nonce") String nonce, @RequestParam(value = "echostr") String echostr) { log.info("-----开始校验签名-----"); PrintWriter out = null; if (CheckUtil.checkSignature(signature, timestamp, nonce)) { log.info("-----签名校验通过-----"); return echostr; } else { log.info("-----校验签名失败-----"); return null; } } }

CheckUtil.java

package com.xu.wemall.commons.utils; import java.util.Arrays; public class CheckUtil { //自己设置,要与微信页面设置的一致 private static final String token = "xulijun137"; public static boolean checkSignature(String signature,String timestamp,String nonce){ String[] str = new String[]{token,timestamp,nonce}; //排序 Arrays.sort(str); //拼接字符串 StringBuffer buffer = new StringBuffer(); for(int i =0 ;i<str.length;i ){ buffer.append(str[i]); } //进行sha1加密 String temp = SHA1.encode(buffer.toString()); //与微信提供的signature进行匹对 return signature.equals(temp); } }

SHA1.java

package com.xu.wemall.commons.utils; import java.security.MessageDigest; /** * 类名称: SHA1 * 类描述: sha1加密 * * @author yuanjun * 创建时间:2017年12月5日上午11:10:01 */ public final class SHA1 { private static final char[] HEX_DIGITS = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; /** * Takes the raw bytes from the digest and formats them correct. * * @param bytes the raw bytes from the digest. * @return the formatted bytes. */ 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(); } public static String encode(String str) { if (str == null) { return null; } try { MessageDigest messageDigest = MessageDigest.getInstance("SHA1"); messageDigest.update(str.getBytes()); return getFormattedText(messageDigest.digest()); } catch (Exception e) { throw new RuntimeException(e); } } }

如何寻找微信中的微信官网,微信怎么找官网(9)

上面的Token请换成自己的token,在你的【开发】--》【基本配置】中你之前自己配置过的Token,上面的接口的基本思路是获取腾讯服务器发送到四个参数你项目接口的,你对这四个参数做还有token一起5个参数按照公众号提供的文档说明处理后,满足条件后返回其中一个参数即可,具体请看我代码部分,注意这个接口是GET请求方式的,因此得到我们的项目RestAPI接口地址是:http:/localhost:80/weChat/connnect,再看我配置的地址http://r7fkc2.natappfree.cc/weChart/connect

这里显然我们配了一个域名地址http://r7fkc2.natappfree.cc/【这里默认设置的是80端口,80端口可以不写】这是我们利用NATAPP将本地的127.0.0.1映射成的一个外网可访问的域名地址【这里就是我们上一课讲的用NATAPP将本地地址映射成外网地址,不清楚的同学请先学习我的上一课,映射本地地址到外网的工具还有其他的,目前笔者最推荐是NATAPP】,通过这个外网的域名地址,腾讯的服务器可以成功连接到我们的项目,因为你开发的话你的本地地址localhost腾讯微信公众号服务器是不可能认识的( 后续你可以把项目部署到外网服务器上,比如阿里云,百度云,腾讯云服务器上),

至此我们就成功得到这个URL地址了,配置后保存即对我们的接口做合法性验证,验证成功会弹出弹窗框提示你验证成功,否则表示你的接口逻辑处理后返回的字符串不符合接口要求,请修改代码后重新配置验证接口,好了今天的课就讲到这里了,下节课再见,如有问题请给我留言。

上一页123末页

栏目热文

文档排行

本站推荐

Copyright © 2018 - 2021 www.yd166.com., All Rights Reserved.