import com.fasterxml.jackson.databind.ObjectMapper;
import com.myjo.luim.common.utils.AddressUtils;
import com.myjo.luim.common.utils.IpUtils;
import com.myjo.luim.common.utils.ServletUtils;
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.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Component;
import org.springframework.data.domain.Pageable;
import java.text.Format;
@Aspect
@Component
@Slf4j
public class PageSizeAspect {
@Pointcut("execution(public * com..*.*Controller.*(..))")
public void doOperation() {
log.info("分页size参数检验AOP");
}
@Around("doOperation()")
public Object doBefore(ProceedingJoinPoint joinPoint) throws Throwable {
Object[] args = joinPoint.getArgs();
for (int i = 0; i < args.length-1; i++) {
if (args[i] instanceof Pageable) {
if (((Pageable) args[i]).getPageSize() > 200){
Pageable pageable = new PageRequest(((Pageable) args[i]).getPageNumber(), 200,((Pageable) args[i]).getSort());
log.info(String.format("拦截到过大size:%s-IP地址:%s",((Pageable) args[i]).getPageSize(),IpUtils.getIpAddr(ServletUtils.getRequest())));
args[i] = pageable;
break;
}
}
}
return joinPoint.proceed(args);
}
}