Skip to content

Commit ea2d4e5

Browse files
committed
aop-log,问题service没有答应日志
1 parent 1f49d81 commit ea2d4e5

File tree

12 files changed

+405
-0
lines changed

12 files changed

+405
-0
lines changed

springboot-aop-log/.gitignore

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
HELP.md
2+
/target/
3+
!.mvn/wrapper/maven-wrapper.jar
4+
5+
### STS ###
6+
.apt_generated
7+
.classpath
8+
.factorypath
9+
.project
10+
.settings
11+
.springBeans
12+
.sts4-cache
13+
14+
### IntelliJ IDEA ###
15+
.idea
16+
*.iws
17+
*.iml
18+
*.ipr
19+
20+
### NetBeans ###
21+
/nbproject/private/
22+
/nbbuild/
23+
/dist/
24+
/nbdist/
25+
/.nb-gradle/
26+
/build/
27+
28+
### VS Code ###
29+
.vscode/

springboot-aop-log/pom.xml

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>org.springframework.boot</groupId>
7+
<artifactId>spring-boot-starter-parent</artifactId>
8+
<version>2.1.3.RELEASE</version>
9+
<relativePath/> <!-- lookup parent from repository -->
10+
</parent>
11+
<groupId>cn.tellsea</groupId>
12+
<artifactId>springboot-aop-log</artifactId>
13+
<version>0.0.1-SNAPSHOT</version>
14+
<name>springboot-aop-log</name>
15+
<description>Demo project for Spring Boot</description>
16+
17+
<properties>
18+
<java.version>1.8</java.version>
19+
</properties>
20+
21+
<dependencies>
22+
<dependency>
23+
<groupId>org.springframework.boot</groupId>
24+
<artifactId>spring-boot-starter-web</artifactId>
25+
</dependency>
26+
27+
<dependency>
28+
<groupId>org.projectlombok</groupId>
29+
<artifactId>lombok</artifactId>
30+
<optional>true</optional>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.springframework.boot</groupId>
34+
<artifactId>spring-boot-starter-test</artifactId>
35+
<scope>test</scope>
36+
</dependency>
37+
38+
<!-- aop -->
39+
<dependency>
40+
<groupId>org.springframework.boot</groupId>
41+
<artifactId>spring-boot-starter-aop</artifactId>
42+
</dependency>
43+
44+
<dependency>
45+
<groupId>com.alibaba</groupId>
46+
<artifactId>fastjson</artifactId>
47+
<version>1.2.56</version>
48+
</dependency>
49+
</dependencies>
50+
51+
<build>
52+
<plugins>
53+
<plugin>
54+
<groupId>org.springframework.boot</groupId>
55+
<artifactId>spring-boot-maven-plugin</artifactId>
56+
</plugin>
57+
</plugins>
58+
</build>
59+
60+
</project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package cn.tellsea;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class SpringbootAopLogApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(SpringbootAopLogApplication.class, args);
11+
}
12+
13+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package cn.tellsea.annotation;
2+
3+
import java.lang.annotation.*;
4+
5+
@Target({ElementType.PARAMETER, ElementType.METHOD})//作用在参数和方法上
6+
@Retention(RetentionPolicy.RUNTIME)//运行时注解
7+
@Documented//表明这个注解应该被 javadoc工具记录
8+
public @interface SystemControllerLog {
9+
10+
String description() default "";
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package cn.tellsea.annotation;
2+
3+
import java.lang.annotation.*;
4+
5+
@Target({ElementType.PARAMETER, ElementType.METHOD})
6+
@Retention(RetentionPolicy.RUNTIME)
7+
@Documented
8+
public @interface SystemServiceLog {
9+
10+
String description() default "";
11+
}
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
package cn.tellsea.aspect;
2+
3+
import cn.tellsea.annotation.SystemControllerLog;
4+
import cn.tellsea.annotation.SystemServiceLog;
5+
import cn.tellsea.utils.IPUtils;
6+
import com.alibaba.fastjson.JSONObject;
7+
import lombok.extern.slf4j.Slf4j;
8+
import org.aspectj.lang.JoinPoint;
9+
import org.aspectj.lang.annotation.AfterThrowing;
10+
import org.aspectj.lang.annotation.Aspect;
11+
import org.aspectj.lang.annotation.Before;
12+
import org.aspectj.lang.annotation.Pointcut;
13+
import org.springframework.stereotype.Component;
14+
import org.springframework.web.context.request.RequestContextHolder;
15+
import org.springframework.web.context.request.ServletRequestAttributes;
16+
17+
import javax.servlet.http.HttpServletRequest;
18+
import javax.servlet.http.HttpSession;
19+
import java.lang.reflect.Method;
20+
21+
@Slf4j
22+
@Aspect
23+
@Component
24+
@SuppressWarnings("all")
25+
public class SystemLogAspect {
26+
27+
//注入Service用于把日志保存数据库,实际项目入库采用队列做异步
28+
// @Resource
29+
// private ActionService actionService;
30+
31+
//Controller层切点
32+
@Pointcut("@annotation(cn.tellsea.annotation.SystemControllerLog)")
33+
public void controllerAspect() {
34+
}
35+
36+
//Service层切点
37+
@Pointcut("@annotation(cn.tellsea.annotation.SystemServiceLog)")
38+
public void serviceAspect() {
39+
}
40+
41+
/**
42+
* 前置通知 用于拦截Controller层记录用户的操作
43+
*
44+
* @param joinPoint
45+
*/
46+
@Before("controllerAspect()")
47+
public void doBefore(JoinPoint joinPoint) {
48+
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
49+
HttpSession session = request.getSession();
50+
//读取session中的用户
51+
// User user = (User) session.getAttribute("user");
52+
String ip = IPUtils.getIpAddr(request);
53+
try {
54+
//*========控制台输出=========*//
55+
System.out.println("==============前置通知开始==============");
56+
System.out.println("请求接口" + (joinPoint.getTarget().getClass().getName() + "." + joinPoint.getSignature().getName()));
57+
System.out.println("方法描述:" + getControllerMethodDescription(joinPoint));
58+
System.out.println("请求人:" + "test");
59+
System.out.println("请求ip:" + ip);
60+
61+
//*========数据库日志=========*//
62+
// Action action = new Action();
63+
// action.setActionDes(getControllerMethodDescription(joinPoint));
64+
// action.setActionType("0");
65+
// action.setActionIp(ip);
66+
// action.setUserId(user.getId());
67+
// action.setActionTime(new Date());
68+
// //保存数据库
69+
// actionService.add(action);
70+
71+
} catch (Exception e) {
72+
//记录本地异常日志
73+
log.error("==前置通知异常==");
74+
log.error("异常信息:{}", e.getMessage());
75+
}
76+
}
77+
78+
/**
79+
* 异常通知 用于拦截service层记录异常日志
80+
*
81+
* @param joinPoint
82+
* @param e
83+
*/
84+
@AfterThrowing(pointcut = "serviceAspect()", throwing = "e")
85+
public void doAfterThrowing(JoinPoint joinPoint, Throwable e) {
86+
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
87+
HttpSession session = request.getSession();
88+
//读取session中的用户
89+
// User user = (User) session.getAttribute("user");
90+
//获取请求ip
91+
String ip = IPUtils.getIpAddr(request);
92+
//获取用户请求方法的参数并序列化为JSON格式字符串
93+
String params = "";
94+
if (joinPoint.getArgs() != null && joinPoint.getArgs().length > 0) {
95+
for (int i = 0; i < joinPoint.getArgs().length; i++) {
96+
params += JSONObject.toJSON(joinPoint.getArgs()[i]) + ";";
97+
}
98+
}
99+
try {
100+
/*========控制台输出=========*/
101+
System.out.println("=====异常通知开始=====");
102+
System.out.println("异常代码:" + e.getClass().getName());
103+
System.out.println("异常信息:" + e.getMessage());
104+
System.out.println("异常方法:" + (joinPoint.getTarget().getClass().getName() + "." + joinPoint.getSignature().getName() + "()"));
105+
System.out.println("方法描述:" + getServiceMethodDescription(joinPoint));
106+
System.out.println("请求人:" + null);
107+
System.out.println("请求IP:" + ip);
108+
System.out.println("请求参数:" + params);
109+
/*==========数据库日志=========*/
110+
// Action action = new Action();
111+
// action.setActionDes(getServiceMethodDescription(joinPoint));
112+
// action.setActionType("1");
113+
// action.setUserId(user.getId());
114+
// action.setActionIp(ip);
115+
// action.setActionTime(new Date());
116+
// //保存到数据库
117+
// actionService.add(action);
118+
} catch (Exception ex) {
119+
//记录本地异常日志
120+
log.error("==异常通知异常==");
121+
log.error("异常信息:{}", ex.getMessage());
122+
}
123+
}
124+
125+
126+
/**
127+
* 获取注解中对方法的描述信息 用于service层注解
128+
*
129+
* @param joinPoint
130+
* @return
131+
* @throws Exception
132+
*/
133+
public static String getServiceMethodDescription(JoinPoint joinPoint) throws Exception {
134+
String targetName = joinPoint.getTarget().getClass().getName();
135+
String methodName = joinPoint.getSignature().getName();
136+
Object[] arguments = joinPoint.getArgs();
137+
Class targetClass = Class.forName(targetName);
138+
Method[] methods = targetClass.getMethods();
139+
String description = "";
140+
for (Method method : methods) {
141+
if (method.getName().equals(methodName)) {
142+
Class[] clazzs = method.getParameterTypes();
143+
if (clazzs.length == arguments.length) {
144+
description = method.getAnnotation(SystemServiceLog.class).description();
145+
break;
146+
}
147+
}
148+
}
149+
return description;
150+
}
151+
152+
/**
153+
* 获取注解中对方法的描述信息 用于Controller层注解
154+
*
155+
* @param joinPoint
156+
* @return
157+
* @throws Exception
158+
*/
159+
public static String getControllerMethodDescription(JoinPoint joinPoint) throws Exception {
160+
String targetName = joinPoint.getTarget().getClass().getName();
161+
String methodName = joinPoint.getSignature().getName();//目标方法名
162+
Object[] arguments = joinPoint.getArgs();
163+
Class targetClass = Class.forName(targetName);
164+
Method[] methods = targetClass.getMethods();
165+
String description = "";
166+
for (Method method : methods) {
167+
if (method.getName().equals(methodName)) {
168+
Class[] clazzs = method.getParameterTypes();
169+
if (clazzs.length == arguments.length) {
170+
description = method.getAnnotation(SystemControllerLog.class).description();
171+
break;
172+
}
173+
}
174+
}
175+
return description;
176+
}
177+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package cn.tellsea.service;
2+
3+
public interface UserService {
4+
5+
String test();
6+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package cn.tellsea.service.impl;
2+
3+
import cn.tellsea.annotation.SystemServiceLog;
4+
import cn.tellsea.service.UserService;
5+
import org.springframework.stereotype.Service;
6+
7+
@Service
8+
public class UserServiceImpl implements UserService {
9+
10+
@Override
11+
@SystemServiceLog(description = "测试service层日志打印")
12+
public String test() {
13+
return "测试service层日志打印";
14+
}
15+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package cn.tellsea.utils;
2+
3+
import javax.servlet.http.HttpServletRequest;
4+
5+
public class IPUtils {
6+
7+
private static final String UNKNOWN = "unknown";
8+
9+
/**
10+
* 通过request.getRemoteAddr()可以获取到客户端的IP地址
11+
* 但是若使用Nginx等反向代理软件就不能用其获取客户端IP了,
12+
* 比如将http://37.12.456/index反向代理为http://tycoding.cn/index,那么通过getRemoteAddr()获取的是代理服务器的地址:127.0.0.1、192.168.1.110、unknown
13+
* 但反向代理时会在HTTP Header中加入x-forwarded-for信息,用以跟踪原客户端IP地址
14+
* <p>
15+
* 如果通过多级反向代理,x-forwarded-for的值并不止一个,而是一串IP值,那么x-forwarded-for中第一个非unknown的字符串是IP
16+
*
17+
* @param request
18+
* @return
19+
*/
20+
public static String getIpAddr(HttpServletRequest request) {
21+
String ip = request.getHeader("x-forwarded-for");
22+
if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
23+
ip = request.getHeader("Proxy-Client-IP");
24+
}
25+
if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
26+
ip = request.getHeader("WL-Proxy-Client-IP");
27+
}
28+
if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
29+
ip = request.getRemoteAddr();
30+
}
31+
return "0:0:0:0:0:0:0:1".equals(ip) ? "127.0.0.1" : ip;
32+
}
33+
}

0 commit comments

Comments
 (0)