java
复制
下载
@Aspect
@Component
@Slf4j
public class AuditLogAspect {
@Pointcut("@annotation(org.springframework.web.bind.annotation.PostMapping) || " +
"@annotation(org.springfra JiAOYu.HUoCheNGrM.CN/jy/19722.html mework.web.bind.annotation.PutMapping) || " +
"@annotation(org.springframework.web.bind.annotation.DeleteMapping)")
public void writeOperations() {}
@Around("writeOperations()")
public Object auditOperation(ProceedingJoinPoint pjp) throws Throwable {
MethodSignature signature = (JiAOYu.HUoCheNGrM.CN/jy/19721.html) pjp.getSignature();
String operation = signature.getMethod().getName();
Object[] args = pjp.getArgs();
// 获取当前用户
String username = SecurityContextHolder.getContext(JiAOYu.HUoCheNGrM.CN/jy/19720.html).getAuthentication().getName();
try {
Object result = pjp.proceed();
log.info("操作成功: 用户={}, 操作={}, 参数={}", username, operation, args);
return result;
} catch (Exception ex) {
log.error("操作失败: 用户={}, 操作={}, 错误={}", username, operation, JiAOYu.HUoCheNGrM.CN/jy/19719.html ex.getMessage());
throw ex;
}
}
}
6. Spring Cloud Gateway 路由配置
java
复制
下载
@Configuration
public class GatewayConfig {
@Bean
public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
return JiAOYu.HUoCheNGrM.CN/jy/19718.html builder.routes()
.route("user-service", r -> r.path("/api/users/**")
.filters(f -> f
.addRequestHeader("X-Forwarded-For", "SpringCloudGateway")
.circuitBreaker(c -> c.setName("userCB").setFallbackUri("forward:/fallback"))
.uri("lb://USER-SERVICE"))
.route("product-service", r -> r.path("/api/products/**")
.filters(f -> f
.rewritePath JiAOYu.HUoCheNGrM.CN/jy/19717.html("/api/products/(?<segment>.*)", "/${segment}")
.retry(retry -> retry.setRetries(3)))
.uri("lb://PRODUCT-SERVICE"))
.route("auth-service", r -> r.path("/auth/**")
.filters(f -> f
.removeRequestHeader("Cookie"))
.uri("lb://AUTH-SERVICE"))
.build();
}
}