基于自定义注解实现拦截器

上一篇文章写了怎么实现自定义注解

https://blog.csdn.net/qq_29569183/article/details/109310967

在个拦截器中解析并使用注解

@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface ApiIdempotent {
}

下面代码主要分析下如何分析注解

1.handler转换为HandlerMethod 对象

 HandlerMethod handlerMethod = (HandlerMethod) handler;

2.获取HandlerMethod 对象的Method属性

  Method method = handlerMethod.getMethod();

3.获取Method上的注解 

ApiIdempotent methodAnnotation = method.getAnnotation(ApiIdempotent.class)

4.判断注解是否存在,做后续的逻辑处理

拦截器的使用就不赘述了,实现HandlerInterceptor接口重写方法,再基于WebMvcConfigurer 做配置使拦截器生效即可。

public class ApiIdempotentInterceptor implements HandlerInterceptor {

    private static final String VERSION_NAME = "version";

    private RedisTemplate<String, Object> redisTemplate;


    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        //如果不是一个接口请求(比如静态资源类的),那么handler就不是一个HandlerMethod
        if (!(handler instanceof HandlerMethod)) {
            return true;
        }

        HandlerMethod handlerMethod = (HandlerMethod) handler;
        Method method = handlerMethod.getMethod();

        //获取目标方法上的幂等注解
        ApiIdempotent methodAnnotation = method.getAnnotation(ApiIdempotent.class);
        if (methodAnnotation != null) {
            // 幂等性校验, 校验通过则放行, 校验失败则抛出异常, 并通过统一异常处理返回友好提示
            checkApiIdempotent(request);
        }
        return true;
    }


    private void checkApiIdempotent(HttpServletRequest request) {
        String version = request.getHeader(VERSION_NAME);
        if (StringUtils.isBlank(version)) {// header中不存在token
            version = request.getParameter(VERSION_NAME);
            if (StringUtils.isBlank(version)) {// parameter中也不存在token
                throw new IllegalArgumentException("无效的参数");
            }
        }
        if (!redisTemplate.hasKey(version)) {
            throw new IllegalArgumentException("不存在对应的参数");
        }
        Boolean bool = redisTemplate.delete(version);
        if (!bool) {
            throw new IllegalArgumentException("没有删除对应的version");
        }
    }

    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }

    public ApiIdempotentInterceptor(RedisTemplate<String, Object> redisTemplate) {
        this.redisTemplate = redisTemplate;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

WannaRunning

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值