第一种(@Aspect切面类)
package com.gx.interceptor;import lombok.AllArgsConstructor;import lombok.extern.slf4j.Slf4j;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.Around;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Pointcut;import org.aspectj.lang.reflect.MethodSignature;import org.springframework.stereotype.Component;import java.lang.reflect.Method;@Aspect@Component@AllArgsConstructor@Slf4jpublic class AuthorityHandlerAspect { @Pointcut("execution(* com.gx.controller.*.*(..))") public void executeService() { } @Around("executeService()") public Object authorityHandler(ProceedingJoinPoint pjp) throws Throwable { MethodSignature methodSignature = (MethodSignature) pjp.getSignature(); Method method = methodSignature.getMethod(); /判断方法上是否有此注解 if (method.isAnnotationPresent(WorkSheetAnnotation.class)) {} //判断类上是否有次注解 if(pjp.getTarget().getClass().isAnnotationPresent(WorkSheetAnnotation.class)){} //获取方法上的注解值 method.getAnnotation(NoAuth.class) log.info("method{}",method); System.out.println("!!!!!!!!!!!!!!!!!已被拦截"); return pjp.proceed(); } }
@Target({METHOD, FIELD})@Retention(RUNTIME)@Documentedpublic @interface NoAuth{ String message() default "";}
/** * @Description 工单类注解 * @Author GX * @Date 2019/5/7 10:02 * @Version V1.0 **/@Target(TYPE)@Retention(RUNTIME)@Documentedpublic @interface WorkSheetAnnotation {}
//获取当前请求的URIHttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();String uri = request.getRequestURI()
第二种(自定义拦截器类)
第一步 package com.chitic.bank.web.interceptor;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.http.HttpHeaders;import org.springframework.web.servlet.ModelAndView;import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;public class CrossDomainInterceptor extends HandlerInterceptorAdapter{ /** *在整个请求结束之后被调用,也就是在DispatcherServlet 渲染了对应的视图之后执行(主要是用于进行资源清理工作)
*/ @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { } @Override public void afterConcurrentHandlingStarted(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { } /** *请求处理之后进行调用,但是在视图被渲染之前(Controller方法调用之后)
*/ @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { } /**
*在请求处理之前进行调用(Controller方法调用之前)
**/ @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { if (request.getHeader(HttpHeaders.ORIGIN) != null) { response.setHeader("Access-Control-Allow-Origin", "*"); response.setHeader("Access-Control-Allow-Methods", "POST, GET"); response.setHeader("Access-Control-Allow-Headers", "token,Content-Type"); response.setHeader("Access-Control-Expose-Headers", "token"); } return true; } } 第二步:注册自定义拦截器
package com.chitic.bank.web.config; import com.chitic.bank.web.interceptor.CrossDomainInterceptor; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.annotation.Order; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; @Configuration public class CustomWebConfig extends WebMvcConfigurerAdapter { @Order(1) //作用,值越小,越优先执行 @Bean @ConditionalOnMissingBean(CrossDomainInterceptor.class) public CrossDomainInterceptor crossDomainInterceptor() { return new CrossDomainInterceptor(); } @Override public void addInterceptors(InterceptorRegistry registry) { super.addInterceptors(registry); registry.addInterceptor(crossDomainInterceptor()); } }