springboot-12-自定义拦截器的配置interceptor

springmvc中拦截器的概念已经被弱化了, springboot中使用的也不甚广泛, 通常在用户登录等方面仍有用处

创建拦截器步骤: 

1, 创建拦截器类继承HandlerInterceptor
2, 创建java类继承WebMvcConfigurerAdapter, 重写addInterceptors()方法
3, 在addInterceptor中添加自己的拦截器组成拦截器链

1, 对请求的拦截器类: 

package com.iwhere.test.web.intercepter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

/**
 * springboot 配置intercepter
 * @author wenbronk
 * @time 2017年3月17日  下午4:58:58  2017
 */

@Component
public class MyIntercepter implements HandlerInterceptor { @Override public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3) throws Exception { System.out.println(">>>MyInterceptor1>>>>>>>在整个请求结束之后被调用,也就是在DispatcherServlet 渲染了对应的视图之后执行(主要是用于进行资源清理工作)"); } @Override public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3) throws Exception { System.out.println(">>>MyInterceptor1>>>>>>>请求处理之后进行调用,但是在视图被渲染之前(Controller方法调用之后)"); } @Override public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception { System.out.println(">>>MyInterceptor1>>>>>>>在请求处理之前进行调用(Controller方法调用之前)"); return true; } }

2, 注册拦截器

package com.iwhere.test.web.intercepter;

import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**
 * 在此类中配置拦截器, 可添加拦截器栈
 * @author wenbronk
 * @time 2017年3月17日  下午5:01:38  2017
 */

@SpringBootConfiguration
public class MyWebMvcConfigureAdapter extends WebMvcConfigurerAdapter { /** * 此方法中添加连接器 */ @Override public void addInterceptors(InterceptorRegistry registry) { super.addInterceptors(registry); registry.addInterceptor(new MyIntercepter()).addPathPatterns("/**"); // 添加多个拦截器 , 组成拦截器栈 // registry.addInterceptor(new MyIntercepter2()).addPathPatterns("/intercepter/pre"); } }

然后, 进行项目访问, 既可以进行拦截. . .

3. 对方法的拦截

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
 
@Aspect
@Component
public class EnhanceMonitor {
    
    //定义切点
    @Pointcut("execution(* com.peilian.*..attack(..))")
    public void pointCutMethod(){}
    
    //前置
    @Before("pointCutMethod()")
    public void magicEnhance1(){
        System.out.println("----冰霜特效----");
    }
    
    //前置
    @Before("pointCutMethod()")
    public void magicEnhance2(){
        System.out.println("----火焰特效----");
    }
    
    //后置
    @After("pointCutMethod()")
    public void afterAttack(){
        System.out.println("---攻击后僵直---");
    }
    
    /*环绕
    @Around("pointCutMethod()")
    public void buffAttack(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("----骑士光环---");
        pjp.proceed();
        System.out.println("-----骑士光环消失----");
    }*/
}
原文地址:https://www.cnblogs.com/wenbronk/p/6567385.html