• springboot 请求拦截器


    参考 https://www.jianshu.com/p/e52444f0a121

    创建一个Config来注册拦截器,代码如下:

    MyMvcConfig

    package com.config;
    
    import com.controle.lanjie.LoginHandlerInterceptor;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
    
    @Configuration
    public class MyMvcConfig implements WebMvcConfigurer {
    
        //所有的WebMvcConfigurerAdapter组件都会一起起作用
        @Bean //将组件注册在容器中
        public WebMvcConfigurer webMvcConfigurerAdapter(){
            return new WebMvcConfigurer(){
    
                //注册拦截器
                @Override
                public void addInterceptors(InterceptorRegistry registry) {
                    //静态资源; *.css,*.js
                    //SpringBoot已经做好了静态资源映射
                    registry.addInterceptor(new LoginHandlerInterceptor()).addPathPatterns("/**");
    //                .excludePathPatterns("/index.html","/","/user/login","/static/**","/webjars/**");
                    // /**  表示拦截所有路径下的所有请求
    //                registry.addInterceptor(new LoginHandlerInterceptor())
    //                        .addPathPatterns("/person.html","/Person.html",
    //                                "/questionnaire.html","/Questionnaire.html",
    //                                "/result.html","/Result.html");
                }
            };
        }
    
    }

    创建一个拦截器

    LoginHandlerInterceptor

    package com.controle.lanjie;
    
    import org.springframework.stereotype.Component;
    import org.springframework.web.servlet.HandlerInterceptor;
    import org.springframework.web.servlet.ModelAndView;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    @Component
    public class LoginHandlerInterceptor implements HandlerInterceptor {
    
        //目标方法执行之前
        @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
            //获取请求参数
            Object user5 = request.getQueryString();
            System.out.println(user5);
            //获取请求头参数
            Object user6 = request.getHeader("currentUser");
            System.out.println(user6);
            //获取请求方式
            Object user7 = request.getMethod();
            System.out.println(user7);
            //获取请求路径
            Object user8 = request.getRequestURI();
            System.out.println(user8);
            //重定向访问路径
            response.sendRedirect("/bbb2");
    //        添加请求头
            response.addHeader("location","http://baidu.com");
    //        if (user == null) {
    //            //未登录,返回登录页面
    //            System.out.println("第一拦截了,请求前");
    ////            response.sendRedirect("/LoginError.html");
    //            return false;
    //        }else {
    //            //放行
                return true;
    //        }
        }
    
        @Override
        public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) {
            System.out.println("第二拦截了,请求中");
        }
    
        @Override
        public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
            System.out.println("第三拦截了,请求后");
        }
    }
  • 相关阅读:
    iview modal关闭问题 自动关闭、异步关闭、定时关闭
    关于localStorage和sessionStorage
    promise简单应用
    Vue(JavaScript)按照内容搜索和排序功能实现
    javascripit常用的验证工具
    vue中$refs的用法及作用
    JS遍历数组的三种方法:map、forEach、filter
    Vue项目搭建,Git提交分支,token验证登录流程(重点)
    axios捕获401 赋值token
    Quartus系列:Quartus II 原理图输入设计
  • 原文地址:https://www.cnblogs.com/kaibindirver/p/15527052.html
Copyright © 2020-2023  润新知