• springboot过滤器和拦截器(二)


    springboot过滤器和拦截器

    定义一个拦截器从,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);
        }
  • 相关阅读:
    感想
    正则表达式
    推送、透传、MQ
    Spring集成Quartz定时任务 ---- 定时执行
    代码优化
    nginx配置详解、端口重定向和504
    JAVA实现EXCEL导出
    js 按需加载
    MyBatis使用(二)分页查询
    MyBatis使用(一)
  • 原文地址:https://www.cnblogs.com/wangbin2188/p/16501063.html
Copyright © 2020-2023  润新知