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

Spring拦截器实现主要依靠与HandlerInterceptorWebMvcConfigurer

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);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值