2.0
WebMvcConfigurer
实现WebMvcConfigurer中的一些方法来配置springboot
@Component
public class LogInterceptor implements HandlerInterceptor {
static Logger logger = LoggerFactory.getLogger(LogInterceptor.class);
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
logger.info("请求路径:{}", request.getRequestURI());
return true;
}
}
@Configuration
public class WebMvc implements WebMvcConfigurer {
@Autowired
private LogInterceptor logInterceptor;
/**
* 重写添加拦截器方法并添加配置拦截器
* @param registry
*/
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(logInterceptor).addPathPatterns("/**");
}
}