AOP,全称是Aspect Oriented Programming,即面向切面编程。AOP的目的是将那些与业务无关,但是业务模块都需要的功能,如日志统计、安全控制、事务处理等,封装成可重用的组件,从而将它们从业务逻辑代码中划分出来,编写成独立的切面。这样做,既可以保持业务逻辑的纯净和高内聚性,又可以使得系统的多个模块都可以共享这些公共的功能。 Spring框架提供了对AOP的支持,Spring Boot自然也不例外。使用Spring Boot的AOP功能,我们可以在运行时动态地将代码横向切入到各个关注点(方法或者类)中。这种横向切面的方式,比传统的纵向切面(继承)更加灵活。
xml
java<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> </dependency>
拦截器常用方法 LogInterceptor.preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)方法:拦截请求并记录日志的方法。 LogInterceptor.getIpAddress(HttpServletRequest request)方法:获取请求的IP地址。 LogInterceptor.getCurrentUsername()方法:获取当前用户。 LogInterceptor.getParams(HttpServletRequest request)方法:获取请求的参数。 LogAspect.logAspect()方法:定义AOP切入点,拦截Controller类中的所有方法。 LogAspect.doBefore(JoinPoint joinPoint)方法:执行方法拦截操作,并调用LogInterceptor.preHandle方法来记录日志。
java
@Component
public class LogInterceptor implements HandlerInterceptor {
@Autowired
private LogRepository logRepository;
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
// 获取请求的IP地址
String ip = getIpAddress(request);
// 获取当前用户
String username = getCurrentUsername();
// 获取请求的方法名
String method = request.getMethod();
// 获取请求的URL
String url = request.getRequestURI();
// 获取请求的参数
String params = getParams(request);
// 创建日志实体
Log log = new Log();
log.setIp(ip);
log.setMethod(method);
log.setOperation("访问");
log.setParams(params);
log.setUsername(username);
log.setCreateTime(new Date());
// 保存日志到数据库中
logRepository.save(log);
return true;
}
// 省略实现HandlerInterceptor接口的其他方法
/**
* 获取请求的IP地址
*/
private String getIpAddress(HttpServletRequest request) {
String ip = request.getHeader("X-Forwarded-For");
if (StringUtils.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
if (StringUtils.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if (StringUtils.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_CLIENT_IP");
}
if (StringUtils.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_X_FORWARDED_FOR");
}
if (StringUtils.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
return ip;
}
/**
* 获取当前用户
*/
private String getCurrentUsername() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication != null) {
return authentication.getName();
}
return null;
}
/**
* 获取请求的参数
*/
private String getParams(HttpServletRequest request) {
Map<String, String[]> parameterMap = request.getParameterMap();
if (parameterMap == null || parameterMap.isEmpty()) {
return null;
}
StringBuilder sb = new StringBuilder();
for (Map.Entry<String, String[]> entry : parameterMap.entrySet()) {
sb.append(entry.getKey()).append("=").append(Arrays.toString(entry.getValue())).append("&");
}
return sb.toString();
}
}
java
@Aspect
@Component
public class LogAspect {
@Autowired
private LogInterceptor logInterceptor;
@Pointcut("execution(public * com.example.demo.controller..*.*(..))")
public void logAspect() {}
@Before("logAspect()")
public void doBefore(JoinPoint joinPoint) {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
if (attributes == null) {
return;
}
HttpServletRequest request = attributes.getRequest();
HttpServletResponse response = attributes.getResponse();
HandlerMethod handlerMethod = (HandlerMethod) joinPoint.getSignature();
try {
logInterceptor.preHandle(request, response, handlerMethod);
} catch (Exception e) {
e.printStackTrace();
}
}
}
本文作者:Weee
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!