接口拦截器–附带了登录拦截
package com.futuredata.cloud.cap.aop;
import com.futuredata.cloud.cap.config.feign.UaaFeignService;
import com.futuredata.cloud.cap.config.feign.vo.UserVo;
import com.futuredata.cloud.cap.mapper.UserLogMapper;
import com.futuredata.cloud.cap.model.UserLog;
import com.futuredata.cloud.cap.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Date;
@Aspect
@Component
@Slf4j
public class LogAspect {
@Autowired
private UserLogMapper mapper;
@Autowired
private UaaFeignService uaaFeignService;
@Pointcut("@annotation(com.futuredata.cloud.cap.aop.Fdlog)")
public void webLog() {
}
@Around("webLog()")
public Object saveLog(ProceedingJoinPoint pjp) {
// 此处joinPoint的实现类是MethodInvocationProceedingJoinPoint
Signature signature = pjp.getSignature();
// 获取参数名
MethodSignature methodSignature = (MethodSignature) signature;
Fdlog fdlog = methodSignature.getMethod().getAnnotation(Fdlog.class);
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
UserVo vo = uaaFeignService.selectUser().getData();
String ipAddress = request.getHeader("X-Real-IP");
String userName = vo.getUserName();
String userId = vo.getId();
String description = fdlog.description();
String type = fdlog.type();
UserLog userLog = new UserLog();
if (StringUtils.isEmpty(ipAddress)) {
userLog.setIpAddress("192.168.1.117");
} else {
userLog.setIpAddress(ipAddress);
}
userLog.setUserId(userId);
userLog.setUserName(userName);
userLog.setEnable(1);
userLog.setDescription(description);
userLog.setDepartId(vo.getDepartId());
userLog.setDepartName(vo.getDepartName());
userLog.setType(type);
userLog.setCreateTime(new Date());
mapper.insertSelective(userLog);
try {
// 直接pjp.proceed(),没有return ,前台页面获取不到ajax数据
return pjp.proceed();
} catch (Throwable e) {
doAfterThrowing(e);
return null;
}
}
@Pointcut("execution(public * com.futuredata.cloud.cap.controller.*.*(..))")
public void log() {
}
@Before("log()")
public void doBefore(JoinPoint joinPoint) throws Throwable {
// 接收到请求,记录请求内容
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
// 记录下请求内容
log.debug("### 请求地址 : " + request.getRequestURL().toString());
log.debug("### 请求类型 : " + request.getMethod());
log.debug("### 请求IP : " + request.getRemoteAddr());
log.debug("### 请求方法 : " + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
log.debug("### 请求参数 : " + Arrays.toString(joinPoint.getArgs()));
}
@AfterReturning(returning = "ret", pointcut = "log()")
public void doAfterReturning(Object ret) throws Throwable {
// 处理完请求,返回内容
log.info("### 请求返回结果 : " + ret);
}
@AfterThrowing(pointcut = "log()", throwing = "e")
public void doAfterThrowing (Throwable e) {
log.error("### 返回异常信息:", e);
}
}
自定义注解
package com.futuredata.cloud.cap.aop;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
@Documented
@Retention(RUNTIME)
@Target(METHOD)
public @interface Fdlog {
String description() default "";
String type() default "查看";
}
注解使用方式及效果图