java后端获取微信jssdk并生成签名

1.主体代码

/**
	 * 获取jssdk
	 * @param url
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getWechatJSSDK",method={RequestMethod.POST,RequestMethod.GET})
	public @ResponseBody Object getWechatJSSDK(String url) throws Exception {
		Map map =  new HashMap<>();
        String access_token = "";
        //拼接地址获取token
        String tokenStr  = HttpRequestUtil.sendGet("https://api.weixin.qq.com/cgi-bin/token","grant_type=client_credential&appid="+miniProAppId+"&secret="+miniProAppSecret);
        JSONObject token = JSON.parseObject(tokenStr);
        access_token = token.getString("access_token");
        //获取jsapi
        String jsapiStr  = HttpRequestUtil.sendGet("https://api.weixin.qq.com/cgi-bin/ticket/getticket","access_token="+access_token+"&type=jsapi");
        JSONObject jsapi = JSON.parseObject(jsapiStr);      
        String jsapi_ticket = jsapi.getString("ticket"); 
        /*****************获取签名signature********************/
        //生成随机字符串  noncestr
        String nonceStr = UUID.randomUUID().toString();
        //时间戳 timestamp,精确到秒 
        String timestamp = Long.toString(System.currentTimeMillis() / 1000);
        //url
        //String Url = url;
        String Stitching = "jsapi_ticket="+jsapi_ticket+"&noncestr="+nonceStr+"×tamp="+timestamp+"&url="+url;
        System.out.println("Stitching:"+Stitching);
        //SHA1 加密
        java.security.MessageDigest digest = java.security.MessageDigest.getInstance("SHA-1");        
        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", miniProAppId);
        map.put("timestamp", timestamp);
        map.put("nonceStr", nonceStr);
        map.put("signature", hexStr.toString());
        return AppUtil.returnObject(map,Const.SUCCESSCODE);
	}

注:(1).这里的url是由前端传过来的

(2).常量miniProAppId为appid,miniProAppSecret为secret

2.HttpRequestUtil.sendGet()方法代码:

/**
	 * 向指定URL发送GET方法的请求
	 * 
	 * @modificationHistory.
	 * @param url
	 *            发送请求的URL
	 * @param param
	 *            请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
	 * @return URL 所代表远程资源的响应结果
	 */
	public static String sendGet(String url, String param) {
		StringBuffer result = new StringBuffer("");
		BufferedReader in = null;
		try {
			String urlNameString = url + "?" + param;
			URL realUrl = new URL(urlNameString);
			// 打开和URL之间的连接
			URLConnection connection = realUrl.openConnection();
			// 设置通用的请求属性
			connection.setRequestProperty("accept", "*/*");
			connection.setRequestProperty("connection", "Keep-Alive");
			connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
			// 建立实际的连接
			connection.connect();
			// 定义 BufferedReader输入流来读取URL的响应
			in = new BufferedReader(new InputStreamReader(connection.getInputStream(),"UTF-8"));
			String line;
			while ((line = in.readLine()) != null) {
				result.append(line);
			}
		} catch (Exception e) {
			// System.out.println("发送GET请求出现异常!" + e);
			e.printStackTrace();
			return "400";
		}
		// 使用finally块来关闭输入流
		finally {
			try {
				if (in != null) {
					in.close();
				}
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
		return result.toString();
	}

你可能感兴趣的:(Java,WeChat,java,微信,开发语言)