zoukankan      html  css  js  c++  java
  • 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("第三拦截了,请求后");
        }
    }
  • 相关阅读:
    python实现梯度下降法
    sklearn评估模型的方法
    sklearn进行拟合
    装饰器原理-代码解释
    多进程+协程 处理IO问题
    mongodb增删改查
    关于Redis在Linux手动安装配置
    MongoDB安装配置及使用
    键盘没有小键盘怎么办?怎么打开屏幕软键盘?
    WebService中WSDL和WADL(转)
  • 原文地址:https://www.cnblogs.com/kaibindirver/p/15527052.html
Copyright © 2011-2022 走看看