import com.xxx.LogAop; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.http.ResponseEntity; 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; import java.io.BufferedReader; import java.util.Date; import java.util.HashMap; import java.util.Map; /** * 记录日志的切面 * 目的:记录访问日志信息 * @author lff */ @Aspect //定义一个切面 @Configuration public class LogRecordAspect { // 可以定义切点Pointcut 自己使用注解来标记处理就不需要再定义切点了 // @Pointcut("execution(* com.xxx.controller.*Controller.*(..))") // public void excudeService() { // } @Autowired private LogInfoService logInfoService; @Autowired private JsonUtils jsonUtils; //拦截 环绕 @Around(" @annotation(logAop)") public Object doAround(ProceedingJoinPoint pjp, LogAop logAop) throws Throwable { // RequestAttributes ra = RequestContextHolder.getRequestAttributes(); ServletRequestAttributes sra = (ServletRequestAttributes) ra; HttpServletRequest request = sra.getRequest(); //访问ip String ip = IpUtils.getIpAddr(request); //拦截的接口 String uri = request.getRequestURI(); //入参 String parameter = this.getParameter(request); //从请求头获取信息 String token = request.getHeader("token"); } }
package com.xxx.annotation;xxx import org.springframework.core.annotation.AliasFor; import java.lang.annotation.*; /** * 自定义注解类 * 目的:需要记录日志的接口 */ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) @Documented public @interface LogAop { //保存重复提交标记 默认为需要保存 必须要传入接口id参数,否则不拦截 @AliasFor("value") String name() default "未定义接口名字"; @AliasFor("name") String value() default "未定义接口名字"; }