2024-01-17 09:44:29 +08:00
|
|
|
|
package com.realtime.protection.configuration.response;
|
|
|
|
|
|
|
|
|
|
|
|
import com.realtime.protection.ProtectionApplication;
|
|
|
|
|
|
import com.realtime.protection.configuration.entity.user.User;
|
2024-06-05 19:12:09 +08:00
|
|
|
|
import com.realtime.protection.configuration.entity.user.UserFull;
|
|
|
|
|
|
import jakarta.servlet.http.HttpSession;
|
2024-01-17 09:44:29 +08:00
|
|
|
|
import lombok.AllArgsConstructor;
|
|
|
|
|
|
import lombok.Data;
|
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
2024-06-05 19:12:09 +08:00
|
|
|
|
import org.jetbrains.annotations.NotNull;
|
2024-01-17 09:44:29 +08:00
|
|
|
|
import org.springframework.core.MethodParameter;
|
|
|
|
|
|
import org.springframework.http.HttpStatus;
|
|
|
|
|
|
import org.springframework.http.MediaType;
|
|
|
|
|
|
import org.springframework.http.converter.HttpMessageConverter;
|
|
|
|
|
|
import org.springframework.http.server.ServerHttpRequest;
|
|
|
|
|
|
import org.springframework.http.server.ServerHttpResponse;
|
2024-06-05 19:12:09 +08:00
|
|
|
|
import org.springframework.http.server.ServletServerHttpRequest;
|
|
|
|
|
|
import org.springframework.web.bind.annotation.ControllerAdvice;
|
2024-01-17 09:44:29 +08:00
|
|
|
|
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
|
|
|
|
|
import org.springframework.web.reactive.function.client.WebClient;
|
|
|
|
|
|
import org.springframework.web.reactive.function.client.WebClientRequestException;
|
|
|
|
|
|
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
|
|
|
|
|
|
import reactor.core.publisher.Mono;
|
2024-06-22 18:15:02 +08:00
|
|
|
|
|
|
|
|
|
|
import java.util.Enumeration;
|
|
|
|
|
|
|
2024-06-05 19:12:09 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 修改人: Fulian Li
|
|
|
|
|
|
* 功能:执行日志审计
|
|
|
|
|
|
**/
|
2024-01-17 09:44:29 +08:00
|
|
|
|
|
|
|
|
|
|
@RestControllerAdvice(basePackageClasses = {ProtectionApplication.class})
|
|
|
|
|
|
@Slf4j
|
2024-06-05 19:12:09 +08:00
|
|
|
|
@ControllerAdvice
|
2024-01-17 09:44:29 +08:00
|
|
|
|
public class AuditAdvice implements ResponseBodyAdvice<ResponseResult> {
|
|
|
|
|
|
|
|
|
|
|
|
private final WebClient webClient = WebClient
|
|
|
|
|
|
.builder()
|
|
|
|
|
|
.baseUrl("http://39.105.210.156:8090/chanct-log/audit-xgs")
|
2024-06-11 00:39:28 +08:00
|
|
|
|
// .baseUrl("http://10.58.44.241:1888/api/chanct-log/audit-xgs")
|
2024-01-17 09:44:29 +08:00
|
|
|
|
.build();
|
|
|
|
|
|
|
2024-06-18 22:52:37 +08:00
|
|
|
|
@Data
|
|
|
|
|
|
@AllArgsConstructor
|
|
|
|
|
|
private static class AuditRes{
|
|
|
|
|
|
private AuditData auditBase;
|
|
|
|
|
|
}
|
2024-01-17 09:44:29 +08:00
|
|
|
|
@Data
|
|
|
|
|
|
@AllArgsConstructor
|
|
|
|
|
|
private static class AuditData {
|
2024-06-05 19:12:09 +08:00
|
|
|
|
private String userId;
|
|
|
|
|
|
private String deptId;
|
2024-01-17 09:44:29 +08:00
|
|
|
|
private String userName;
|
|
|
|
|
|
private String deptName;
|
|
|
|
|
|
private String menu;
|
|
|
|
|
|
private String action;
|
|
|
|
|
|
private String res;
|
|
|
|
|
|
private String content;
|
2024-06-18 22:52:37 +08:00
|
|
|
|
|
|
|
|
|
|
private String userIp;
|
2024-01-17 09:44:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
|
2024-06-22 18:15:02 +08:00
|
|
|
|
// return false;
|
|
|
|
|
|
return true;
|
2024-01-17 09:44:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
2024-06-05 19:12:09 +08:00
|
|
|
|
public ResponseResult beforeBodyWrite(ResponseResult body, MethodParameter returnType, MediaType selectedContentType, Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
|
2024-06-22 18:15:02 +08:00
|
|
|
|
// 可以不发送query的请求,数据量太大
|
2024-06-18 22:52:37 +08:00
|
|
|
|
if (request.getURI().getPath().contains("query")){
|
|
|
|
|
|
return body;
|
|
|
|
|
|
}
|
2024-06-05 19:12:09 +08:00
|
|
|
|
AuditData auditData = getAuditData(body, request);
|
2024-06-18 22:52:37 +08:00
|
|
|
|
|
|
|
|
|
|
AuditRes auditRes = new AuditRes(auditData);
|
2024-06-06 12:56:33 +08:00
|
|
|
|
log.info("auditData-----------:"+auditData);
|
2024-01-17 09:44:29 +08:00
|
|
|
|
|
|
|
|
|
|
Mono<String> mono = webClient
|
|
|
|
|
|
.post()
|
|
|
|
|
|
.uri("/save")
|
2024-06-18 22:52:37 +08:00
|
|
|
|
.bodyValue(auditRes)
|
2024-01-17 09:44:29 +08:00
|
|
|
|
.exchangeToMono(res -> {
|
|
|
|
|
|
if (res.statusCode().equals(HttpStatus.OK)) {
|
2024-06-06 12:56:33 +08:00
|
|
|
|
log.info("发送审计日志成功:{}",res.statusCode());
|
2024-01-17 09:44:29 +08:00
|
|
|
|
return res.bodyToMono(String.class);
|
|
|
|
|
|
}
|
2024-06-06 12:56:33 +08:00
|
|
|
|
log.info("发送审计日志失败:{}",res.statusCode());
|
2024-01-17 09:44:29 +08:00
|
|
|
|
return null;
|
|
|
|
|
|
})
|
|
|
|
|
|
.doOnError(WebClientRequestException.class, err ->
|
2024-04-22 15:07:49 +08:00
|
|
|
|
log.warn("审计服务器遭遇异常{}", err.getMessage()));
|
2024-01-17 09:44:29 +08:00
|
|
|
|
|
|
|
|
|
|
mono.subscribe(AuditAdvice::handleMono);
|
|
|
|
|
|
|
|
|
|
|
|
return body;
|
|
|
|
|
|
}
|
2024-06-22 18:15:02 +08:00
|
|
|
|
// 解析 X-Forwarded-For 头中的第一个 IP 地址
|
|
|
|
|
|
private static String extractFirstIpAddress(String xForwardedForHeader) {
|
|
|
|
|
|
if (xForwardedForHeader != null) {
|
|
|
|
|
|
// 根据逗号分隔获取第一个 IP 地址
|
|
|
|
|
|
String[] ips = xForwardedForHeader.trim().split("\\s*,\\s*");
|
|
|
|
|
|
return ips[0];
|
|
|
|
|
|
}
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
2024-06-05 19:12:09 +08:00
|
|
|
|
@NotNull
|
|
|
|
|
|
private static AuditData getAuditData(ResponseResult body, ServerHttpRequest request) {
|
|
|
|
|
|
HttpSession session = ((ServletServerHttpRequest) request).getServletRequest().getSession();
|
|
|
|
|
|
AuditData auditData;
|
2024-06-18 22:52:37 +08:00
|
|
|
|
if(session==null || session.getAttribute("user")==null){
|
2024-06-05 19:12:09 +08:00
|
|
|
|
auditData = new AuditData(
|
2024-06-21 16:31:56 +08:00
|
|
|
|
"0000000","0000000","NSADD管理员","组织树",
|
2024-06-05 19:12:09 +08:00
|
|
|
|
request.getURI().getPath(),
|
|
|
|
|
|
request.getMethod().toString(),
|
|
|
|
|
|
body.getCode()==200?"成功":"失败",
|
2024-06-18 22:52:37 +08:00
|
|
|
|
body.getData().toString(),
|
2024-06-22 18:15:02 +08:00
|
|
|
|
extractFirstIpAddress(request.getHeaders().getFirst("X-Forwarded-For"))
|
2024-06-05 19:12:09 +08:00
|
|
|
|
);
|
|
|
|
|
|
}else {
|
|
|
|
|
|
UserFull user = (UserFull) session.getAttribute("user");
|
|
|
|
|
|
auditData = new AuditData(
|
|
|
|
|
|
user.uid, user.getOrgCode(),user.name, user.getOrgName(),
|
|
|
|
|
|
request.getURI().getPath(),
|
|
|
|
|
|
request.getMethod().toString(),
|
|
|
|
|
|
body.getCode()==200?"成功":"失败",
|
2024-06-18 22:52:37 +08:00
|
|
|
|
body.getData().toString(),
|
2024-06-22 18:15:02 +08:00
|
|
|
|
extractFirstIpAddress(request.getHeaders().getFirst("X-Forwarded-For"))
|
2024-06-05 19:12:09 +08:00
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
return auditData;
|
2024-01-17 09:44:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static void handleMono(String result) {
|
2024-06-18 22:52:37 +08:00
|
|
|
|
log.info("审计服务器返回结果:" + result);
|
2024-01-17 09:44:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|