英文:
How to register an Interceptor in Spring Boot 3 and Spring Framework 6
问题
我正在使用 Spring Boot 3.1.0-SNAPSHOT 构建后端,使用了 Spring Framework 6x。
拦截器:
@Slf4j
public class MyInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        log.info("preHandle");
        return true;
    }
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        log.info("afterCompletion");
    }
}
在之前的版本(Spring Boot 2)中,添加拦截器的方式是:
@Configuration
public class AppConfig implements WebMvcConfigurer {
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new MyInterceptor());
    }
}
现在,正确的方式是:
@Configuration
public class AppConfig {
    // 现在不起作用
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new MyInterceptor()).addPathPatterns("/api/**");
    }
}
如何添加一个拦截器呢?
我尝试过:
@Configuration
public class AppConfig {
    @Bean
    public InterceptorRegistry interceptorRegistry() {
        InterceptorRegistry registry = new InterceptorRegistry();
        registry.addInterceptor(new MyInterceptor());
        return registry;
    }
}
但是不起作用...
一个可行的示例是使用 MappedInterceptor:
@Configuration
public class AppConfig {
    @Bean
    public MappedInterceptor loginInter() {
        return new MappedInterceptor(null, new MyInterceptor());
    }
}
但根据这里的其他讨论串,比如这个 https://stackoverflow.com/questions/69816821/mappedinterceptor-bean-vs-webmvcconfigurer-addinterceptors-what-is-the-correct ,正确的方式是使用 registry.addInterceptor()。遗憾的是,对我来说这种方式不起作用。
如何正确注册一个拦截器呢?
英文:
I'm building a backend using Spring Boot 3.1.0-SNAPSHOT, which uses Spring Framework 6x.
Interceptor:
@Slf4j
public class MyInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        log.info("preHandle");
        return true;
    }
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        log.info("afterCompletion");
    }
}
In previous versions (Spring Boot 2), the way to add an Interceptor was:
@Configuration
public class AppConfig implements WebMvcConfigurer {
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
     registry.addInterceptor( new MyInterceptor());
    }
}
Now, the correct way to add this type of configuration class is:
@Configuration
public class AppConfig {
    // Not working now
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
     registry.addInterceptor( new MyInterceptor()).addPathPatterns("/api/**");
    }
}
How to add an Interceptor now?
I have tried:
@Configuration
public class AppConfig {
    @Bean
    public InterceptorRegistry interceptorRegistry() {
        InterceptorRegistry registry = new InterceptorRegistry();
        registry.addInterceptor(new MyInterceptor());
        return registry;
    }
}
Not working...
A working example is by using MappedInterceptor:
@Configuration
public class AppConfig {
    @Bean
    public MappedInterceptor loginInter() {
        return new MappedInterceptor(null, new MyInterceptor());
    }
}
But according to other threads here, for example this one https://stackoverflow.com/questions/69816821/mappedinterceptor-bean-vs-webmvcconfigurer-addinterceptors-what-is-the-correct, says that the correct way is by using registry.addInterceptor(). Sadly is not working for me this way.
How to correctly register an Interceptor now?
答案1
得分: 2
I was able to still use the HandlerInterceptor with Spring Boot 3 replacing the javax class imports to jakarta:
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
@Component
public class AuthorizeInterceptorConfig implements HandlerInterceptor {
@Autowired
private AuthorizeController authorizeController;
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
    return authorizeController.isValidUser(request.getHeader("X-Document-Id"));
}
(Note: The code part is translated as per your request, and no additional content is provided.)
英文:
I was able to still use the HandlerInterceptor with Spring Boot 3 replacing the javax class imports to jakarta:
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
@Component
public class AuthorizeInterceptorConfig implements HandlerInterceptor {
@Autowired
private AuthorizeController authorizeController;
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse 
 response, Object handler) {
	return authorizeController.isValidUser(request.getHeader("X-Document-Id"));
}
答案2
得分: 0
根据 https://docs.spring.io/spring-boot/docs/3.0.5/reference/pdf/spring-boot-reference.pdf,我们仍然可以使用实现了 WebMvcConfigurer 接口的 @Configuration 注解类,但不需要使用 @EnableWebMvc 注解来实现这种用例。因此,以下代码仍然有效。
@Configuration
public class AppConfig implements WebMvcConfigurer {
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new CustomInterceptor());
    }
}
英文:
According to https://docs.spring.io/spring-boot/docs/3.0.5/reference/pdf/spring-boot-reference.pdf
We can still use @Configuration annotated class that implements WebMvcConfigurer but without @EnableWebMvc annotation for this use case. So, the following is still valid.
@Configuration
public class AppConfig implements WebMvcConfigurer {
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
     registry.addInterceptor( new CustomInterceptor());
    }
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论