package com.example.demo.config;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
@Component
@Aspect
public class TestInterceptor {
@Around("execution(* com.example.demo..*(..)) && @annotation(org.springframework.web.bind.annotation.RequestMapping)")
public Object Test(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
Object object = null;
RequestAttributes res = RequestContextHolder.getRequestAttributes();
ServletRequestAttributes srs = (ServletRequestAttributes) res;
HttpServletRequest httpServletRequest= srs.getRequest();
String url = httpServletRequest.getRequestURI();
boolean next = false;
// 放行/useTest下面的所有请求;拦截其他请求
if(url.startsWith("/useTest")){
next = true;
}
System.out.println(url);
if(next) {
System.out.println("开始: begin around");
object = proceedingJoinPoint.proceed();
System.out.println("结束: end around");
return object;
}else{
return "false";
}
}
}
springboot使用@Aspect注解方式实现拦截
最新推荐文章于 2024-03-11 19:06:55 发布
