通辽网站开发招聘,西部数码网站管理助手v4.0,网站数据库做好了 怎么做网页,山西手机版建站系统哪家好文章目录一、常用的场景1. 请求拦截2. 异步保存轨迹二、案例实战2.1. pom2.2. 自定义注解2.3. aop拦截2.4. 测试类2.5. 保存日志一、常用的场景
1. 请求拦截
通过aop 请求拦截#xff0c;举个例子#xff0c;第三方厂商请求平台接口#xff0c;先去数据库查询该接口#…
文章目录一、常用的场景1. 请求拦截2. 异步保存轨迹二、案例实战2.1. pom2.2. 自定义注解2.3. aop拦截2.4. 测试类2.5. 保存日志一、常用的场景
1. 请求拦截
通过aop 请求拦截举个例子第三方厂商请求平台接口先去数据库查询该接口此ip是否有访问权限有如果就通过继续下面的逻辑否则权限访问拦截请求到此结束
2. 异步保存轨迹
见下面案例说一下思路 也是同理同样通过拦截器来实现的利用下的注解即可案例中柚子 Aspect Pointcut(“execution( * com.gblfy.logboot...*(…))”)//两个…代表所有子目录最后括号里的两个…代表所有参数 After Around
二、案例实战
2.1. pom dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-aop/artifactId/dependencydependencygroupIdorg.aspectj/groupIdartifactIdaspectjweaver/artifactId/dependency2.2. 自定义注解
package com.gblfy.logboot.annotation;import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;Retention(RetentionPolicy.RUNTIME)
//注解作用的位置ElementType.METHOD表示该注解仅能作用于方法上
Target(ElementType.METHOD)
public interface Log {String value() default ;
}2.3. aop拦截
package com.gblfy.logboot;import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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;/*** 日志文件记录*/
Aspect
Component
public class WebLogAspect {private static final Logger logger LoggerFactory.getLogger(WebLogAspect.class);AutowiredHttpServletRequest request;Pointcut(execution( * com.gblfy.logboot.*.*.*(..)))//两个..代表所有子目录最后括号里的两个..代表所有参数public void logPointCut() {}Before(logPointCut())public void doBefore(JoinPoint joinPoint) throws Throwable {// 接收到请求记录请求内容ServletRequestAttributes attributes (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();HttpServletRequest request attributes.getRequest();// 记录下请求内容logger.info(请求地址 : request.getRequestURL().toString());logger.info(HTTP METHOD : request.getMethod());// 获取真实的ip地址//logger.info(IP : WebUtils.getIpAddress(request));logger.info(CLASS_METHOD : joinPoint.getSignature().getDeclaringTypeName() . joinPoint.getSignature().getName());logger.info(参数 : Arrays.toString(joinPoint.getArgs()));
// loggger.info(参数 : joinPoint.getArgs());}After(logPointCut())public void doAfter(JoinPoint joinPoint) throws Throwable {System.out.println(request--- request.getAttribute(aa));}Around(logPointCut())public Object doAround(ProceedingJoinPoint pjp) throws Throwable {long startTime System.currentTimeMillis();Object ob pjp.proceed();// ob 为方法的返回值logger.info(耗时 : (System.currentTimeMillis() - startTime));return ob;}
}
2.4. 测试类
package com.gblfy.logboot.controller;import com.gblfy.logboot.annotation.Log;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;RequestMapping(/log)
RestController(日志控制器)
public class LogController {Log(测试收集日志)RequestMapping(/save)public String saveLog(RequestParam(name token) String token,RequestParam(name name) String name,HttpServletRequest request, HttpServletResponse response) {System.out.println(开始收集logtokenname);request.setAttribute(aa,assddddd);return 收集日志succes333s;}
// http://localhost:8080/log/save?token123
}2.5. 保存日志