import cn.hutool.extra.spring.SpringUtil;
import cn.hutool.http.HttpUtil;
import cn.RedisUtil;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
/**
* <p>
* 获取微信的token工具类
* </p>
*
* @author wss
* @date 2021-06-07
* */
@Slf4j
public class AccessTokenUtil {
//获取access_token的接口地址
public static String accessTokenUrl;
//应用ID
public static String appId;
//开发者密钥
public static String secret;
public static RedisUtil redisUtil= SpringUtil.getBean("redisUtil");
/**
* 获取token
* */
public static String getToken () {
//先查看Redis当中有没有,如果没有,就请求微信的接口,同时存储到Redis,同时设置过期时间
String token = (String) redisUtil.get("wechat_token");
if (token == null) {
//Redis没有,请求微信的接口
try {
//构建url
String url = accessTokenUrl + "?grant_type=client_credential&appid=" + appId + "&secret=" + secret;
//3、请求接口,拿到响应信息
String response = HttpUtil.get(url);
//处理返回的结果
//拿到响应回来的token
token = JSON.parseObject(response).getString("access_token");
//存放到Redis
redisUtil.set("wechat_token" , token , 7100);
}catch (Exception e) {
log.error("获取token时异常:" + e);
}
}
return token;
}
}
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
/**
* @Auther: wss
* @Date: 2020/5/26 10:32
* @Description:
*/
@Service
public class RedisUtil {
@Autowired
private RedisTemplate<String, Object> redisTemplate;
// =============================common============================
/**
* 指定缓存失效时间
* @param key 键
* @param time 时间(秒)
* @return
*/
public boolean expire(String key, long time) {
try {
if (time > 0) {
redisTemplate.expire(key, time, TimeUnit.SECONDS);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
// ============================String=============================
/**
* 普通缓存获取
* @param key 键
* @return 值
*/
public Object get(String key) {
return key == null ? null : redisTemplate.opsForValue().get(key);
}
/**
* 普通缓存放入
* @param key 键
* @param value 值
* @return true成功 false失败
*/
public boolean set(String key, Object value) {
try {
redisTemplate.opsForValue().set(key, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 普通缓存放入并设置时间
* @param key 键
* @param value 值
* @param time 时间(秒) time要大于0 如果time小于等于0 将设置无限期
* @return true成功 false 失败
*/
public boolean set(String key, Object value, long time) {
try {
if (time > 0) {
redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
} else {
set(key, value);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 删除缓存
* @param key 可以传一个值 或多个
*/
@SuppressWarnings("unchecked")
public void del(String... key) {
if (key != null && key.length > 0) {
if (key.length == 1) {
redisTemplate.delete(key[0]);
} else {
redisTemplate.delete(CollectionUtils.arrayToList(key));
}
}
}
/**
* 删除缓存
* @param keys 可以传一个值 或多个
*/
@SuppressWarnings("unchecked")
public void del(Collection keys) {
if (org.apache.commons.collections4.CollectionUtils.isNotEmpty(keys)) {
redisTemplate.delete(keys);
}
}
/**
* 前缀获取所有
* @param prefix
* @return
*/
public Map<String, Object> getAllKeyValues(String prefix) {
Collection<String> allKeys = this.getAllKeys(prefix);
HashMap<String, Object> results = CollectionUtil.newHashMap();
for (String key : allKeys) {
results.put(key, this.get(key));
}
return results;
}
/**
* 前缀获取所有key
* @param prefix
* @return
*/
public Collection<String> getAllKeys(String prefix) {
Set<String> keys = redisTemplate.keys(prefix + "*");
if (keys != null) {
// 去掉缓存key的common prefix前缀
return keys.stream().map(key -> StrUtil.removePrefix(key, prefix)).collect(Collectors.toSet());
} else {
return CollectionUtil.newHashSet();
}
}
}
标签:return,String,微信,获取,token,key,import,public
From: https://www.cnblogs.com/guobabiancheng/p/17487992.html