微信JSSDKconfig接口注入权限验证配置的参数获取--java版

参考文档
微信官网文档

步骤看官网文档,这里只有java后台代码

签名工具类

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

/**
 * @author xmg
 * @Description 签名工具类
 * @Date:2019/5/7
 */
public class SignUtil {

    /**
     * 前端jssdk页面配置需要用到的配置参数
     * @param url
     * @return {appid,timestamp,nonceStr,signature}
     * @throws Exception
     */
    public static HashMap jsSDK_Sign(String url) throws Exception {
        String nonce_str = create_nonce_str();
        String timestamp= System.currentTimeMillis()/1000 + "";
        String jsapi_ticket= WxService.getJsSdkTicket();
        // 注意这里参数名必须全部小写,且必须有序
        // 参数
        Map packageParams = new HashMap<>();
        packageParams.put("url", url);
        packageParams.put("noncestr", nonce_str);
        packageParams.put("jsapi_ticket", jsapi_ticket);
        packageParams.put("timestamp", timestamp);

        // 获得拼接好的参数,按照ASCLL大小排序
        String createLinkString = createLinkString(packageParams);
        String signature = SHA1(createLinkString);
        // 参数封装,返回前台
        HashMap jssdk=new HashMap();
        jssdk.put("appId", WxService.APP_ID);
        jssdk.put("timestamp", timestamp);
        jssdk.put("nonceStr", nonce_str);
        jssdk.put("signature", signature);
        return jssdk;
    }

    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 "";
    }
    /**
     * 把数组所有元素排序,并按照“参数=参数值”的模式用“&”字符拼接成字符串
     * @param params 需要排序并参与字符拼接的参数组
     * @return 拼接后字符串
     */
    public static String createLinkString(Map params) {
        List keys = new ArrayList(params.keySet());
        Collections.sort(keys);
        String prestr = "";
        for (int i = 0; i < keys.size(); i++) {
            String key = keys.get(i);
            String value = params.get(key);
            if (i == keys.size() - 1) {// 拼接时,不包括最后一个&字符
                prestr = prestr + key + "=" + value;
            } else {
                prestr = prestr + key + "=" + value + "&";
            }
        }
        return prestr;
    }

    private static String create_nonce_str() {
        return UUID.randomUUID().toString();
    }
}

/**
 * @author xmg
 * @Description Ticket封装
 * @Date:2019/5/7 0:31
 */
public class Ticket {
    /** ticket */
    private String ticket;
    /** 过期时间 */
    private long expiresTime;

    public String getTicket() {
        return ticket;
    }

    public void setTicket(String ticket) {
        this.ticket = ticket;
    }

    public long getExpiresTime() {
        return expiresTime;
    }

    public void setExpiresTime(long expiresTime) {
        this.expiresTime = expiresTime;
    }

    public Ticket(String ticket, String expiresIn) {
        super();
        this.ticket = ticket;
        this.expiresTime = System.currentTimeMillis() + Integer.parseInt(expiresIn)*1000;
    }

    /**
     * 判断token是否过期
     * @return
     */
    public boolean isExpired(){
        return System.currentTimeMillis() > this.expiresTime;
    }
}

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.*;

/**
 * @author xmg
 * @Description
 * @email [email protected]
 * 

* Date:2019/4/5 15:25 */ @Service public class WxService { private final static Logger LOGGER = LoggerFactory.getLogger(WxService.class); /** 获取ticket地址 */ private final static String GET_TICKET_URL = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=ACCESS_TOKEN&type=jsapi"; /** appid */ public final static String APP_ID = "你自己appid"; /** appsecret */ public final static String APP_SECRET = "你自己的secret"; private static Ticket TICKET; /** * 获取ticket */ private static void getTicket(){ // RestTemplate RestTemplate restTemplate = new RestTemplate(); String url = GET_TICKET_URL.replace("ACCESS_TOKEN",getAccessToken()); String forObject = restTemplate.getForObject(url, String.class); JSONObject jsonObject = JSONUtil.parseObj(forObject); String ticket = jsonObject.getStr("ticket"); String expiresIn = jsonObject.getStr("expires_in"); // 创建token对象,并存起来 TICKET = new Ticket(ticket,expiresIn); } /** * 向外暴露的获取Ticket方法 * @return */ public static String getJsSdkTicket(){ if (TICKET == null || TICKET.isExpired()){ getTicket(); } return TICKET.getTicket(); } }

你可能感兴趣的:(微信JSSDKconfig接口注入权限验证配置的参数获取--java版)