java 获取公众号签名signature,微信JS-SDK中config接口注入权限验证配置

wxjssdk类

package com.cdtskj.ext.wls.weixin.action;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//import com.cdtskj.ext.wls.weixin.action.StringUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.net.HttpURLConnection;
public class wxjssdk extends HttpServlet {


    /**
     * 通过code换取网页授权access_token,根据授权返回页面获取的code值获取openid
     */
    protected void doPost(HttpServletRequest request,
                          HttpServletResponse response) throws ServletException, IOException {

//        StringUtil st = new StringUtil();
        String appid =  "wxf3ae33747a5bd16c";
        String secret = "3083ab3b68567e2f0f341e9fa28db02e";

        //System.out.println("appid="+appid);
        //System.out.println("secret="+secret);
        String url=request.getParameter("wxurl");
        try {
           /* Map map2  = WxUtil.oppen_id(request, request.getSession());
            String oppen_id = (String) map2.get("oppen_id");
            String accessToken  = (String) map2.get("access_token");*/
            String accessToken = getAccessToken(appid,secret);
            //2、获取Ticket
            String jsapi_ticket = getTicket(accessToken);

            //3、时间戳和随机字符串
            String noncestr = UUID.randomUUID().toString().replace("-", "").substring(0, 16);//随机字符串
            String timestamp = String.valueOf(System.currentTimeMillis() / 1000);//时间戳

            System.out.println("accessToken:"+accessToken+"\njsapi_ticket:"+jsapi_ticket+"\n时间戳:"+timestamp+"\n随机字符串:"+noncestr);

            //4、获取url
            //String url="http://shuiqitong.com/xzw/jNotice/jNotice_templet/templet.jsp";
            /*根据JSSDK上面的规则进行计算
            String[] ArrTmp = {"jsapi_ticket","timestamp","nonce","url"};
            Arrays.sort(ArrTmp);
            StringBuffer sf = new StringBuffer();
            for(int i=0;i= 19968 && chr1 <= 171941) {// 汉字范围 \u4e00-\u9fa5 (中文)
                result = true;
            }
        }
        return result;
    }


}

web.xml


	wxjssdk
	com.weixin.controller.wxjssdk


	wxjssdk
	/wxjssdk

jsp

 function ws_values(){
            //自动获取url
            var url = location.href.split('#').toString();//url不能写死

            var path = "<%=request.getContextPath() %>/wxjssdk";
            $.post(path,{wxurl:url},function(data){
                var data=JSON.parse(data);
                l_timestamp=data[0];//
                l_noncestr=data[1];
                l_signature=data[2];
                appid=data[4];
                wx.config({
                    debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
                    appId: appid, // 必填,公众号的唯一标识
                    timestamp:l_timestamp, // 必填,生成签名的时间戳,上文通过后台获取的
                    nonceStr: l_noncestr, // 必填,生成签名的随机串,上文通过后台获取的
                    signature: l_signature,// 必填,签名,上文通过后台获取的
                    jsApiList: ['onMenuShareTimeline','onMenuShareAppMessage','checkJsApi', 'openLocation', 'getLocation'] // 必填,需要使用的JS接口列表,就是下文的分享朋友圈和转发给朋友的列表
                });
            });
        }

 

你可能感兴趣的:(java 获取公众号签名signature,微信JS-SDK中config接口注入权限验证配置)