SpringBoot2自定义注解式拦截器

SpringBoot2自定义注解式拦截器

最近比较忙,好像咕咕咕太久了...
为了防止你们不要把我忘了,于是乎从我的CSDN搬运篇文章过来刷下存在感~

1.自定义注解

@Inherited
@Documented
@Target({ElementType.TYPE,ElementType.METHOD})  //作用于类、接口等与方法上
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
	int intVal() default 5;
	String stringVal() default "";
}

2.自定义拦截器

有两种方法,第一种:

@Component
public class MyAnnotationInterceptor implements HandlerInterceptor {

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)throws Exception {
		if (handler instanceof HandlerMethod) {
			Method method = ((HandlerMethod) handler).getMethod();
				//判断该方法/类是否被@MyAnnotation注解修饰
			if (AnnotatedElementUtils.isAnnotated(method, MyAnnotation.class)) {
				MyAnnotation myAnnotation = method.getAnnotation(MyAnnotation.class);
				if (myAnnotation == null) {
	    		return true;
	    		}
				//获取注解的参数
				int intVal = myAnnotation.intVal();
				String stringVal = myAnnotation.stringVal();
				System.out.println("intVal=" + intVal + "stringVal=" + stringVal);
				
				//继续处理...
				
				return false;
			}
		}
		return true;
	}
	
	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,ModelAndView modelAndView) throws Exception {
	}
	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)throws Exception {
	}

第二种:

@Component
public class MyAnnotationInterceptor implements HandlerInterceptor {

	 @Override
	 public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
	    if (handler instanceof HandlerMethod) {
	    	Method method = ((HandlerMethod) handler).getMethod();
	    	//如果没被@MyAnnotation注解修饰
	    	if (!method.isAnnotationPresent(MyLimit.class)) {
	    		return true;
	    		}
	    	MyAnnotation myAnnotation = method.getAnnotation(MyAnnotation.class);
	    	if (myAnnotation == null) {
	    		return true;
	    		}
	    	int intVal = myAnnotation.intVal();
	    	String stringVal = myAnnotation.stringVal();
	    	System.out.println("intVal=" + intVal + "stringVal=" + stringVal);
	    	//继续处理...
				
	    	return false;
	     }
	    return true;
	 }
	
	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,ModelAndView modelAndView) throws Exception {
	}
	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)throws Exception {
	}

3.Config配置

注意此处与SpringBoot1.x配置不同,Spring2.x中WebMvcConfigurerAdapter被deprecated了,改为实现WebMvcConfigurer

@Configuration
public class MyAnnotationInterceptorConfig implements WebMvcConfigurer {

	@Resource
	private MyAnnotationInterceptor myAnnotationInterceptor;
	
	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		// 自定义拦截器,添加拦截路径和排除拦截路径
		registry.addInterceptor(myAnnotationInterceptor).addPathPatterns("/**");
	}
	
}

开始使用

@RestController
public class MyController {
	@MyLimit(intVal = 2,stringVal = "hello")
	@GetMapping("/testmyannotation")
	public String testMyAnnotation(@PathVariable("id") Long id) {
		return "success";
	}
}