通过微信授权拿到的token有一个7200秒的有效期,并且获取token的次数也有一定限制,所以token不能“随用随取”,通过网络资源获取,解决方案有①储存在数据库中,每次使用时都查询一次数据库,②定义静态全局变量(单例模式),开启线程监控变量是否“过期”,③结合数据库做缓存,开启定时任务,每7200秒从微信服务器中获取一次token并保存到自己的数据库中
各方法的优缺:
①:简单粗暴,对数据库的操作过多,适合访问量小,调用微信API次数少的项目
②:(单例模式)全局变量在服务器重启时会清除,不使用数据库的方式,变量设置不当可能会混淆(微信有两个token,一个用于(粉丝)用户授权,一个用于开发者授权)
③:灵活,易结合到各类开发中,spring也对各种缓存方案有很好的支持
以下为方法③的实现方式之一(未开启定期任务,缓存方案使用ehcache)
基本配置
ehcache.xml配置
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">
<defaultCache
eternal="false"
maxElementsInMemory="1000"
overflowToDisk="false"
diskPersistent="false"
timeToIdleSeconds="0"
timeToLiveSeconds="600"
memoryStoreEvictionPolicy="LRU" />
<cache
name="TokenCache"
eternal="false"
maxElementsInMemory="100"
overflowToDisk="false"
diskPersistent="false"
timeToIdleSeconds="7200"
timeToLiveSeconds="7200"
memoryStoreEvictionPolicy="LRU" />
<!-- eternal:true表示对象永不过期,此时会忽略timeToIdleSeconds和timeToLiveSeconds属性,默认为false -->
<!-- maxEntriesLocalHeap:堆内存中最大缓存对象数,0没有限制 -->
<!-- timeToIdleSeconds: 设定允许对象处于空闲状态的最长时间,以秒为单位。当对象自从最近一次被访问后,
如果处于空闲状态的时间超过了timeToIdleSeconds属性值,这个对象就会过期,EHCache将把它从缓存中清空。
只有当eternal属性为false,该属性才有效。如果该属性值为0,则表示对象可以无限期地处于空闲状态 -->
</ehcache>
spring boot配置
#端口
server.port=8080
#数据源
spring.datasource.name=test
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=true&zeroDateTimeBehavior=convertToNull
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
##连接池属性配置
spring.datasource.initialSize=5
spring.datasource.minIdle=5
spring.datasource.maxActive=20
spring.datasource.maxWait=60000
spring.datasource.timeBetweenEvictionRunsMillis=60000
spring.datasource.minEvictableIdleTimeMillis=300000
spring.datasource.validationQuery=SELECT 1 FROM DUAL
spring.datasource.testWhileIdle=true
spring.datasource.testOnBorrow=false
spring.datasource.testOnReturn=false
spring.datasource.poolPreparedStatements=true
spring.datasource.maxPoolPreparedStatementPerConnectionSize=20
spring.datasource.filters=stat,wall,log4j
spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
#mybatis.config
mybatis.mapper-locations=classpath:mapping/*xml
mybatis.type-aliases-package=cn.vision.springbootdemo.model
#分页插件
pagehelper.helper-dialect=mysql
pagehelper.reasonable=true
pagehelper.support-methods-arguments=true
pagehelper.params=count=countSql
#日志
logging.level.cn.vision.weixindemo.mapper = debug
# spring 设置缓存方案为ehcache
spring.cache.type=ehcache
spring.cache.ehcache.config=classpath:cache/ehcache.xml
微信工具类———微信post、get请求工具类
package cn.vision.weixindemo.utils.base;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.ht