微信公众号分享签名

  @ApiOperation(value="微信公众号分享签名", notes="url")
    @PostMapping(value="/getTicket")
    public Result<Map<String,String>> getTicket(String url){
        String urlss = "https://api.weixin.qq.com/cgi-bin/ticket/getticket";
        String urls = "https://api.weixin.qq.com/cgi-bin/token";
        String appid = "";
        String secret = "";
        String accessToken = WxTkenUtil.getAccessToken(urls, "client_credential", appid, secret);
        if(accessToken == null){
            return  Result.error("access_token获取失败");
        }
        String tokenUrl = urlss +"?access_token="+accessToken+"&type=jsapi";
        JSONObject json = RestUtil.get(tokenUrl);
        String jsapi_ticket = json.getString("ticket");

        //生成随机字符串  noncestr
        String nonceStr = UUID.randomUUID().toString();
        //时间戳 timestamp,精确到秒
        String timestamp = Long.toString(System.currentTimeMillis() / 1000);
        HashMap<String,String> map = new HashMap();
        String Stitching = "jsapi_ticket="+jsapi_ticket+"&noncestr="+nonceStr+"&timestamp="+timestamp+"&url="+url;
        System.out.println("Stitching:"+Stitching);
        //SHA1 加密
        java.security.MessageDigest digest = null;
        try {
            digest = java.security.MessageDigest.getInstance("SHA-1");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        digest.update(Stitching.getBytes());
        byte messageDigest[] = digest.digest();
        StringBuffer hexStr = new StringBuffer();
        // 字节数组转换为 十六进制 数
        for (int i = 0; i < messageDigest.length; i++) {
            String shaHex = Integer.toHexString(messageDigest[i] & 0xFF);
            if (shaHex.length() < 2) {
                hexStr.append(0);
            }
            hexStr.append(shaHex);
        }
        map.put("appid", appid);
        map.put("timestamp", timestamp);
        map.put("nonceStr", nonceStr);
        map.put("signature", hexStr.toString());

        return Result.OK("请求成功",map);
    }
package org.jeecg.modules.util;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.util.RestUtil;
import org.jeecg.config.sign.util.HttpUtils;

import javax.xml.transform.Result;

public class WxTkenUtil {

    public static String getAccessToken(String url,String grantType,String appid,String secret){
        String access_token ="";
        String tokenUrl = url +"?grant_type="+grantType+"&appid="+appid +"&secret="+ secret;
        Object result = RestUtil.get(tokenUrl);
        JSONObject jsons = JSONObject.parseObject(result.toString());
        String expires_in = jsons.getString("expires_in");
        if(StringUtils.isNotEmpty(expires_in) && Integer.parseInt(expires_in) == 7200){
            access_token = jsons.getString("access_token");
        }
        return access_token;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值