需要分页的网页引入下面jsp
weixin.jsp
<%--
Created by IntelliJ IDEA.
User:
Date: 2019/2/20
Time: 15:12
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%--<%@ include file="/WEB-INF/views/modules/cms/front/include/taglib.jsp"%>--%>
<%----%>
<%----%>
后台获取签名授权等信息的方法
下面的签名每天获取有次数限制,微信的签名2个小时内有效, 此处我用的是Memcached缓存起来,各位可以自行实现
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.memcached.MemcachedUtil;
import com.thinkgem.jeesite.common.utils.CacheUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
/**
* 微信分享
*/
@Controller
@RequestMapping(value = "/f/weixinShare")
public class CourseWeixinShare extends BaseController {
// 微信分享入口
@RequestMapping(value = "getWxShareData")
@ResponseBody
public Map getWxShareData(HttpServletRequest request, HttpServletResponse response) throws Exception {
Map returnMap = new HashMap<>();
String ticket = "";
String[] wxInfo = new String[]{Global.getConfig("appId"),Global.getConfig("appsecret")};//设置appid 和 appsecret
String ticketResString = this.getShareJsapiTicket(wxInfo);
if (StringUtils.isNotEmpty(ticketResString)) {
// JSONObject ticketJSONObject = JSONObject.fromObject(ticketResString);
Gson gson =new Gson();
Map ticketJSONObject = gson.fromJson(ticketResString,Map.class);
if ((Double)ticketJSONObject.get("errcode") == 0) {
// ticket = JSONObject.fromObject(ticketResString).optString("ticket", "");
if(ticketJSONObject.get("ticket")!=null){
ticket = (String) ticketJSONObject.get("ticket");
}
}
}
if (StringUtils.isEmpty(ticket)) {
returnMap.put("errcode", "10002");
returnMap.put("errmsg", "ticket_error");
// this.responseWrite(jsonObject.toString());
return returnMap;
}
String noncestr = this.createNonceStr();
int timestamp = this.createTimestamp();
String requestRefererURL = request.getHeader("referer");
logger.warn("requestRefererURL: " + requestRefererURL);
String signature = this.createSignature(noncestr, ticket, timestamp, requestRefererURL);
returnMap.put("errcode", 0);
returnMap.put("errmsg", "");
returnMap.put("appId", wxInfo[0]); // appId
returnMap.put("timestamp", timestamp);
returnMap.put("nonceStr", noncestr);
returnMap.put("signature", signature);
return returnMap;
}
/**
* 微信分享,获取Js api Ticket
* @param wxInfo
* @return
* @throws Exception
*/
private String getShareJsapiTicket(String[] wxInfo) throws Exception {
String jsapiTicket = null;
if(CacheUtils.get("jsapiTicket_addTime") !=null &&( System.currentTimeMillis() - (Long) CacheUtils.get("jsapiTicket_addTime"))<7080*1000){//不超过2个小时的缓存有效
jsapiTicket = (String) CacheUtils.get("jsapiTicket");
this.logger.warn(" from memcached jsapiTicket: " + jsapiTicket);
return jsapiTicket;
}else{
String accessToken = this.getWeiXinAccessToken(wxInfo);
if (StringUtils.isEmpty(accessToken)) { // 获取 accessToken 失败
this.logger.warn(" accessToken is empty.");
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("errcode", "10000");
jsonObject.addProperty("errmsg", "access_error");
return jsonObject.toString();
}
String url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token="+ accessToken + "&type=jsapi";
jsapiTicket = this.httpReqExecute(url);
this.logger.warn(" from weixin api jsapiTicket is: " + jsapiTicket);
if(StringUtils.isNotEmpty(jsapiTicket)) {
// 向memcached里写内容,第二个参数为过期时间,单位为:秒 两个小时失效
// remoteMemcachedClient.set(siteId + "_jsapiTicket", 7200, jsapiTicket);
CacheUtils.put("jsapiTicket",jsapiTicket);
CacheUtils.put("jsapiTicket_addTime",System.currentTimeMillis());//设置 缓存的时间
return jsapiTicket;
}
return null;
}
}
/**
* 微信分享,获取access_token
* 先从memcached获取
* @param wxInfo 0:appId 1:secret
* @return
* @throws Exception
*/
private String getWeiXinAccessToken(String[] wxInfo) throws Exception {
String access_token = MemcachedUtil.get("ParentsSpace_access_token");
if(StringUtils.isNotBlank(access_token)){
return access_token;
}
/* if( CacheUtils.get("access_token_addTime")!=null && (System.currentTimeMillis() - (Long)CacheUtils.get("access_token_addTime")) <7080*1000 ){//不超过2个小时的缓存有效
return (String) CacheUtils.get("access_token");
}*/else{//失效了 就重新请求
String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + wxInfo[0] + "&secret=" + wxInfo[1];
String result = this.httpReqExecute(url);
this.logger.warn("from weixin api accessToken: " + result);
try {
if(StringUtils.isNotEmpty(result)) {
// 解析respContent,并获取其中的更新的key,
Gson gson =new Gson();
Map resultMap = gson.fromJson(result,Map.class);
// String accessToken = JSONObject.fromObject(result).optString("access_token", "");
String accessToken = "";
if(resultMap.get("access_token")!=null){
accessToken = resultMap.get("access_token");
//缓存起来
/* CacheUtils.put("access_token_addTime",System.currentTimeMillis());
CacheUtils.put("access_token",accessToken);*/
//缓存118分钟
MemcachedUtil.set("ParentsSpace_access_token",accessToken,118*60);
}
return accessToken;
}
} catch (Exception e) {
logger.error("getAccessToken error in WeiXinShareAction", e);
}
return null;
}
}
/**
* 数据签名
* @param nocestr
* @param ticket
* @param timestamp
* @param url
* @return
*/
private String createSignature(String nocestr, String ticket, int timestamp, String url) {
// 这里参数的顺序要按照 key 值 ASCII 码升序排序
String s = "jsapi_ticket=" + ticket + "&noncestr=" + nocestr + "×tamp=" + timestamp + "&url=" + url;
return DigestUtils.sha1Hex(s);
}
/**
* 创建随机串 自定义个数0 < ? < 32
* @return
*/
private String createNonceStr() {
String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
String nonceStr = "";
for (int i = 0; i < 16; i++) {
int beginIndex = (int) Math.round(Math.random() * 10);
nonceStr += str.substring(beginIndex, beginIndex + 1);
}
return nonceStr;
}
/**
* 创建时间戳
* @return
*/
private int createTimestamp() {
return Calendar.getInstance().get(Calendar.SECOND);
}
// 输出信息
/*private void responseWrite(String content) {
try {
getResponse().setCharacterEncoding("utf-8");
getResponse().getWriter().write(content);
} catch (Exception e) {
logger.error("responseWrite error in WeiXinShareAction", e);
}
}*/
// HTTP远程调用
private String httpReqExecute(String url) {
String result = "";
// DefaultHttpClient httpclient = null ;
HttpClient httpclient = null ;
try {
httpclient = HttpClientBuilder.create().build();//获取DefaultHttpClient请求
HttpPost httppost = new HttpPost(url);
// 执行
org.apache.http.HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
if(entity != null && response.getStatusLine().getStatusCode() == 200){
result = EntityUtils.toString(entity, "UTF-8");
}
} catch (Exception e) {
logger.error(" WeiXinShareAction 调用微信 API 失败!", e);
} finally { // 关闭连接,释放资源
httpclient.getConnectionManager().shutdown();
}
return result;
}
}