获取
token
、
获取api_ticket
,这个是用在签名里面的,直接调用就行;(这两个方法都写了缓存,和是失效时间,并且在用的时候都会去判断缓存里面有没有值,没有的话,我们再去请求重新获取,而不是每次都请求获取,这样会造成接口请求频繁受到限制的问题)
- @Component
- public class SignTicketUtils {
-
- @Autowired
- private WeiXinConfig weiXinConfig;
-
- @Autowired
- private RedisCache redisService;
-
- //公众号请求的地址
- public static String WX_GZH_API = "https://api.weixin.qq.com/cgi-bin/";
-
-
- /**
- * 第一步:获取access_token的值
- * @return
- */
- public String getToken() {
- String token = "";
- String path = "token?grant_type=client_credential&appid=" + weiXinConfig.getAppid() + "&secret=" + weiXinConfig.getSecret();
- String body = HttpUtils.sendGet(WX_GZH_API + path);
- JSONObject object = JSON.parseObject(body);
- //获取token
- token = object.getString("access_token");
- //将token值的值放在redis里面
- redisService.setCacheObject("gzh_access_token", token,7190, TimeUnit.SECONDS);
- return token;
- }
-
- /**
- * 第二步:获取jsapi_ticket
- * @return
- */
- public String getJsapiTicket() {
- //获取redis里面的token
- Object access_token = redisService.getCacheObject("gzh_access_token");
- if (access_token==null) {
- access_token = getToken();
- }
- String path = "ticket/getticket?access_token=" + access_token.toString() + "&type=jsapi";
- String body = HttpUtils.sendGet(WX_GZH_API + path);
- JSONObject object = JSON.parseObject(body);
- //获取ticket
- String ticket = object.getString("ticket");
- //错误码
- Integer errcode = object.getInteger("errcode");
- if(errcode==0){
- //将ticket值的值放在redis里面
- redisService.setCacheObject("gzh_ticket", ticket,7190,TimeUnit.SECONDS);
- }
- return ticket;
- }
-
- /**
- * 公众号网页开发
- * @param jsapi_ticket
- * @param url
- * @return
- */
- public static Map
sign(String jsapi_ticket, String url) { - Map
ret = new HashMap(); - String nonce_str = create_nonce_str();
- String timestamp = create_timestamp();
- String string1;
- String signature = "";
-
- //注意这里参数名必须全部小写,且必须有序
- string1 = "jsapi_ticket=" + jsapi_ticket +
- "&noncestr=" + nonce_str +
- "×tamp=" + timestamp +
- "&url=" + url;
- System.out.println(string1);
-
- try
- {
- MessageDigest crypt = MessageDigest.getInstance("SHA-1");
- crypt.reset();
- crypt.update(string1.getBytes("UTF-8"));
- signature = byteToHex(crypt.digest());
- }
- catch (NoSuchAlgorithmException | UnsupportedEncodingException e)
- {
- e.printStackTrace();
- }
-
- ret.put("url", url);
- ret.put("jsapi_ticket", jsapi_ticket);
- ret.put("nonceStr", nonce_str);
- ret.put("timestamp", timestamp);
- ret.put("signature", signature);
-
- return ret;
- }
-
- 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 static String create_nonce_str() {
- return UUID.randomUUID().toString();
- }
-
- private static String create_timestamp() {
- return Long.toString(System.currentTimeMillis() / 1000);
- }
-
- }
获取签名
- @Api("微信分享")
- @RestController
- @RequestMapping("/自己的路径")
- public class SignTicketController {
-
- @Autowired
- private RedisCache redisService;
-
- @Autowired
- private WeiXinConfig weiXinConfig;
-
- @Autowired
- private SignTicketUtils signTicketUtils;
-
-
- /**
- * 开始签名
- */
- @GetMapping("/getSign")
- public R getSign(String url){
- //从redis里面获取ticket
- Object ticket = redisService.getCacheObject("gzh_ticket");
- if(ticket==null){
- ticket = signTicketUtils.getJsapiTicket();
- }
- Map
ret = signTicketUtils.sign(ticket.toString(), url); - JSONObject objectData = new JSONObject();
- for (Map.Entry entry : ret.entrySet()) {
- objectData.put(entry.getKey().toString(),entry.getValue());
- }
- objectData.put("appId", weiXinConfig.getAppid());
- return R.ok(objectData);
- }
-
- }
返回结果集类(可用自己的,附上仅为了复制代码直接可用)
- public class R
implements Serializable - {
- private static final long serialVersionUID = 1L;
-
- /** 成功 */
- public static final int SUCCESS = HttpStatus.SUCCESS;
-
- /** 失败 */
- public static final int FAIL = HttpStatus.ERROR;
-
- private int code;
-
- private String msg;
-
- private T data;
-
- public static
R ok() - {
- return restResult(null, SUCCESS, "操作成功");
- }
-
- public static
R ok(T data) - {
- return restResult(data, SUCCESS, "操作成功");
- }
-
- public static
R ok(T data, String msg) - {
- return restResult(data, SUCCESS, msg);
- }
-
- public static
R fail() - {
- return restResult(null, FAIL, "操作失败");
- }
-
- public static
R fail(String msg) - {
- return restResult(null, FAIL, msg);
- }
-
- public static
R fail(T data) - {
- return restResult(data, FAIL, "操作失败");
- }
-
- public static
R fail(T data, String msg) - {
- return restResult(data, FAIL, msg);
- }
-
- public static
R fail(int code, String msg) - {
- return restResult(null, code, msg);
- }
-
- private static
R restResult(T data, int code, String msg) - {
- R
apiResult = new R<>(); - apiResult.setCode(code);
- apiResult.setData(data);
- apiResult.setMsg(msg);
- return apiResult;
- }
-
- public int getCode()
- {
- return code;
- }
-
- public void setCode(int code)
- {
- this.code = code;
- }
-
- public String getMsg()
- {
- return msg;
- }
-
- public void setMsg(String msg)
- {
- this.msg = msg;
- }
-
- public T getData()
- {
- return data;
- }
-
- public void setData(T data)
- {
- this.data = data;
- }
-
- public static
Boolean isError(R ret) - {
- return !isSuccess(ret);
- }
-
- public static
Boolean isSuccess(R ret) - {
- return R.SUCCESS == ret.getCode();
- }
- }