有白名单,黑名单方式,建议使用黑名单,好处是消耗资源少, 减轻服务器压力。
黑名单实现方式如下:
1.在退出接口中加上如下示例:
// 引入相关
// import cn.hutool.jwt.JWT;
// import cn.hutool.jwt.JWTUtil;
// import java.time.*;
try{
JWT jwt = JWTUtil.parseToken(token);
cn.hutool.json.JSONObject jsonObject = jwt.getPayloads();
String jti = (String) jsonObject.get("jti");
// 过期时间
Long exp = jsonObject.getLong("exp");
// 将 Unix 时间戳转换为 Instant 对象
Instant expirationTime = Instant.ofEpochSecond(exp);
// 获取当前时间
Instant now = Instant.now();
// 计算剩余时间
Duration remainingTime = Duration.between(now, expirationTime);
// 设置exp过期时间为key的过期时间
redisUtils.set(CacheNames.JTI_PREFIX + jti, "", remainingTime.getSeconds());
}catch (Exception e){
logger.error("设置黑名单失败", e);
}
- 使用 redisTemplate 需要先序列化
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<Object, Object> template = new RedisTemplate();
template.setConnectionFactory(redisConnectionFactory);
//设置key的序列化方式
template.setKeySerializer(RedisSerializer.string());
template.setHashKeySerializer(RedisSerializer.string());
//设置值的序列化方式
template.setValueSerializer(RedisSerializer.json());
template.setHashValueSerializer(RedisSerializer.json());
return template;
}
}
3.找到鉴权相关代码
(下图是shiro相关的自定义过滤器)
4.在过滤器中加黑名单判断