Springboot微信開発ウィジェットのaccess_tokenの取得とストレージ

5502 ワード

サービス側API:https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/access-token/auth.getAccessToken.htmlウィジェットのグローバル唯一のバックグラウンドインタフェース呼び出し認証情報(access_token)を取得します.ほとんどのバックグラウンドインタフェースを呼び出す場合はaccess_tokenを使用し、開発者は適切に保存する必要があります.
access_tokenのストレージと更新access_tokenの記憶は少なくとも512文字の空間を保持しなければならない.access_tokenの有効期間は現在2時間で、タイミングリフレッシュが必要で、繰り返し取得すると前回取得したaccess_token失効;開発者は、中央制御サーバを使用してaccessを統一的に取得およびリフレッシュすることを推奨します.token、他のビジネスロジックサーバで使用されるaccess_tokenはいずれもこの中制御サーバから来ており、それぞれリフレッシュすべきではありません.そうしないと衝突しやすくなり、access_tokenがカバーして業務に影響する.access_tokenの有効期間は、返されたexpire_を通過します.inは、現在7200秒以内の値であり、中制御サーバはこの有効時間に基づいて事前にリフレッシュする必要があることを伝えます.リフレッシュ中に、中国制御サーバが外部に出力し続ける古いaccess_token、この時公衆プラットフォームのバックグラウンドは5分以内に保証して、新旧access_tokenは、サードパーティのビジネスのスムーズな移行を保証するために使用できます.access_tokenの有効時間は将来調整される可能性があるため、中制御サーバは内部タイミングでアクティブにリフレッシュするだけでなく、パッシブリフレッシュaccessを提供する必要があります.tokenのインタフェースは、ビジネスサーバがAPIでaccessを呼び出すのを容易にする.tokenがタイムアウトした場合、access_をトリガーできます.tokenのリフレッシュプロセス.
パラメータ設定パラメータ
package com.austin.pay.common.base;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * @author liuqh
 * @date 2019/6/10   2:35
 * @description     
 */
@Component
@ConfigurationProperties(prefix = "parameter")
@Data
public class Parameter {

    /**
     *    appid
     */
    private String APPID;
    /**
     *    appSecrect
     */
    private String APP_SECRECT;
}

application.yml入力パラメータ値
parameter:
  a-p-p-i-d:    appid
  a-p-p-s-e-c-r-e-c-t:    appSecrect

AccessTokenエンティティ
package com.austin.pay.common.weixin.token;

import lombok.Data;

/**
 * @author liuqh
 * @date 2019/8/6   1:51
 * @description
 */
@Data
public class AccessToken {

    private String access_token;

    private Long expires_in;
}

GetTokenは要求を送信し、得られるaccess_tokenとexpires_inエンティティークラスに入れる
package com.austin.pay.common.weixin.token;

import com.austin.pay.common.weixin.utils.CommonUtil;
import com.austin.pay.common.weixin.utils.WeixinUrl;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * @author liuqh
 * @date 2019/8/6   1:49
 * @description
 */
@Component
public class GetToken {

    private Logger logger = LoggerFactory.getLogger(GetToken.class);

    public AccessToken getToken(String appid, String appSecrect) {

        AccessToken token = null;

        String url = WeixinUrl.ACCESS_TOKEN + "?grant_type=client_credential&appid=" + appid
                + "&secret=" + appSecrect;
        String result = CommonUtil.httpsRequest(url, "GET", null);
        JSONObject jsonObject = JSONObject.fromObject(result);

        if (jsonObject != null) {

            try {
                token = new AccessToken();

                token.setAccess_token(jsonObject.getString("access_token"));

                token.setExpires_in(jsonObject.getLong("expires_in"));
            } catch (Exception e) {
                token = null;
                e.printStackTrace();
                logger.error("     !");
            }
        } else {
            token = null;
            //   token  
            logger.error("jsonObject  ,  token  ");
        }
        return token;

    }
}

メモリフィールドtokenの設定
package com.austin.pay.common.weixin.token;

/**
 * @author liuqh
 * @date 2019/8/6   2:30
 * @description
 */
public class WeixinToken {

    public static String token;
}

タイマーAccessTokenTaskをオンにするとaccess_に入りますtokenメモリ、および7000秒ごとに呼び出される1秒遅延実行を設定
package com.austin.pay.common.weixin.token;

import com.austin.pay.common.base.Parameter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * @author liuqh
 * @date 2019/8/6   1:47
 * @description
 */
@Component
public class AccessTokenTask {

    private Logger logger = LoggerFactory.getLogger(AccessTokenTask.class);

    @Autowired
    private GetToken getToken;
    @Autowired
    private Parameter parameter;

    /**
     * access_token              
     * access_token       2    ,       access_token,              
     * access_token   
     *       
     */
    @Scheduled(initialDelay = 1000, fixedDelay = 7000*1000 )
    public void getTouTiaoAccessToken(){
        try {
            String token = getToken.getToken(parameter.getAPPID(), parameter.getAPP_SECRECT()).getAccess_token();
            //     token    
            WeixinToken.token = token;
            logger.info("      accessToken "+token);
        } catch (Exception e) {
            logger.error("    adcessToken  ,    ");
            e.printStackTrace();
        }

    }
}


メインプログラムにオープンタイマーの注記@EnableSchedulingを追加
package com.austin.pay;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling
public class AustinPayApplication {

    public static void main(String[] args) {
        SpringApplication.run(AustinPayApplication.class, args);
    }
}