springboot 请求拦截器

2021/11/9 6:11:18

本文主要是介绍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("第三拦截了,请求后");
    }
}

 



这篇关于springboot 请求拦截器的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程