HandlerInterceptor拦截器搭配注解实现web请求的增强处理

8 篇文章 0 订阅

         自定义实现HandlerInterceptor的实现类,同时自定义WebMvcConfigurationSupport的实现类,重写其addIntecepters方法注册改拦截器。

@Configuration
public class SpringWebConfig extends WebMvcConfigurationSupport {         
     @Bean
     MarkAuthInteceptor markAuthInteceptor(){
          return new MarkAuthInteceptor();
     }
     @Override
     public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(interfaceAuthInterceptor()).addPathPatterns("/**").excludePathPatterns(filterPath);
        registry.addInterceptor(markAuthInteceptor()).addPathPatterns("/**");
        super.addInterceptors(registry);
     }
}
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
public @interface MarkAuth {

    boolean validateType() default false;
}
import com.educationtek.common.annotation.MarkAuth;
import com.educationtek.common.domain.UserRelationInfo;
import com.educationtek.common.domain.response.MarkingAcl;
import com.educationtek.common.enums.MarkAuthEnums;
import com.educationtek.markingsystem.thirdpart.UserCenter;
import org.springframework.beans.factory.annotation.Autowired;
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;
import java.lang.reflect.Method;
import java.util.Set;
public class MarkAuthInteceptor implements HandlerInterceptor {

    @Autowired
    private UserCenter userCenterRest;

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

        Boolean authRet = false;

        if (handler instanceof  HandlerMethod) {

            UserRelationInfo userInfo = (UserRelationInfo) request.getAttribute("user");
            String token = request.getParameter("token");

            String uri = request.getRequestURI();

            HandlerMethod handlerMethod = (HandlerMethod) handler;

            // controller
            Class<?> clazz = handlerMethod.getBeanType();
            Method method = handlerMethod.getMethod();

            Boolean continueTab = false;
            if (clazz != null && method != null) {
                MarkAuth cmarkAuth = clazz.getAnnotation(MarkAuth.class);
                if (cmarkAuth != null && cmarkAuth.validateType()) {
                    continueTab = true;
                }
                MarkAuth methodMark = method.getAnnotation(MarkAuth.class);
                if (methodMark != null) {
                    if (methodMark.validateType()) {
                        continueTab = true;
                    } else {
                        continueTab = false;
                    }
                }
            }

            if (!continueTab) {
                return true;
            }

            MarkingAcl permission = userCenterRest.getUserPermission();
            if (permission != null && permission.getAcl() != null) {
                Set<String> retSet = permission.getAcl().get("marking");
                if (retSet != null && !retSet.isEmpty()) {
                    System.out.println(MarkAuthEnums.getAuthValueByRequestUri(uri));
                    for (String element : retSet) {
                        if (MarkAuthEnums.getAuthValueByRequestUri(uri).contains(element)) {
                            authRet = true;
                        }
                    }
                }
            }
        }
        return authRet;
    }

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

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值