当前位置: 首页> 健康> 母婴 > seo推广培训课程_辽宁建设工程信息网项目经理解锁_重庆网站推广联系方式_国际军事新闻最新消息今天

seo推广培训课程_辽宁建设工程信息网项目经理解锁_重庆网站推广联系方式_国际军事新闻最新消息今天

时间:2025/7/9 1:31:16来源:https://blog.csdn.net/qq_37069064/article/details/145066860 浏览次数:0次
seo推广培训课程_辽宁建设工程信息网项目经理解锁_重庆网站推广联系方式_国际军事新闻最新消息今天

1.首先创建拦截器类:


package com.example.interceptor;import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;@Slf4j
@Component
public class RequestInterceptor implements HandlerInterceptor {@Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {String requestURI = request.getRequestURI();String method = request.getMethod();log.info("请求开始,URI: {}, Method: {}", requestURI, method);// 获取请求头中的token(如果有)String token = request.getHeader("Authorization");if (token != null) {log.info("Token: {}", token);}// 返回true表示继续执行,返回false表示中断请求return true;}@Overridepublic void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {log.info("请求处理完成");}@Overridepublic void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {log.info("请求完全结束");if (ex != null) {log.error("请求过程中发生错误:", ex);}}
}

2. 创建拦截器配置类:

package com.example.config;import com.example.interceptor.RequestInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;@Configuration
public class WebMvcConfig implements WebMvcConfigurer {private final RequestInterceptor requestInterceptor;public WebMvcConfig(RequestInterceptor requestInterceptor) {this.requestInterceptor = requestInterceptor;}@Overridepublic void addInterceptors(InterceptorRegistry registry) {registry.addInterceptor(requestInterceptor).addPathPatterns("/**")         // 拦截所有请求.excludePathPatterns(           // 排除不需要拦截的路径"/swagger-ui/**",           // swagger"/v3/api-docs/**",          // swagger"/error",                   // 错误页面"/static/**",               // 静态资源"/*.ico"                    // 图标文件);}
}

3.创建一个测试控制器:

package com.example.controller;import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@Slf4j
@RestController
@RequestMapping("/api")
public class TestController {@GetMapping("/test")public String test() {log.info("测试接口被调用");return "Hello from test endpoint!";}
}

关键字:seo推广培训课程_辽宁建设工程信息网项目经理解锁_重庆网站推广联系方式_国际军事新闻最新消息今天

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: