This commit is contained in:
Jane
2023-12-22 10:59:10 +08:00
parent 751c43e199
commit d1ede2d4aa
2774 changed files with 291509 additions and 0 deletions

View File

@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>common</artifactId>
<groupId>com.platform</groupId>
<version>0.4.x</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<version>0.4.x</version>
<artifactId>common-log</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>com.platform</groupId>
<artifactId>common-service-api</artifactId>
<version>0.4.x</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>5.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>com.platform</groupId>
<artifactId>generic</artifactId>
<version>0.4.x</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,20 @@
package cn.datax.common.log.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ ElementType.PARAMETER, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface LogAop {
/** 模块 */
String module() default "";
/** 描述 */
String value() default "";
}

View File

@@ -0,0 +1,134 @@
package cn.datax.common.log.annotation;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import com.platform.utils.RequestHolder;
import cn.datax.service.system.api.dto.LogDto;
import cn.hutool.core.util.URLUtil;
import cn.hutool.extra.servlet.ServletUtil;
import cn.hutool.http.useragent.UserAgent;
import cn.hutool.http.useragent.UserAgentUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.platform.utils.SecurityUtil;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
@Slf4j
@Aspect
public class LogAspect {
// @Autowired
// private LogServiceFeign logServiceFeign;
@Autowired
private ObjectMapper objectMapper;
// 配置织入点
@Pointcut("@annotation(cn.datax.common.log.annotation.LogAop)")
public void logPointCut() {}
/**
* 通知方法会将目标方法封装起来
*
* @param joinPoint 切点
*/
@Around(value = "logPointCut()")
public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
long startTime = System.currentTimeMillis();
Object result = joinPoint.proceed();
long endTime = System.currentTimeMillis();
LogDto logDto = getLog();
logDto.setTime(String.valueOf(endTime - startTime));
handleLog(joinPoint, logDto);
return result;
}
/**
* 通知方法会在目标方法抛出异常后执行
*
* @param joinPoint
* @param e
*/
@AfterThrowing(value = "logPointCut()", throwing = "e")
public void doAfterThrowing(JoinPoint joinPoint, Exception e) {
LogDto logDto = getLog();
logDto.setExCode(e.getClass().getSimpleName()).setExMsg(e.getMessage());
handleLog(joinPoint, logDto);
}
private LogDto getLog(){
LogDto log = new LogDto();
HttpServletRequest request = RequestHolder.getHttpServletRequest();
log.setUserId(SecurityUtil.getUserId());
log.setUserName(SecurityUtil.getUserName());
log.setRemoteAddr(ServletUtil.getClientIP(request));
log.setRequestUri(URLUtil.getPath(request.getRequestURI()));
UserAgent ua = UserAgentUtil.parse(request.getHeader("User-Agent"));
log.setBrowser(ua.getBrowser().toString());
log.setOs(ua.getOs().toString());
return log;
}
protected void handleLog(final JoinPoint joinPoint, LogDto logDto) {
// 获得注解
LogAop logAop = getAnnotationLog(joinPoint);
if(null == logAop) {
return;
}
// 设置方法名称
String className = joinPoint.getTarget().getClass().getName();
String methodName = joinPoint.getSignature().getName();
logDto.setModule(logAop.module()).setTitle(logAop.value())
.setClassName(className).setMethodName(methodName);
try {
logDto.setParams(objectMapper.writeValueAsString(getRequestParams(joinPoint)));
} catch (JsonProcessingException e) {}
// 保存数据库
// logServiceFeign.saveLog(logDto);
}
/**
* 是否存在注解,如果存在就获取
*/
private LogAop getAnnotationLog(JoinPoint joinPoint) {
Signature signature = joinPoint.getSignature();
MethodSignature methodSignature = (MethodSignature) signature;
Method method = methodSignature.getMethod();
return null;
}
/**
* 获取入参
* */
private Map<String, Object> getRequestParams(JoinPoint joinPoint) {
Map<String, Object> requestParams = new HashMap<>();
// 参数名
String[] paramNames = ((MethodSignature)joinPoint.getSignature()).getParameterNames();
// 参数值
Object[] paramValues = joinPoint.getArgs();
for (int i = 0; i < paramNames.length; i++) {
Object value = paramValues[i];
// 如果是文件对象
if (value instanceof MultipartFile) {
MultipartFile file = (MultipartFile) value;
// 获取文件名
value = file.getOriginalFilename();
}
requestParams.put(paramNames[i], value);
}
return requestParams;
}
}

View File

@@ -0,0 +1,26 @@
package com.platform.exception;
import lombok.Getter;
import org.springframework.http.HttpStatus;
import static org.springframework.http.HttpStatus.BAD_REQUEST;
/**
* @author AllDataDC
* @date 2023-01-27
* 统一异常处理
*/
@Getter
public class BadRequestException extends RuntimeException{
private Integer status = BAD_REQUEST.value();
public BadRequestException(String msg){
super(msg);
}
public BadRequestException(HttpStatus status,String msg){
super(msg);
this.status = status.value();
}
}

View File

@@ -0,0 +1,19 @@
package com.platform.utils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.util.Objects;
/**
* 获取 HttpServletRequest
* @author AllDataDC
* @date 2023-01-27
*/
public class RequestHolder {
public static HttpServletRequest getHttpServletRequest() {
return ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
}
}

View File

@@ -0,0 +1,125 @@
package com.platform.utils;
import com.platform.exception.BadRequestException;
import cn.datax.service.system.api.dto.JwtUserDto;
import cn.datax.service.system.api.feign.UserServiceFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
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.ArrayList;
import java.util.List;
@Component
public class SecurityUtil {
@Autowired
private UserServiceFeign userServiceFeign;
@Autowired
private JwtUtil jwtUtil;
/**
* 获取用户
*
* @return user
*/
public static JwtUserDto getDataUser() {
UserServiceFeign userServiceFeign = SpringContextHolder.getBean(UserServiceFeign.class);
return userServiceFeign.loginByUsername(getCurrentUsername());
}
public static String getCurrentUsername() {
HttpServletRequest request =((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
String authorization = request.getHeader("Authorization");
String tokenSubjectObject = String.valueOf(JwtUtil.getTokenSubjectObject(authorization));
if (tokenSubjectObject == null) {
throw new BadRequestException(HttpStatus.UNAUTHORIZED, "当前登录状态过期");
}
return tokenSubjectObject;
}
/**
* 获取用户ID
*
* @return id
*/
public static String getUserId() {
JwtUserDto user = getDataUser();
if (user != null){
return user.getUser().getId()+"";
}
return "";
}
/**
* 获取用户部门
*
* @return id
*/
public static String getUserDeptId() {
JwtUserDto user = getDataUser();
if (user != null){
return user.getUser().getDeptId()+"";
}
return "";
}
/**
* 获取用户名称
*
* @return username
*/
public static String getUserName() {
JwtUserDto user = getDataUser();
if (user != null){
return user.getUsername();
}
return "";
}
/**
* 获取用户昵称
*
* @return nickname
*/
public static String getNickname() {
JwtUserDto user = getDataUser();
if (user != null){
return user.getUser().getNickName();
}
return "";
}
/**
* 获取用户角色
*
* @return username
*/
public static List<String> getUserRoleIds() {
JwtUserDto user = getDataUser();
if (user != null){
List<String> roles = new ArrayList<>(user.getRoles());
return roles;
}
return null;
}
/**
* 获取用户
*
* @return user
*/
public static boolean isAdmin() {
JwtUserDto user = getDataUser();
if (user != null){
return user.getUser().getIsAdmin();
}
return false;
}
}

View File

@@ -0,0 +1,2 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
cn.datax.common.log.annotation.LogAspect