定义一个拦截器从,session中反查出用户信息,当然这只是一个demo
@Component public class HttpRequestInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { String cookie = response.getHeader("Set-Cookie"); User user = User.build(cookie); request.setAttribute("user",user); return true; } }
注册拦截器
@Configuration public class RegisterInterceptorConfig implements WebMvcConfigurer { @Autowired private HttpRequestInterceptor httpRequestInterceptor; @Override public void addInterceptors(InterceptorRegistry registry) { InterceptorRegistration registration = registry.addInterceptor(httpRequestInterceptor); registration.addPathPatterns("/**"); } }
现在每一个Request的请求都会本拦截,然后再attribute中放入一个user对象。
现在把user取出来
@GetMapping(value = "list") public Response<InfoVO> listProcess(@RequestParam Integer page, @RequestParam Integer size, @RequestAttribute("user") User user ) { log.info(user.toString()); return Response.single(all); }