如何保证access_token长期有效--微信公众平台开发

http://blog.csdn.net/qq_33556185/article/details/52758781

 

import javax.servlet.ServletContext;

import org.apache.log4j.Logger;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;

import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;


public class TokenUtil {

    private static final Logger logger = Logger.getLogger(MessageService.class);

    public static String getAccessToken(String appId, String appSecret,String flag) {
        WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext();
        ServletContext application = webApplicationContext.getServletContext();

        if (application.getAttribute(flag) != null) {

            WeixinAccessToken tempToken = (WeixinAccessToken) application.getAttribute(flag);

            if (System.currentTimeMillis() < tempToken.getExpirationTime()) {
                return tempToken.getAccessToken();
            } else {
                return getAccessTokenContent(appId, appSecret, application,flag);
            }
        }
        return getAccessTokenContent(appId, appSecret, application,flag);

    }

    private static String getAccessTokenContent(String appId, String appSecret, ServletContext application,String flag) {
        WeixinAccessToken accessTokenFromUrl = getAccessTokenFromUrl(appId, appSecret);
        String accessToken = null;
        if (accessTokenFromUrl != null) {
            application.setAttribute(flag, accessTokenFromUrl);
            accessToken = accessTokenFromUrl.getAccessToken();
            logger.info("accessToken from url");
        }
        return accessToken;
    }

    public static WeixinAccessToken getAccessTokenFromUrl(String appId, String appSecret) {
        String requestUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=SECRET";

        requestUrl = requestUrl.replace("APPID", appId);
        requestUrl = requestUrl.replace("SECRET", appSecret);

        JSONObject jsonObject = CommonUtil.httpsRequest(requestUrl, "GET", null);
        WeixinAccessToken tokenModel = null;
        if (null != jsonObject) {
            try {
                String access_token = jsonObject.getString("access_token");
                int expires_in = jsonObject.getIntValue("expires_in");
                tokenModel = new WeixinAccessToken(access_token, System.currentTimeMillis() + expires_in*1000);
            } catch (JSONException e) {
                logger.info("获取token失败 errcode:" + jsonObject.getIntValue("errcode") + ",errmsg:"
                        + jsonObject.getString("errmsg"));
            }
        }
        return tokenModel;
    }
}

WeixinAccessToken

public class WeixinAccessToken {  
    private String accessToken;  
    private long expirationTime;  
      
    public WeixinAccessToken(){  
          
    }  
  
   public WeixinAccessToken(String accessToken,long expirationTime){  
      this.accessToken=accessToken;  
      this.expirationTime=expirationTime;  
    }  
  
      public String getAccessToken() {  
        return accessToken;  
    }  
    public void setAccessToken(String accessToken) {  
        this.accessToken = accessToken;  
    }  
    public long getExpirationTime() {  
        return expirationTime;  
    }  
    public void setExpirationTime(long expirationTime) {  
        this.expirationTime = expirationTime;  
    }  
      
  
}  
View Code

 

转载于:https://www.cnblogs.com/newlangwen/p/7660883.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值