Interceptor
java
@Slf4j
@Component
public class XxxxInterceptor implements HandlerInterceptor {
@Override
@SneakyThrows
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
....
throw new DefineException("xxxxx....");
....
request.setAttribute("userInfo",userInfo);
return true;
}
Aspect
java
@Aspect
@Component
@Slf4j
@Order(2)
public class DataLimitAspect {
@Autowired
private XxxxService xxxService;
@Pointcut("@annotation(xxx.annotation.DataLimit)")
public void dataLimitPointcut() {
}
@Before("dataLimitPointcut()")
public void before(JoinPoint joinPoint) {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
UserDTO userInfo = (UserDTO)request.getAttribute("userInfo");
....
DataLimit annotation = method.getAnnotation(DataLimit.class);
if (annotation.idOnly()) {
request.setAttribute("id",id);
return;
}
....
for (Object arg : joinPoint.getArgs()) {
if (arg instanceof String){
functionAaa((String) arg ,id);
}
}
....
}
void functionAaa(String str ,Integer id){
....
xxxService.count()...
};
}
GET请求
java
@DataLimit
@GetMapping("/xxx")
@ApiOperation("...")
public AjaxResult getXxxx(@RequestParam String str , HttpServletRequest request ) {
Object idObj = request.getAttribute("id");
if (null!=idObj) {
...
}
}
POST请求
java
@DataLimit
@PostMapping("/xxx")
@ApiOperation("...")
public AjaxResult postXxxx(@RequestPart("file") MultipartFile file,
@RequestAttribute("userInfo") UserDTO userInfo ,
HttpServletRequest request ) {
Object idObj = request.getAttribute("id");
if (null!=idObj) {
...
}
}