Spring Boot中微信全局token的缓存实现
为什么要缓存token?
这里的token指的是微信JSAPI中基础支持的ACCESS_TOKEN,并非网页授权ACCESS_TOKEN。网页授权Token每天的调用次数没有限制,不需要缓存。
| 接口 | 每日限额 |
|---|---|
| 获取access_token | 2000 |
| 自定义菜单创建 | 1000 |
| 自定义菜单查询 | 10000 |
| 获取用户基本信息 | 5000000 |
| 获取网页授权access_token | 无 |
| 刷新网页授权access_token | 无 |
| 网页授权获取用户信息 | 无 |
从上面的表格我们可以看到,微信基础支持的token每天调用频次为2000次。而token的有效时间为7200s,当实现了token的全局缓存后,理论每天只需要调用12次。相反,2000次即使仅供微信分享回调,在有一定用户基础的项目中完全满足不了。
缓存方案
- 项目启动时开启一个定时器,每7180s执行一次Http请求,从微信获取最新的access_token并将redis中旧的access_token替换掉。
- 代码中有需要使用access_token时,直接从缓存中读取。
Spring Boot使用定时器
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.sql.SQLException;
import javax.annotation.Resource;
import org.apache.log4j.Logger;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import com.bjb.dao.impl.RedisTokenHelper;
import net.sf.json.JSONObject;
/**
* 全局定时器
* @author qiqj
*
*/
@Component
public class Scheduler {
private final Logger logger = Logger.getRootLogger();
@Resource
private RedisTokenHelper redisTokenHelper;
/**
* 定时获取access_token
* @throws SQLException
*/
@Scheduled(fixedDelay=7180000)
public void getAccessToken() throws SQLException{
logger.info("==============开始获取access_token===============");
String access_token = null;
String grant_type = "client_credential";
String AppId= WxPropertiseUtil.getProperty("appid");
String secret= WxPropertiseUtil.getProperty("secret");
String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type="+grant_type+"&appid="+AppId+"&secret="+secret;
try {
URL urlGet = new URL(url);
HttpURLConnection http = (HttpURLConnection) urlGet.openConnection();
http.setRequestMethod("GET"); // 必须是get方式请求
http.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
http.setDoOutput(true);
http.setDoInput(true);
http.connect();
InputStream is = http.getInputStream();
int size = is.available();
byte[] jsonBytes = new byte[size];
is.read(jsonBytes);
String message = new String(jsonBytes, "UTF-8");
JSONObject demoJson = JSONObject.fromObject(message);
//System.out.println("JSON字符串:"+demoJson);
access_token = demoJson.getString("access_token");
is.close();
logger.info("==============结束获取access_token===============");
} catch (Exception e) {
e.printStackTrace();
}
logger.info("==============开始写入access_token===============");
redisTokenHelper.saveObject("global_token", access_token);
logger.info("==============写入access_token成功===============");
}
}
读取配置文件工具类
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import org.apache.log4j.Logger;
/**
* 读取微信配置文件 wxconfig.properties工具类
* @author qiqj
*
*/
public class WxPropertiseUtil {
private static Properties properties = new Properties();
protected static final Logger logger = Logger.getRootLogger();
static{
InputStream in = WxPropertiseUtil.class.getClassLoader().getResourceAsStream("wxconfig.properties");
try {
properties.load(in);
} catch (IOException e) {
e.printStackTrace();
logger.error(e.getMessage());
}
}
public static String getProperty(String key){
return properties.getProperty(key);
}
public static void UpdateProperty(String key,String value){
properties.setProperty(key, value);
}
}
微信配置文件
appid=xxxx
secret=xxxxxx
Redis操作工具类
import java.util.concurrent.TimeUnit;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Repository;
/**
* 封装Redis存取Token对的工具类
* @author qiqj
*
*/
@Repository
public class RedisTokenHelper {
@Autowired
StringRedisTemplate stringRedisTemplate;
@Autowired
RedisTemplate<Object, Object> redisTemplate;
@Resource(name="stringRedisTemplate")
ValueOperations<String, String> ops;
@Resource(name="redisTemplate")
ValueOperations<Object, Object> objOps;
/**
* 键值对存储 字符串 :有效时间3分钟
* @param tokenType Token的key
* @param Token Token的值
*/
public void save(String tokenType,String Token){
ops.set(tokenType, Token, 180, TimeUnit.SECONDS);
}
/**
* 根据key从redis获取value
* @param tokenType
* @return String
*/
public String getToken(String tokenType){
return ops.get(tokenType);
}
/**
* redis 存储一个对象
* @param key
* @param obj
* @param timeout 过期时间 单位:s
*/
public void saveObject(String key,Object obj,long timeout){
objOps.set(key, obj,timeout,TimeUnit.SECONDS);
}
/**
* redis 存储一个对象 ,不过期
* @param key
* @param obj
*/
public void saveObject(String key,Object obj){
objOps.set(key, obj);
}
/**
* 从redis取出一个对象
* @param key
* @param obj
*/
public Object getObject(String key){
return objOps.get(key);
}
/**
* 根据Key删除Object
* @param key
*/
public void removeObject(String key){
redisTemplate.delete(key);
}
}
简单测试
import org.apache.log4j.Logger;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.transaction.annotation.Transactional;
import com.bjb.Application;
import com.bjb.dao.impl.RedisTokenHelper;
/**
* Junit单元测试类
* @author qiqj
*
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes=Application.class)
@WebAppConfiguration
@Transactional
public class JUnitTest {
private final Logger logger = Logger.getRootLogger();
@Resource
private RedisTokenHelper redisTokenHelper;
@Test
public void test(){
String access_token = (String) redisTokenHelper.getObject("global_token");
System.out.println("access_token:"+access_token);
}
}
Spring Boot中微信全局token的缓存实现的更多相关文章
- Spring Boot 中集成 Redis 作为数据缓存
只添加注解:@Cacheable,不配置key时,redis 中默认存的 key 是:users::SimpleKey [](1.redis-cli 中,通过命令:keys * 查看:2.key:缓存 ...
- Spring Boot2 系列教程(十三)Spring Boot 中的全局异常处理
在 Spring Boot 项目中 ,异常统一处理,可以使用 Spring 中 @ControllerAdvice 来统一处理,也可以自己来定义异常处理方案.Spring Boot 中,对异常的处理有 ...
- 在Spring Boot中添加全局异常捕捉提示
在一个项目中的异常我们我们都会统一进行处理的,那么如何进行统一进行处理呢? 全局异常捕捉: 新建一个类GlobalDefaultExceptionHandler, 在class注解上@Controll ...
- Spring Boot中使用缓存
Spring Boot中使用缓存 随着时间的积累,应用的使用用户不断增加,数据规模也越来越大,往往数据库查询操作会成为影响用户使用体验的瓶颈,此时使用缓存往往是解决这一问题非常好的手段之一. 原始的使 ...
- Spring Boot中的缓存支持(一)注解配置与EhCache使用
Spring Boot中的缓存支持(一)注解配置与EhCache使用 随着时间的积累,应用的使用用户不断增加,数据规模也越来越大,往往数据库查询操作会成为影响用户使用体验的瓶颈,此时使用缓存往往是解决 ...
- spring boot中的声明式事务管理及编程式事务管理
这几天在做一个功能,具体的情况是这样的: 项目中原有的几个功能模块中有数据上报的功能,现在需要在这几个功能模块的上报之后生成一条消息记录,然后入库,在写个接口供前台来拉取消息记录. 看到这个需求,首先 ...
- Spring Boot中的微信支付(小程序)
前言 微信支付是企业级项目中经常使用到的功能,作为后端开发人员,完整地掌握该技术是十分有必要的. logo 一.申请流程和步骤 图1-1 注册微信支付账号 获取微信小程序APPID 获取微信商家的商户 ...
- 在Spring Boot中使用数据缓存
春节就要到了,在回家之前要赶快把今年欠下的技术债还清.so,今天继续.Spring Boot前面已经预热了n篇博客了,今天我们来继续看如何在Spring Boot中解决数据缓存问题.本篇博客是以初识在 ...
- Spring Boot中使用EhCache实现缓存支持
SpringBoot提供数据缓存功能的支持,提供了一系列的自动化配置,使我们可以非常方便的使用缓存.,相信非常多人已经用过cache了.因为数据库的IO瓶颈.一般情况下我们都会引入非常多的缓存策略, ...
随机推荐
- [转载]迅为4418开发板Qt移植移动4G模块第一部分
本文转自迅为论坛:http://topeetboard.com 平台:iTOP-4418开发板 1.首先要配置内核,这个一步和Android系统移植3G或者4G模块是一样的.一般模块的 ...
- CAD交互绘制带周长面积的矩形框(com接口)
主要用到函数说明: _DMxDrawX::DrawLine 绘制一个直线.详细说明如下: 参数 说明 DOUBLE dX1 直线的开始点x坐标 DOUBLE dY1 直线的开始点y坐标 DOUBLE ...
- ionic提供的配色方案
.light #ffffff .stable #f8f8f8 .positive #387ef5 .calm #11c1f3 .balanced #33cd5f .energized #ffc900 ...
- better-scroll的使用
<template> <div> <div> <h2 class="h2">{{msg}}</h2> </div& ...
- 笔试算法题(38):并查集(Union-Find Sets)
议题:并查集(Union-Find Sets) 分析: 一种树型数据结构,用于处理不相交集合(Disjoint Sets)的合并以及查询:一开始让所有元素独立成树,也就是只有根节点的树:然后根据需要将 ...
- <Redis> 入门X 分布式锁
分布式其实就是多进程的程序,当多个进程访问一个资源,会造成问题: 1.资源共享的竞争问题 2.数据的安全性 分布式锁的解决方案: 1.怎么去获取锁 数据库 zookeeper redis 2.怎么释放 ...
- c++基础_字母图形
#include <iostream> #include <algorithm> using namespace std; int main(){ ,m=,c; cin> ...
- LeetCode(6) ZigZag Conversion
题目 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows li ...
- ExecutorService 线程池 (转发)
1.ExecutorService java.util.concurrent.ExecutorService 接口.用来设置线程池并执行多线程任务.它有以下几个方法. Future<?> ...
- BUAA_OO_博客作业四
BUAA_OO_博客作业四 1 第四单元两次作业的架构设计 1.1 第13次作业 类图 作业要求:通过实现UmlInteraction这个官方提供的接口,来实现自己的UmlInteraction解 ...