access_token是何物,access_token是公眾號的全局唯一接口調用憑據,公眾號調用各接口時都需使用access_token。開發者需要進行妥善保存。access_token的存儲至少要保留512個字符空間。access_token的有效期目前為2個小時,需定時刷新,重復獲取將導致上次獲取的access_token失效。
官方是這么介紹的,由於有請求次數限制,肯定不能隨用隨取,需要采用中控服務器保存。
此外在刷新過程中,中控服務器可對外繼續輸出的老access_token,此時公眾平台后台會保證在5分鍾內,新老access_token都可用,這保證了第三方業務的平滑過渡,這就保證了不需要考慮access_token失效的問題了,前提是正確獲取到並及時刷新了。
一、安裝Redis
Linux可以用docker去安裝,Windows下安裝並設置Redis
二、獲取access_token並緩存到Redis
AppID和AppSecret可在“微信公眾平台-開發-基本配置”頁中獲得(需要已經成為開發者,且帳號沒有異常狀態)。
如果是測試號,不存在白名單一說,正式的公眾號調用接口時,請登錄“微信公眾平台-開發-基本配置”提前將服務器IP地址添加到IP白名單中,點擊查看設置方法,否則將無法調用成功
聲明: 由於沒開發過實際的公眾號,不清楚如何正確的緩存access_token,只是我個人的做法。
多環境配置,在application-dev.yml加入 ,appid 、appsecret、token替換成自己的
#wechat
wechat:
appid: yours
appsecret: yours
token: yours
讀取配置
/** * FileName: WechatAccountConfig * Author: Phil * Date: 11/20/2018 3:10 PM * Description: 配置文件 * History: * <author> <time> <version> <desc> * 作者姓名 修改時間 版本號 描述 */ package com.phil.modules.config; import lombok.Getter; import lombok.Setter; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; /** * 〈一句話功能簡述〉<br> * 〈接入配置信息〉 * * @author Phil * @create 11/20/2018 3:10 PM * @since 1.0 */ @Component @Getter @Setter @ConfigurationProperties(prefix = "wechat") public class WechatAccountConfig { /*** * 微信的appid */ private String appid; /*** * 微信的secret */ private String appsecret; /*** * 微信授權url */ private String returnUrl; /*** * 微信的驗證token */ private String token; }
application.properties里加上配置
#auth
#獲取憑證的鏈接
wechat.auth.get-access-token-url=https://api.weixin.qq.com/cgi-bin/token
讀取配置
/** * FileName: WechatAuthConfig * Author: Phil * Date: 11/21/2018 7:10 PM * Description: * History: * <author> <time> <version> <desc> * 作者姓名 修改時間 版本號 描述 */ package com.phil.wechat.auth.config; import lombok.Getter; import lombok.Setter; import lombok.ToString; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; /** * 〈一句話功能簡述〉<br> * 〈〉 * * @author Phil * @create 11/21/2018 7:10 PM * @since 1.0 */ @Component @Getter @Setter @ToString @ConfigurationProperties(prefix = "wechat.auth") public class WechatAuthConfig { //獲取憑證 private String getAccessTokenUrl; }
定義接收的實體Bean
/**
* FileName: AccessToken
* Author: Phil
* Date: 12/4/2018 2:13 PM
* Description:
* History:
* <author> <time> <version> <desc>
* 作者姓名 修改時間 版本號 描述
*/
package com.phil.wechat.auth.model.response;
import com.google.gson.annotations.SerializedName; import lombok.Getter; import lombok.Setter; import java.io.Serializable; /** * 〈一句話功能簡述〉<br> * 〈微信通用接口憑證〉 * * @author Phil * @create 12/4/2018 2:13 PM * @since 1.0 */ @Getter @Setter public class AccessToken implements Serializable { private static final long serialVersionUID = 5806078615354556552L; // 獲取到的憑證 @SerializedName("access_token") private String accessToken; // 憑證有效時間,單位:秒 private int expires_in; }
讀取並緩存到Redis
/** * FileName: AuthServiceImpl * Author: Phil * Date: 11/21/2018 12:13 PM * Description: * History: * <author> <time> <version> <desc> * 作者姓名 修改時間 版本號 描述 */ package com.phil.wechat.auth.service.impl; import com.google.gson.JsonSyntaxException; import com.phil.modules.config.WechatAccountConfig; import com.phil.modules.result.ResultState; import com.phil.modules.util.HttpUtil; import com.phil.modules.util.JsonUtil; import com.phil.modules.util.RedisUtils; import com.phil.wechat.auth.config.WechatAuthConfig; import com.phil.wechat.auth.model.BasicAuthParam; import com.phil.wechat.auth.model.response.AccessToken; import com.phil.wechat.auth.model.response.AuthAccessToken; import com.phil.wechat.auth.model.response.AuthUserInfo; import com.phil.wechat.auth.service.WechatAuthService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.util.HashMap; import java.util.Map; import java.util.Objects; import java.util.TreeMap; /** * 〈一句話功能簡述〉<br> * 〈〉 * * @author Phil * @create 11/21/2018 12:13 PM * @since 1.0 */ @Service @Slf4j public class WechatAuthServiceImpl implements WechatAuthService { @Value("${wechat.token}") private String token; @Resource RedisUtils redisUtils; @Resource WechatAuthConfig wechatAuthConfig; @Resource private WechatAccountConfig wechatAccountConfig; /** * 獲取授權憑證token * * @return 授權憑證token */ @Override public String getAccessToken() { String accessToken = null; if (Objects.isNull(redisUtils.get(token))) { Map<String, String> map = new TreeMap<>(); map.put("appid", wechatAccountConfig.getAppid()); map.put("secret", wechatAccountConfig.getAppsecret()); map.put("grant_type", "client_credential"); String json = HttpUtil.doGet(wechatAuthConfig.getGetAccessTokenUrl(), map); AccessToken bean = JsonUtil.fromJson(json, AccessToken.class); if (bean != null) { accessToken = bean.getAccessToken(); log.info("從微信服務器獲取的授權憑證{}", accessToken); redisUtils.set(token, accessToken, 60 * 120); log.info("從微信服務器獲取的token緩存到Redis"); } } else { accessToken = redisUtils.get(token).toString(); log.info("從redis中獲取的授權憑證{}", accessToken); } return accessToken; } }
又或者可以使用定時去緩存刷新
完整代碼查看GitHub鏈接,不定時更新