java获取微信signature

  • 先将自己的服务器IP设置微信白名单,否则会一直返回
{"errcode":40164,"errmsg":"invalid ip ********** ipv6 ::ffff:*********, not in whitelist hint: [vyRt9a0380b101]"}
  • 微信获取signture工具类
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

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

/**
 * @Author: yangyang
 * @Date: 2020-02-29 23:43
 * @Description:
 */
public class WeChatUtils {



    public static JSONObject getWeChatSignature(String url) {

        String appid =  Constant.WX_ACCOUNT_APPID;
        String secret = Constant.WX_ACCOUNT_SECRET;

        String accessToken = getAccessToken(appid,secret);
        String jsapiTicket = getTicket(accessToken);
        String noncestr = UUID.randomUUID().toString().replace("-", "").substring(0, 16);
        String timestamp = String.valueOf(System.currentTimeMillis() / 1000);

        String str = "jsapi_ticket="+jsapiTicket+"&noncestr="+noncestr+"×tamp="+timestamp+"&url=" + url;

        //6、将字符串进行sha1加密
        String signature =SHA1(str);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("access_token", accessToken);
        jsonObject.put("jsapi_ticket", jsapiTicket);
        jsonObject.put("noncestr", noncestr);
        jsonObject.put("timestamp", timestamp);
        jsonObject.put("signature", signature);
        return jsonObject;
    }

    /**
     * 获取access_token
     * @param appid
     * @param secret
     * @return
     */
    public static String getAccessToken(String appid,String secret) {
        String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential" + "&appid=" + appid + "&secret=" + secret;
        String result = HttpUtil.createGet(url).execute().body();
        JSONObject jsonObject = JSON.parseObject(result);
        System.out.println(result);
        return jsonObject.getString("access_token");
    }

    /**
     * 获取ticket
     * @param access_token
     * @return
     */
    public static String getTicket(String access_token) {
        String url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token="+ access_token +"&type=jsapi";
        String result = HttpUtil.createGet(url).execute().body();
        System.out.println(result);
        JSONObject jsonObject = JSON.parseObject(result);
        return jsonObject.getString("ticket");
    }

    public static String SHA1(String decript) {
        try {
            MessageDigest digest = java.security.MessageDigest.getInstance("SHA-1");
            digest.update(decript.getBytes());
            byte messageDigest[] = digest.digest();
            // Create Hex String
            StringBuffer hexString = new StringBuffer();
            // 字节数组转换为 十六进制 数
            for (int i = 0; i < messageDigest.length; i++) {
                String shaHex = Integer.toHexString(messageDigest[i] & 0xFF);
                if (shaHex.length() < 2) {
                    hexString.append(0);
                }
                hexString.append(shaHex);
            }
            return hexString.toString();

        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "";
    }
}

你可能感兴趣的:(java获取微信signature)