1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
| import java.io.UnsupportedEncodingException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Formatter; import java.util.HashMap; import java.util.Map; import java.util.UUID;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.listenlives.dao.WXTokenDao; import com.listenlives.domain.WXToken; import com.listenlives.information.util.HttpHelper;
import net.sf.json.JSONObject;
@SuppressWarnings("static-access") @Service public class WXTokenServiceImpl {
String nonce_str = create_nonce_str(); String timestamp = create_timestamp(); String signature = "";
String jsapiTicket = ""; String assceeToken = "";
try { WXToken wxToken = wxTokenDao.selectWXToken();
assceeToken = wxToken.getAccessToken();
if (compareWithExpiresIn(wxToken.getCreateTime())) {
assceeToken = getNewAccessToken();
}
jsapiTicket = getNewJSAPITicket(assceeToken, 0).get("ticket").toString();
signature = "jsapi_ticket=" + jsapiTicket + "&noncestr=" + nonce_str + "×tamp=" + timestamp + "&url=" + url;
MessageDigest crypt = MessageDigest.getInstance("SHA-1"); crypt.reset(); crypt.update(signature.getBytes("UTF-8"));
signature = byteToHex(crypt.digest());
} catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); }
resultMap.put("url", url); resultMap.put("nonceStr", nonce_str); resultMap.put("timestamp", timestamp); resultMap.put("signature", signature);
return resultMap; }
public boolean compareWithExpiresIn(String createTime) {
boolean flag = false;
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date nowDate; Date tokenDate; long diff;
try {
nowDate = df.parse(df.format(new Date())); tokenDate = df.parse(createTime); diff = nowDate.getTime() - tokenDate.getTime();
flag = diff > 7200000;
} catch (ParseException e) { e.printStackTrace(); }
return flag; }
private static String byteToHex(final byte[] hash) { Formatter formatter = new Formatter(); for (byte b : hash) { formatter.format("%02x", b); } String result = formatter.toString(); formatter.close(); return result; }
private String create_nonce_str() { return UUID.randomUUID().toString(); }
private String create_timestamp() { return Long.toString(System.currentTimeMillis() / 1000); }
public String getNewAccessToken() { String url = "https://api.weixin.qq.com/cgi-bin/token"; String data = ""; String result = ""; try { Map<String, String> accessMap = new HashMap<String, String>(); accessMap.put("grant_type", "client_credential"); accessMap.put("appid", ${appid}); accessMap.put("secret", ${secret}); data = HttpHelper.post(accessMap, url); if (data != null && data != "") { JSONObject json = new JSONObject().fromObject(data); String access_token = (String) json.get("access_token"); if (null != access_token && !"".equals(access_token)) {
WXToken wxToken = new WXToken(); wxToken.setAccessToken(access_token); wxTokenDao.updateWXToken(wxToken);
result = access_token; } } } catch (Exception e) { e.printStackTrace(); } return result; }
public Map<String, Object> getNewJSAPITicket(String accessToken, int flag) {
if (flag > 1) { return null; } String url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket"; String data = ""; Map<String, Object> resultMap = new HashMap<String, Object>(); try {
Map<String, String> accessMap = new HashMap<String, String>(); accessMap.put("access_token", accessToken); accessMap.put("type", "jsapi"); data = HttpHelper.post(accessMap, url); if (data != null && data != "") { JSONObject json = new JSONObject().fromObject(data); String ticket = (String) json.get("ticket"); if (null != ticket && !"".equals(ticket)) { resultMap.put("ticket", ticket); } else { resultMap = getNewJSAPITicket(getNewAccessToken(), flag + 1); } } } catch (Exception e) { e.printStackTrace(); } return resultMap; }
}
|