springboot实现拦截器(配置多个拦截器)

拦截器方法介绍:

	1. preHandle(......)方法:当url已经匹配到controller层中某个方法时,在方法执行前执行
	它会决定是否放行,返回true,放行,返回false,不会执行
	2.  postHandle(……) 方法:url 匹配到Controller 中的某个方法,且执行完了该方法,
	但是在 DispatcherServlet 视图渲染之前执行。在这个方法中有个 ModelAndView 参数,可以在此做一些修改动作。
	3. afterCompletion(……) 方法:在整个请求处理完成后(包括视图渲染)执行,
	做一些资源的清理工作,这个方法只有在 preHandle(……) 被成功执行后并且返回true才会被执行。

创建自己的拦截器类:
拦截器1:

package com.wty.springboot.interceptor;

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 MyInterceptoroOne implements HandlerInterceptor {


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("在请求到达方法之前调用,通过了,才会去执行目标方法,不通过不会执行");
        String token = request.getParameter("token");
        if(token != null){
            return true;
        }else{
            return false;
        }
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }
}

拦截器2:

package com.wty.springboot.interceptor;

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 MyInterceptorTwo implements HandlerInterceptor {


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("方法前调用");
        String token = request.getParameter("token");
        if(token != null){
            return true;
        }else{
            return false;
        }
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }
}

注册拦截器
创建配置类

SpringBoot 1.5版本都是靠重写 WebMvcConfigurerAdapter 的方式来添加自定义拦截器,
消息转换器等。SpringBoot2.0 后,该类被标识为@Deprecated。官方推荐直接实现
WebMvcConfigurer(推荐)或者直接继承WebMVCConfigurationSupport
package com.wty.springboot.config;

import com.wty.springboot.interceptor.MyInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;



@Configuration
public class Webconfig implements WebMvcConfigurer {

    @Resource
    private MyInterceptoroOne myInterceptorone;
    
	@Resource
    private MyInterceptorTwo myInterceptortwo;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
    	//第一个拦截器
        registry.addInterceptor(myInterceptorone)
        .addPathPatterns("/**");//拦截所有请求
		//第二个拦截器
        registry.addInterceptor(myInterceptortwo)
        .addPathPatterns("/**");
    }
}
WebMvcConfigurer常用接口:
1.addInterceptors:拦截器
2.addViewControllers:页面跳转
3. addResourceHandlers:静态资源
4. configureDefaultServletHandling:默认静态资源处理器
5. configureViewResolvers:视图解析器
6. configureContentNegotiation:配置内容裁决的一些参数
7. addCorsMappings:跨域
8. configureMessageConverters:信息转换器

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(loginInterceptor())
                .addPathPatterns("/**")//拦截所有请求
                .excludePathPatterns(  //不需要拦截的请求
                        "/iot/user/**",
                        "/",
                        "/static/**",
                        "/**/*.ico",
                        "/**/*.png",
                        "/**/*.html",
                        "/error",
                        "/collect/iot/setSlaveDataDB"
 
                );

你可能感兴趣的:(java,笔记,spring,boot,java)