自定义实现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 { } }