-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
270 additions
and
97 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.ligg.anno; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(ElementType.METHOD) | ||
public @interface Log { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package com.ligg.aop; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.ligg.mapper.OperateLogMapper; | ||
import com.ligg.pojo.OperateLog; | ||
import com.ligg.utils.JwtUtil; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import lombok.extern.slf4j.Slf4j; | ||
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.stereotype.Component; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.Arrays; | ||
import java.util.Map; | ||
|
||
@Slf4j | ||
@Component | ||
@Aspect | ||
public class LogAspect { | ||
|
||
@Autowired | ||
private OperateLogMapper operateLogMapper; | ||
@Autowired | ||
private HttpServletRequest request; | ||
@Autowired | ||
ObjectMapper objectMapper; | ||
|
||
@Around("@annotation(com.ligg.anno.Log)") | ||
public Object recordLog(ProceedingJoinPoint pjp) throws Throwable { | ||
|
||
//操作人ID | ||
String token = request.getHeader("Authorization"); | ||
if (token == null){ | ||
return null; | ||
} | ||
Map<String, Object> user = JwtUtil.parseTokenWithValidation(token); | ||
Long operateUserId = (Long) user.get("id"); | ||
|
||
//操作时间 | ||
LocalDateTime operateTime = LocalDateTime.now(); | ||
|
||
//操作类名 | ||
String className = pjp.getTarget().getClass().getName(); | ||
|
||
//操作方法名 | ||
String methodName = pjp.getSignature().getName(); | ||
|
||
//操作方法参数 | ||
Object[] args = pjp.getArgs(); | ||
String methodParams = Arrays.toString(args); | ||
|
||
long begin = System.currentTimeMillis(); | ||
//调用原始目标方法运行 | ||
Object result = pjp.proceed(); | ||
long end = System.currentTimeMillis(); | ||
|
||
//方法返回值 | ||
String returnValue = objectMapper.writeValueAsString(result); | ||
//操作耗时 | ||
Long costTime = end - begin; | ||
|
||
|
||
//记录日志 | ||
OperateLog operateLog = new OperateLog(null,operateUserId,operateTime,className,methodName,methodParams,returnValue,costTime); | ||
operateLogMapper.insertLog(operateLog); | ||
log.info("AOP操作日志:[操作人ID:{},操作时间:{},操作类名:{},操作方法名:{},操作方法参数:{},操作耗时:{}]",operateUserId,operateTime,className,methodName,methodParams,costTime); | ||
return result; | ||
} | ||
|
||
public static void main(String[] args) { | ||
Map<String, Object> map = JwtUtil.parseTokenWithValidation("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJjbGFpbXMiOnsiaWQiOjI0NTAyODQ3ODYsInVzZXJuYW1lIjoibGlnZzIwMDMwOSJ9LCJleHAiOjE3MzkyNjIyMzl9.DdDdaMtDOoAHnyQypFAFBVBVJEqaDB7vY-qRzDJ2Lp0"); | ||
System.out.println(map); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.ligg.config; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.web.servlet.config.annotation.CorsRegistry; | ||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | ||
|
||
public class CorsConfig { | ||
@Bean | ||
public WebMvcConfigurer corsConfigurer() { | ||
return new WebMvcConfigurer() { | ||
@Override | ||
public void addCorsMappings(CorsRegistry registry) { | ||
registry.addMapping("/**") | ||
.allowedOrigins("*") // 或指定具体域名 | ||
.allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS") | ||
.allowCredentials(true); | ||
} | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.