问题
I am trying to configure an interceptor in my application and I am not being able to make it work.
In my application configuration class, I have configured in the following way:
@Configuration
@EnableWebMvc
public class AppContextConfiguration extends WebMvcConfigurerAdapter {
...
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new MyInterceptor());
}
...
}
And the interceptor:
public class MyInterceptor extends HandlerInterceptorAdapter{
private static final Logger logger = LoggerFactory.getLogger(MyInterceptor.class);
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
Object handler) throws Exception {
logger.debug("MyInterceptor - PREHANDLE");
}
}
Does anybody know why is not being invoked?
回答1:
I'm using Spring Boot and was having the same problem where addInterceptors() was being called to register the interceptor, but the interceptor never fired during a request. Yet XML configuration worked no problem.
Basically, you don't need the WebMvcConfigurerAdapter class. You just need to declare an @Bean of type MappedInterceptor:
@Bean
public MappedInterceptor myInterceptor()
{
return new MappedInterceptor(null, new MyInterceptor());
}
回答2:
Interceptor classes must be declared in spring context xml configuration file within the tag <mvc:interceptors>. Did you do that?
From the Documentation
An example of registering an interceptor applied to all URL paths:
<mvc:interceptors>
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
</mvc:interceptors>
An example of registering an interceptor limited to a specific URL path:
<mvc:interceptors>
<mvc:interceptor>
<mapping path="/secure/*"/>
<bean class="org.example.SecurityInterceptor" />
</mvc:interceptor>
</mvc:interceptors>
So, you would need to configure MyInterceptor class in the spring context xml file
回答3:
Can someone please mark Theos answer as the correct one? I had the situation of a perfectly working Spring Boot app using i18n and Thymeleaf (with a layout interceptor) as long as the app was running localhost with the following config:
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(localeChangeInterceptor());
registry.addInterceptor(thymeleafLayoutInterceptor());
}
As soon as I deployed the app to an Elasticbeanstalk instance, both interceptors were not fired anymore. Although added once. When I changed the setting to
@Bean
public MappedInterceptor localeInterceptor() {
return new MappedInterceptor(null, localeChangeInterceptor());
}
@Bean
public MappedInterceptor thymeleafInterceptor() {
return new MappedInterceptor(null, thymeleafLayoutInterceptor());
}
everything was working fine on all environments. There must be an issue with firing interceptors added with addInterceptor, it might depend on the URL that is used to invoke the request - I don't know.
Thanks for your answer, Theo, I just wanted to add this here if some else stumbles upon this nice feature.
回答4:
Maybe you should add componentscan annotation in the file where the main class is present.
@ComponentScan("package where the interceptor is placed.")
Worked for me.
回答5:
If it´s possible. Use this approach:
public class Application extends WebMvcConfigurerAdapter{
...
@Bean
public MyInterceptor myInterceptor() {
return new MyInterceptor();
}
public @Override void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(myInterceptor());
}
}
instead of:
@Bean
public MappedInterceptor myInterceptor()
{
return new MappedInterceptor(null, new MyInterceptor());
}
because with the first you can use injection features (like @Autowired, etc...)
来源:https://stackoverflow.com/questions/22633800/spring-mvc-interceptor-never-called