java实现微信公众号token验证

微信公众号token验证

查看了一些微信公众号平台的开发文档,得知签名验证需要以下参数
在这里插入图片描述
签名验证规则是:
1.开发者服务端获取得到signature、timestamp、nonce、echostr参数。
2.然后将token、timestamp、nonce三个参数进行字典序排序 。token字段是服务端填写的,需同公众平台填写的token保持一致。
3.将token、timestamp、nonce这三个参数拼接成一个字符串并按照sha1的算法进行加密,得到一个加密字符串。
4.并将该字符串与获得的signature参数进行比较,如若相同,将echostr参数内容原样返回,代表接入生效,否则失败。

开发步骤

进入微信公众平台,填写接口配置信息

填写请求路径:
http://shoupolan.free.idcfengye.com/weixin/wx/
token:
youjp
在这里插入图片描述
域名映射可查看上一篇博客:
Ngork实现内网穿透:
https://blog.youkuaiyun.com/qq_36654629/article/details/91864899

新建一个weixin项目,添加依赖

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.jp</groupId>
  <artifactId>weixin_day1</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
   <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
    </parent>
 
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
              <groupId>junit</groupId>
              <artifactId>junit</artifactId>
              <version>3.8.1</version>
              <scope>test</scope>
        </dependency>
    </dependencies>
 
    <properties>
        <java.version>1.8</java.version>
    </properties>
 
</project>

yml配置

server:
   port:   8080
   context-path:  /weixin
         
      
spring:
   application:
      name:   weixin   

签名校检工具类

package com.weixin.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * 签名认证工具类
 * @ClassName:  WeixinCheckoutUtil   
 * @Description:TODO
 * @author: jp
 * @date:   2019年6月13日 下午4:56:12   
 *     
 * @Copyright: 2019 www.tydic.com Inc. All rights reserved. 
 *
 */
public class WeixinCheckoutUtil {

	   // 与接口配置信息中的Token要一致
    private static String token = "youjp";
 
    /**
     * 验证签名
     * 
     * @param signature
     * @param timestamp
     * @param nonce
     * @return
     */
    public static boolean checkSignature(String signature, String timestamp, String nonce) {
        String[] arr = new String[] { token, timestamp, nonce };
        // 将token、timestamp、nonce三个参数进行字典序排序
        // Arrays.sort(arr);
        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;
    }
 
    /**
     * 将字节数组转换为十六进制字符串
     * 
     * @param byteArray
     * @return
     */
    private static String byteToStr(byte[] byteArray) {
        String strDigest = "";
        for (int i = 0; i < byteArray.length; i++) {
            strDigest += byteToHexStr(byteArray[i]);
        }
        return strDigest;
    }
 
    /**
     * 将字节转换为十六进制字符串
     * 
     * @param mByte
     * @return
     */
    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;
    }
    public static void sort(String a[]) {
        for (int i = 0; i < a.length - 1; i++) {
            for (int j = i + 1; j < a.length; j++) {
                if (a[j].compareTo(a[i]) < 0) {
                    String temp = a[i];
                    a[i] = a[j];
                    a[j] = temp;
                }
            }
        }
    }

}

服务控制器

package com.weixin.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.weixin.util.WeixinCheckoutUtil;

/**
 * 微信token获取
 * @ClassName:  TestController   
 * @Description:TODO
 * @author: jp
 * @date:   2019年6月13日 下午4:01:32   
 *     
 * @Copyright: 2019 www.tydic.com Inc. All rights reserved. 
 *
 */

@RestController
public class WeixinCheckController {
	
	/**
	 * 微信公众号签名认证接口
	 * @Title: test   
	 * @Description: TODO
	 * @param: @param signature
	 * @param: @param timestamp
	 * @param: @param nonce
	 * @param: @param echostr
	 * @param: @return      
	 * @return: String      
	 * @throws	
	 */
	@RequestMapping("/wx")
	public String test(String signature,String timestamp,String nonce,String echostr) {
	
        // 通过检验signature对请求进行校验,若校验成功则原样返回echostr,表示接入成功,否则接入失败
		 if (signature != null && WeixinCheckoutUtil.checkSignature(signature, timestamp, nonce)) {
           return echostr;
         }

		return null;
	}

}

启动类

package com.weixin;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class WeixinApplication_APP {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

	SpringApplication.run(WeixinApplication_APP.class, args);
	}

}

启动服务,并记得打开Ngork客户端将域名映射开启。我的域名映射端口服务为8080,项目端口也是8080。
在这里插入图片描述
点击提交
在这里插入图片描述

配置成功如下:

在这里插入图片描述

有兴趣的老爷,可以关注我的公众号【一起收破烂】,回复【006】可获取2021最新java面试资料以及简历模型120套哦~
在这里插入图片描述

### 使用Java开发微信公众号消息推送接口教程 #### 准备工作 为了能够通过Java微信公众号发送模板消息,前期准备必不可少。这包括但不限于注册成为微信公众平台开发者并创建应用以获得`appId`和`appSecret`。另外,由于授权页面需在微信客户端内打开,因此建议使用微信提供的开发工具,并设置本地测试环境为`127.0.0.1/getCode`以便于调试[^2]。 #### 获取Access Token 每次调用微信API之前都需要先获取到有效的`access_token`,这是访问大多数微信API所必需的身份验证令牌。可以通过HTTP GET请求的方式从指定URL中取得该token: ```java public String getAccessToken(String appId, String appSecret){ String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+appId+"&secret="+appSecret; OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder().url(url).build(); try (Response response = client.newCall(request).execute()) { JSONObject jsonObject = new JSONObject(response.body().string()); return jsonObject.getString("access_token"); } catch (IOException e) { throw new RuntimeException(e); } } ``` 此方法返回的字符串即为所需的`access_token`[^1]。 #### 组装模板消息数据 构建要发送的信息体时需要注意参数的选择与组合。特别是对于`openId`(目标用户的唯一标识)、`template_id`(预定义的消息模板ID),以及可选字段如链接地址(`url`)或小程序路径(`miniprogram`)等。如果两者均被设定,则优先考虑跳转至小程序;但如果用户设备不支持这一特性,则会转向给定的网页链接[^3]。 以下是用于构造JSON格式的数据包的一个简单例子: ```json { "touser": "OPENID", "template_id": "TEMPLATE_ID", "page": "index", "form_id": "FORMID", "data": { "keyword1": {"value": "商品名称"}, "keyword2": {"value": "订单号"} }, "emphasis_keyword": "" } ``` 请注意,在实际编码过程中应当将上述占位符替换为真实的变量值。 #### 发送请求 最后一步就是利用已有的信息去发起HTTPS POST请求,从而完成整个流程。这里给出一段完整的示例代码片段展示如何执行这项操作: ```java import okhttp3.*; // ...其他必要的导入语句... public void sendTemplateMessage(String accessToken, Map<String,Object> messageData){ MediaType JSON = MediaType.parse("application/json; charset=utf-8"); String jsonBody = new Gson().toJson(messageData); // 假设messageData已经包含了所有必要键值对 HttpUrl url = new HttpUrl.Builder() .scheme("https") .host("api.weixin.qq.com") .addPathSegment("cgi-bin") .addPathSegment("message") .addPathSegment("template") .addPathSegment("send") .addQueryParameter("access_token", accessToken) .build(); RequestBody body = RequestBody.create(jsonBody, JSON); Request request = new Request.Builder() .url(url) .post(body) .build(); OkHttpClient okHttpClient = new OkHttpClient(); Call call = okHttpClient.newCall(request); try(Response response = call.execute()){ System.out.println(response.body().string()); // 输出服务器响应结果供查看 }catch(IOException e){ e.printStackTrace(); } } ``` 这段程序展示了怎样把前面几步收集起来的信息打包成一个标准的POST请求,并最终将其提交给微信服务器处理[^4]。
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

收破烂的小熊猫~

你的鼓励将是我创造最大的东西~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值