春季开机添加http请求拦截器

在spring引导应用程序中添加HttpRequest拦截器的正确方法是什么? 我想要做的是logging每个http请求的请求和响应。

Spring启动文档根本不涵盖这个主题。 ( http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/ )

我发现了一些关于如何在老版本的spring上做同样的web示例,但是那些与applicationcontext.xml一起工作。 请帮忙。

由于您使用的是Spring Boot,因此我认为您应该尽可能依靠Spring的自动configuration。 要添加像拦截器一样的其他自定义configuration,只需提供WebMvcConfigurerAdapter的configuration或bean WebMvcConfigurerAdapter

这是一个configuration类的例子:

 @Configuration public class WebMvcConfig extends WebMvcConfigurerAdapter { @Autowired HandlerInterceptor yourInjectedInterceptor; @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(...) ... registry.addInterceptor(getYourInterceptor()); registry.addInterceptor(yourInjectedInceptor); // next two should be avoid -- tightly coupled and not very testable registry.addInterceptor(new YourInceptor()); registry.addInterceptor(new HandlerInterceptor() { ... }); } } 

注意不要用@EnableWebMvc注释这个,如果你想保留mvc的Spring Boots自动configuration 。

WebMvcConfigurerAdapter将在Spring 5中被弃用。从它的Javadoc :

@deprecated从5.0 {@link WebMvcConfigurer}有默认的方法(由Java 8的基线可能),可以直接实现,而不需要这个适配器

如上所述,您应该做的是实现WebMvcConfigurer并重写addInterceptors方法。

 @Configuration public class WebMvcConfig implements WebMvcConfigurer { @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new MyCustomInterceptor()); } } 

要将拦截器添加到弹簧引导应用程序,请执行以下操作

  1. 创build一个拦截器类

     public class MyCustomInterceptor implements HandlerInterceptor{ //unimplemented methods comes here. Define the following method so that it //will handle the request before it is passed to the controller. @Override public boolean preHandle(HttpServletRequest request,HttpServletResponse response){ //your custom logic here. return true; } } 
  2. 定义一个configuration类

     @Configuration public class MyConfig extends WebMvcConfigurerAdapter{ @Override public void addInterceptors(InterceptorRegistry registry){ registry.addInterceptor(new MyCustomInterceptor()).addPathPatterns("/**"); } } 
  3. 而已。 现在,所有请求都将通过MyCustomInterceptor的preHandle()方法定义的逻辑。

你也可以考虑添加一个SpringSandwich依赖关系,它允许你直接在你的控制器中注释应用哪个拦截器,就像你注释你的url路由一样(披露:我是作者)。

https://github.com/arikast/springsandwich

我有相同的问题WebMvcConfigurerAdapter被弃用。 当我search的例子,我几乎没有发现任何实施的代码。 这是一段工作代码。

创build一个扩展HandlerInterceptorAdapter的类

 import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; import me.rajnarayanan.datatest.DataTestApplication; @Component public class EmployeeInterceptor extends HandlerInterceptorAdapter { private static final Logger logger = LoggerFactory.getLogger(DataTestApplication.class); @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { String x = request.getMethod(); logger.info(x + "intercepted"); return true; } } 

然后实现WebMvcConfigurer接口

 import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import me.rajnarayanan.datatest.interceptor.EmployeeInterceptor; @Configuration public class WebMvcConfig implements WebMvcConfigurer { @Autowired EmployeeInterceptor employeeInterceptor ; @Override public void addInterceptors(InterceptorRegistry registry){ registry.addInterceptor(employeeInterceptor).addPathPatterns("/employee"); } }