SpringBoot 2.0 自定义拦截器实现(继承WebMvcConfigurer 过时)

SpringBoot 2.0自定义拦截器继承WebMvcConfigurer 过时,所以新的方式方法是实现WebMvcConfigurer 接口
此博客是在有面向切面编程的基础上进行。
Springboot 中的 Interceptor 拦截器也就是 mvc 中的拦截器,只是省去了 xml 配置部分。并没有本质的不同,都是通过实现 HandlerInterceptor 中几个方法实现。
具体实现如下:
第一步还是老步骤:

package com.xxx.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

public class AuthorizedInterceptor implements HandlerInterceptor {	
	 /** 
     * 该方法需要preHandle方法的返回值为true时才会执行。
     * 该方法将在整个请求完成之后执行,主要作用是用于清理资源。
     */  
	public void afterCompletion(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception exception)
			throws Exception {
		
	}

	 /** 
     * 这个方法在preHandle方法返回值为true的时候才会执行。
     * 执行时间是在处理器进行处理之 后,也就是在Controller的方法调用之后执行。
     */  
	public void postHandle(HttpServletRequest request, HttpServletResponse response,
			Object handler, ModelAndView mv) throws Exception {
		
	}

	 /** 
     * preHandle方法是进行处理器拦截用的,该方法将在Controller处理之前进行调用,
     * 当preHandle的返回值为false的时候整个请求就结束了。 
     * 如果preHandle的返回值为true,则会继续执行postHandle和afterCompletion。
     */  
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
			Object handler) throws Exception {

		/** 获得请求的ServletPath */
		String servletPath = request.getServletPath();
		String url = request.getRequestURL().toString();
		/**
		*具体的业务实现
		**/

		System.out.println("url  "+url+"   "+"servletPath: "+servletPath);
        return true;		
	}

}

第二步,实现WebMvcConfigurer 并且注意添加相关的注解

package com.xxx.config;

import com.miaoke.bigdata.api.interceptor.AuthorizedInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;


@Configuration
public class CorsConfig implements WebMvcConfigurer {

    //实例化对象
    @Bean
    public HandlerInterceptor getInterceptor(){
        return new AuthorizedInterceptor();
    }

//解决跨域问题
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")//设置允许跨域的路径
                .allowedOrigins("*")//设置允许跨域请求的域名
                .allowCredentials(true)//是否允许证书 不再默认开启
                .allowedMethods("GET","POST","DELETE","PUT")
                .maxAge(3600);
    }

//添加拦截
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(getInterceptor())//注册自定义拦截器
                .addPathPatterns("/**")//拦截的请求路径
                .excludePathPatterns("/error")//排除的请求路径
                .excludePathPatterns("/static/*");
    }
}

效果

猜你喜欢

转载自blog.csdn.net/kzw11/article/details/88693808