Spring拦截指定路径并对指定注解做处理

  • Post author:
  • Post category:其他


Spring拦截器实现主要依靠与

HandlerInterceptor



WebMvcConfigurer


HandlerInterceptor:主要是对我们拦截的路径进行方法的增强,核心的三个方法有preHandle、postHandle、afterCompletion

package handlerMethod;

import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

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

/**
 * @program: Test
 * @description: 拦截器
 * @author: 
 * @create: 2023-02-07 17:16
 **/

@Component
public class MyHandlerInterceptor implements HandlerInterceptor {

    /**
     *  对拦截之前对方法进行处理
     * @param request 请求
     * @param response  响应
     * @param handler   拦截方法
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 获取请求中的token
        String token = request.getHeader("token");
        String account = request.getHeader("account");
        String password = request.getHeader("password");
        if (handler instanceof HandlerMethod) {
            HandlerMethod handlerMethod = (HandlerMethod)handler;
            // 获取 是否有我定义的 MyAnnottation 注解
            if (handlerMethod.hasMethodAnnotation(MyAnnottation.class)) {
                MyAnnottation annotation = handlerMethod.getMethodAnnotation(MyAnnottation.class);
                String reason = annotation.reason();
                System.out.println(" 拦截的理由是: " + reason);
                String methodName = handlerMethod.getMethod().getName();
                System.out.println(" 方法的名称是: " + methodName);
                if ("".equals(reason)){
                    // 拦截
                    return false;
                }else {
                    // 放行
                    return true;
                }
            }
        }
        // 放行
        return true;
    }

    /** 对方法执行完做处理
     * @param request
     * @param response
     * @param handler
     * @param modelAndView
     * @throws Exception
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
    }

    /**
     *  对整个请求执行完成做处理
     * @param request
     * @param response
     * @param handler
     * @param ex
     * @throws Exception
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
    }
}


WebMvcConfigurer:主要是对刚刚实现的拦截器生效

package handlerMethod;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @program: Test
 * @description: 我的拦截器配置
 * @author:
 * @create: 2023-02-07 17:50
 **/
@Configuration
public class MyWebMvcConfigurer implements WebMvcConfigurer {

    @Autowired
    private MyHandlerInterceptor myHandlerInterceptor;

    /**
     * 添加我们自定义的拦截器
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //addPathPatterns方法添加拦截路径匹配规则("/**"是拦截所有),excludePathPatterns方法是设置白名单,放行哪些路
        registry.addInterceptor(myHandlerInterceptor).addPathPatterns("/needToInterceptor/**").excludePathPatterns("/login/*").order(1);
    }
}



版权声明:本文为weixin_43718423原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。