11月 12 12345678910111213141516171819202122232425262728293031323334@Configurationpublic class RequestInterceptor implements WebMvcConfigurer { @Autowired private RedisTemplate redisTemplate; private List<String> patterns = new ArrayList<String>(); //不用过滤的url public void addInterceptor(InterceptorRegistry registry){ //写一个拦截器 registry.addInterceptor(new HandlerInterceptor() { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { //从请求头中获取token String token = request.getHeader("Authorization");// 从redis中获取token if(token != null && redisTemplate.opsForValue().get(token) != null){ //每次认证后就充值为30天 时间单位:天 redisTemplate.expire(token,30, TimeUnit.DAYS); return true; //取到就返回true } //设置响应状态为401 response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); return false; } }).excludePathPatterns(patterns); //不用过滤的url }} 上一篇 自定义UserDetailsServiceImpl 下一篇 JWT 生成Token、解析Token的简单工具类