Java后端集成JSJDK禁用分享

话不多说,上代码

WxService


import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import free.system.service.monitor.WxService;
import free.system.utils.HttpUtils;
import free.system.vo.RetObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Date;

/**
 * @author Administrator
 */
@Service
public class WxServiceImpl implements WxService {

    private static String string = "abcdefghijklmnopqrstuvwxyz";

    private static String appId = "你的appid";


    @Override
    public RetObject disableShare(String url) throws IOException {
        String access_token = GetUrlS();
        String ticket= "";
        String access_tokenResult = HttpUtils.sendGet("https://api.weixin.qq.com/cgi-bin/token", "grant_type=client_credential&appid="+appId+"&secret=你的secret");
        JsonParser jsonParser =new JsonParser();  //创建json解析器
        JsonObject jsonObject = (JsonObject)jsonParser.parse(access_tokenResult);
        access_token = jsonObject.get("access_token").getAsString();
        String jsapi_ticketResult = HttpUtils.sendGet("https://api.weixin.qq.com/cgi-bin/ticket/getticket", "access_token="+access_token+"&type=jsapi");
        System.out.println(jsapi_ticketResult);
        jsonObject = (JsonObject)jsonParser.parse(jsapi_ticketResult);
        System.out.println("jsapi_ticketResult:" +jsapi_ticketResult);
        System.out.println(jsonObject);
        ticket = jsonObject.get("ticket").getAsString();
        String nonceStr = getRandomString(20);
        long timestamp = new Date().getTime()/1000;
        String string1 = "jsapi_ticket="+ticket+"&noncestr="+nonceStr+"&timestamp="+timestamp+"&url="+url;
        System.out.println(string1);
        String signature = getSha1(string1);
        RetObject retObject = new RetObject(appId, timestamp+"", nonceStr, signature);
        return retObject;
    }

    /**
     * 获取access_token
     * @return
     * @throws ClientProtocolException
     * @throws IOException
     */
    public static String GetUrlS() throws ClientProtocolException, IOException {
        HttpGet httpGet = new HttpGet(
                "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="
                        + "你的appid" + "&secret=" + "你的secret");
        HttpClient httpClient = HttpClients.createDefault();
        HttpResponse res = httpClient.execute(httpGet);
        HttpEntity entity = res.getEntity();
        String result = EntityUtils.toString(entity, "UTF-8");
        JSONObject jsons = JSONObject.parseObject(result);
        String expires_in = jsons.getString("expires_in");

        //缓存
        if (Integer.parseInt(expires_in) == 7200) {
            //ok
            String access_token = jsons.getString("access_token");
            System.out.println("access_token:" + access_token);
            return access_token;
        } else {
            System.out.println("出错获取token失败!");
        }
        return null;
    }

    private static String getRandomString(int length){
        StringBuffer sb = new StringBuffer();
        int len = string.length();
        for (int i = 0; i < length; i++) {
            sb.append(string.charAt(getRandom(len-1)));
        }
        return sb.toString();
    }

    public static String getSha1(String str){
        if (null == str || 0 == str.length()){
            return null;
        }
        char[] hexDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
                'a', 'b', 'c', 'd', 'e', 'f'};
        try {
            MessageDigest mdTemp = MessageDigest.getInstance("SHA1");
            mdTemp.update(str.getBytes("UTF-8"));

            byte[] md = mdTemp.digest();
            int j = md.length;
            char[] buf = new char[j * 2];
            int k = 0;
            for (int i = 0; i < j; i++) {
                byte byte0 = md[i];
                buf[k++] = hexDigits[byte0 >>> 4 & 0xf];
                buf[k++] = hexDigits[byte0 & 0xf];
            }
            return new String(buf);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return "";
    }

    private static int getRandom(int count) {
        return (int) Math.round(Math.random() * (count));
    }

}

 

RetObject
package free.system.vo;

import io.swagger.annotations.ApiModel;

/**
 * @author Administrator
 */
@ApiModel("微信禁用分享返回数据")
public class RetObject {

    private String appId;
    private String timestamp;
    private String nonceStr;
    private String signature;

    public RetObject(String appId, String timestamp, String nonceStr, String signature) {
        super();
        this.appId = appId;
        this.timestamp = timestamp;
        this.nonceStr = nonceStr;
        this.signature = signature;
    }

    public String getAppId() {
        return appId;
    }

    public void setAppId(String appId) {
        this.appId = appId;
    }

    public String getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(String timestamp) {
        this.timestamp = timestamp;
    }

    public String getNonceStr() {
        return nonceStr;
    }

    public void setNonceStr(String nonceStr) {
        this.nonceStr = nonceStr;
    }

    public String getSignature() {
        return signature;
    }

    public void setSignature(String signature) {
        this.signature = signature;
    }
}

 

 

各位老板给小弟买个饮料喝吧,感谢Thanks♪(・ω・)ノ

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值