拦截器设计

本文介绍了Java Spring框架中拦截器的设计和配置方法,包括拦截器配置类的实现以及根据业务需求进行的拦截器设置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一 拦截器配置类

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import pro.tjkj.zauth.client.interceptor.ZAuthInterceptor;

import javax.annotation.Resource;

@Slf4j
public class InterceptorConfig implements WebMvcConfigurer {

    @Resource
    ZAuthInterceptor zAuthInterceptor;
    @Resource
    ZAuthConfigurationProperties zAuthConfigurationProperties;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        if (!zAuthConfigurationProperties.isApiGuard()) {
            log.warn("ZAuth API Guard Is Not Enable, Your API Port Is Danger!");
        }else {
            registry.addInterceptor(zAuthInterceptor);
        }
    }
}

二 拦截器设置(根据业务进行配置)

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.mvc.ParameterizableViewController;
import org.springframework.web.servlet.resource.ResourceHttpRequestHandler;
import pro.tjkj.zauth.client.annotation.ZAuthFilter;
import pro.tjkj.zauth.client.config.ZAuthConfigurationProperties;
import pro.tjkj.zauth.client.exception.AuthInterceptException;
import pro.tjkj.zauth.client.service.ZAuthCallBack;
import pro.tjkj.zauth.client.service.ZAuthService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.annotation.Annotation;


@Component
@Slf4j
public class ZAuthInterceptor implements HandlerInterceptor {

    @Resource
    private ZAuthConfigurationProperties zAuthConfigurationProperties;
    @Resource
    ZAuthService zAuthService;
    ZAuthCallBack zAuthCallBack = null;


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if (request.getMethod().equals("OPTIONS")) {
            return true;
        }
        boolean showLog = zAuthConfigurationProperties.getLogMode() == ZAuthConfigurationProperties.LogMode.DEBUG;
        HandlerMethod method;
        try {
            method = (HandlerMethod) handler;
        } catch (ClassCastException classCastException) {
            if (handler instanceof ParameterizableViewController || handler instanceof ResourceHttpRequestHandler) {
                return true;
            }
            if (showLog) {
                log.info(">>> === API Guard === Intercept === >>>");
            }
            throw new AuthInterceptException(">>> === API Guard === Intercept === >>>",true);
        }
        boolean haveZAuthAnnotation = false;
        for (Annotation annotation : method.getMethod().getAnnotations()) {
            if (annotation instanceof ZAuthFilter) {
                haveZAuthAnnotation = true;
                ZAuthFilter zAuthFilter = (ZAuthFilter) annotation;
                if (zAuthFilter.any()) {
                    if (showLog) {
                        log.info(">>> === API Guard === Method === " + handler + " === >>>");
                        log.info(">>> === API Guard === Any Request Pass === >>> ");
                    }
                    return true;
                }
            }
        }

        if (zAuthConfigurationProperties.isApiGuard()) {
            if (!haveZAuthAnnotation) {
                if (showLog) {
                    log.info(">>> === API Guard === Method === " + handler + " === >>>");
                }
                if (zAuthService.sign(zAuthCallBack) || zAuthService.haveToken(zAuthCallBack)) {
                    if (showLog) {
                        log.info(">>> === API Guard === Match Success === >>>");
                    }
                    return true;
                } else {
                    if (showLog) {
                        log.info(">>> === API Guard === Intercept === >>>");
                    }
                    throw new AuthInterceptException(">>> === API Guard === Intercept === >>>", true);
                }
            }
        }

        return HandlerInterceptor.super.preHandle(request, response, handler);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

没伞的孩子努力奔跑

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

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

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

打赏作者

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

抵扣说明:

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

余额充值