package org.springblade.waterModel.interceptor;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Enumeration;
@Component
public class HeaderInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("请求URL: " + request.getRequestURL().toString());
System.out.println("请求方法: " + request.getMethod());
System.out.println("请求头信息: ");
Enumeration<String> headerNames = request.getHeaderNames();
while (headerNames.hasMoreElements()) {
String headerName = headerNames.nextElement();
System.out.println(headerName + ": " + request.getHeader(headerName));
}
return true;
}
}
必须在配置上注册这个拦截器
@EnableAsync
@Configuration
@MapperScan({"org.springblade.**.mapper.**"})
public class ApiConfiguration extends WebMvcConfigurerAdapter {
@Bean
public AuthInterceptor authInterceptor() {
return new AuthInterceptor();
}
@Resource
private HeaderInterceptor headerInterceptor;
// 给所有html的请求带上token的时候再拦截
// @Override
// public void addInterceptors(InterceptorRegistry registry) {
// registry.addInterceptor(authInterceptor())
// .addPathPatterns("/index/**");
.excludePathPatterns("/**/v1/auth/login","/**/v1/device/connect");
// }
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(headerInterceptor);
}