[Solutions] <RequestLogFilter> 请求日志统一拦截器

该代码定义了一个名为RequestLogFilter的Spring组件,用于记录HTTP请求的相关信息,包括请求ID(TID)、请求方法、路径、状态码、用户信息以及请求和响应的时间成本。如果请求处理时间超过配置的阈值,会特别记录警告信息。过滤器还会忽略健康检查URL,不对其进行日志记录。
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.Ordered;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;
import org.springframework.web.util.ContentCachingRequestWrapper;
import org.springframework.web.util.WebUtils;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.UUID;

@Component
@Slf4j
public class RequestLogFilter extends OncePerRequestFilter implements Ordered {
    private static final String TID = "TID";
    @Value(value = "${method.cost.time.threshold:1000}")
    private String methodCostTimeThreshold;

    private static final String HEALTH_CHECK_URL = "/api/health-check/status";

    @Override
    public int getOrder() {
        return 20;
    }

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
            FilterChain filterChain) throws ServletException, IOException {
        try {
            if(!request.getRequestURI().contains(HEALTH_CHECK_URL)) {
                long begin = System.currentTimeMillis();
                String requestId = UUID.randomUUID().toString().replace("-", "");
                MDC.put(TID, requestId);
                ContentCachingRequestWrapper wrappedRequest = new ContentCachingRequestWrapper(request);
                filterChain.doFilter(wrappedRequest, response);
                String body = getBody(wrappedRequest);
                String username = SecurityContextHolder.getContext().getAuthentication().getName();
                long end = System.currentTimeMillis();
                long cost = end - begin;
                if (StringUtils.isNotEmpty(methodCostTimeThreshold) && cost > Long.parseLong(methodCostTimeThreshold)) {
                    log.info("Method time cost exceeds threshold[{}],tid: [{}], method:[{}], path:[{}],status:[{}]", cost,
                            requestId, request.getMethod(), request.getRequestURI(), response.getStatus());
                }
                log.info("tid: {}, method: {}, path: {}, status: {}, user: {}, userAgent: {}, query: {}, body: {}",
                        requestId, request.getMethod(), request.getRequestURI(), response.getStatus(),
                        username, request.getHeader("User-Agent"), request.getQueryString(), body);
            }else{
                filterChain.doFilter(request, response);
            }
        } finally {
            if(!request.getRequestURI().contains(HEALTH_CHECK_URL)) {
                MDC.remove(TID);
            }
        }
    }

    private String getBody(ContentCachingRequestWrapper request) {
        ContentCachingRequestWrapper wrapper = WebUtils.getNativeRequest(request,
                ContentCachingRequestWrapper.class);
        if (wrapper != null) {
            byte[] buf = wrapper.getContentAsByteArray();
            if (buf.length > 0) {
                String payload;
                try {
                    payload = new String(buf, 0, buf.length, wrapper.getCharacterEncoding());
                } catch (UnsupportedEncodingException ex) {
                    payload = "[unknown]";
                }
                return payload;
            }
        }

        return null;
    }
}

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.3.0</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>io.github.xxyopen</groupId> <artifactId>novel</artifactId> <version>3.5.1-SNAPSHOT</version> <name>novel</name> <description>Spring Boot 3 + Vue 3 构建的前后端分离小说系统</description> <properties> <java.version>21</java.version> <mybatis-plus.version>3.5.6</mybatis-plus.version> <mybatis-plus-generator.version>3.5.1</mybatis-plus-generator.version> <jjwt.version>0.11.5</jjwt.version> <xxl-job.version>2.3.1</xxl-job.version> <sentinel.version>1.8.4</sentinel.version> <shardingsphere-jdbc.version>5.5.1</shardingsphere-jdbc.version> <redisson.version>3.19.1</redisson.version> <spring-boot-admin.version>3.0.0-M1</spring-boot-admin.version> <springdoc-openapi.version>2.5.0</springdoc-openapi.version> <logbook.version>3.9.0</logbook.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- mybatis-plus --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-spring-boot3-starter</artifactId> <version>${mybatis-plus.version}</version> </dependency> <!-- mybatis-plus 代码生成器 --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-generator</artifactId> <version>${mybatis-plus-generator.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.velocity</groupId> <artifactId>velocity-engine-core</artifactId> <version>2.3</version> <scope>test</scope> </dependency> <!-- 缓存相关 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>com.github.ben-manes.caffeine</groupId> <artifactId>caffeine</artifactId> </dependency> <!-- JWT 相关 --> <dependency> <groupId>io.jsonwebtoken</groupId> <artifactId>jjwt-api</artifactId> <version>${jjwt.version}</version> </dependency> <dependency> <groupId>io.jsonwebtoken</groupId> <artifactId>jjwt-impl</artifactId> <version>${jjwt.version}</version> <scope>runtime</scope> </dependency> <dependency> <groupId>io.jsonwebtoken</groupId> <artifactId>jjwt-jackson</artifactId> <version>${jjwt.version}</version> <scope>runtime</scope> </dependency> <!-- 请求参数校验相关 --> <dependency> <groupId>org.hibernate.validator</groupId> <artifactId>hibernate-validator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> </dependency> <!-- MQ 相关 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency> <!-- XXL-JOB 相关 --> <dependency> <groupId>com.xuxueli</groupId> <artifactId>xxl-job-core</artifactId> <version>${xxl-job.version}</version> </dependency> <!-- sentinel 相关 --> <dependency> <groupId>com.alibaba.csp</groupId> <artifactId>sentinel-core</artifactId> <version>${sentinel.version}</version> </dependency> <dependency> <groupId>com.alibaba.csp</groupId> <artifactId>sentinel-parameter-flow-control</artifactId> <version>${sentinel.version}</version> </dependency> <!-- ShardingSphere-JDBC --> <dependency> <groupId>org.apache.shardingsphere</groupId> <artifactId>shardingsphere-jdbc</artifactId> <version>${shardingsphere-jdbc.version}</version> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>runtime</scope> </dependency> <!-- Spring Boot 管理和监控 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-client</artifactId> <version>${spring-boot-admin.version}</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <!-- Redisson 相关 --> <dependency> <groupId>org.redisson</groupId> <artifactId>redisson-spring-boot-starter</artifactId> <version>${redisson.version}</version> </dependency> <!-- Aop 相关 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency> <!-- OpenAPI 3 --> <dependency> <groupId>org.springdoc</groupId> <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId> <version>${springdoc-openapi.version}</version> </dependency> <!-- 邮件 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> <!-- AI --> <dependency> <groupId>org.springframework.ai</groupId> <artifactId>spring-ai-openai-spring-boot-starter</artifactId> </dependency> <dependency> <groupId>com.mysql</groupId> <artifactId>mysql-connector-j</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.flywaydb</groupId> <artifactId>flyway-core</artifactId> </dependency> <dependency> <groupId>org.flywaydb</groupId> <artifactId>flyway-mysql</artifactId> </dependency> <dependency> <groupId>org.zalando</groupId> <artifactId>logbook-spring-boot-starter</artifactId> <version>${logbook.version}</version> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.ai</groupId> <artifactId>spring-ai-bom</artifactId> <version>1.0.0-M6</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <excludes> <exclude> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </exclude> </excludes> </configuration> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <!--指定 Java 编译器的 -source 参数 --> <source>${java.version}</source> <!--指定 Java 编译器的 -target 参数 --> <target>${java.version}</target> </configuration> </plugin> </plugins> </build> <repositories> <repository> <id>aliyun</id> <url>https://maven.aliyun.com/repository/public</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>aliyun</id> <url>https://maven.aliyun.com/repository/public</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </pluginRepository> </pluginRepositories> </project> [ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:3.3.0:run (default-cli) on project novel: Process terminated with exit code: 1 -> [Help 1] [ERROR] [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch. [ERROR] Re-run Maven using the -X switch to enable full debug logging. [ERROR] [ERROR] For more information about the errors and possible solutions, please read the following articles: [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
最新发布
06-17
### 解决 Maven 构建过程中 Spring Boot 项目运行失败的问题 当使用 Maven 构建 Spring Boot 项目时,如果 `spring-boot-maven-plugin` 插件执行失败并抛出 `MojoExecutionException` 错误,这通常与以下因素相关:POM 文件配置问题、依赖冲突或资源文件路径错误。 #### POM 文件配置检查 确保 `pom.xml` 文件中 `spring-boot-maven-plugin` 的版本与 Spring Boot 版本兼容。以下是正确的插件配置示例[^1]: ```xml <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>3.3.0</version> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> ``` 此外,还需验证 `maven-compiler-plugin` 和 `maven-resources-plugin` 的配置是否正确。例如,确保编译器插件的源码和目标版本匹配当前 JDK 版本[^2]: ```xml <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <source>17</source> <target>17</target> <encoding>UTF-8</encoding> </configuration> </plugin> ``` #### 依赖冲突排查 依赖冲突可能导致构建失败。通过以下命令检查依赖树并定位冲突: ```bash mvn dependency:tree ``` 如果发现冲突,可以使用 `<dependencyManagement>` 部分显式指定依赖版本,避免版本不一致问题[^3]。例如: ```xml <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>3.3.0</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> ``` #### 资源文件路径问题 如果项目中的资源文件路径存在非法字符或编码问题,可能会导致构建失败。确保 `maven-resources-plugin` 的编码配置正确,并清理可能存在的 BOM 字符(如 `\ufeff`)[^4]。例如: ```xml <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>3.2.0</version> <configuration> <encoding>UTF-8</encoding> </configuration> </plugin> ``` #### 清理与重新构建 在修改配置后,建议清理项目并重新构建以确保更改生效: ```bash mvn clean install ``` --- ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值