1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
@Configuration
public 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

}
}