Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Interceptor not working in Spring Data REST URLs [DATAREST-1155] #1522

Open
spring-projects-issues opened this issue Oct 26, 2017 · 2 comments
Assignees
Labels
type: bug A general bug

Comments

@spring-projects-issues
Copy link

Andre Rocha opened DATAREST-1155 and commented

I am working on a project with Spring Data Rest and JPA and I am trying to configure an HTTP interceptor to configure switch the tenant depending on the HTTP request header. As per the reference docs, available in Spring Web MVC Docs - Handler Mapping Interceptor, I created a component that extends HandlerInterceptorAdapter as follows:

@Component
public class DBEditorTenantInterceptor extends HandlerInterceptorAdapter {

Logger logger = LoggerFactory.getLogger(DBEditorTenantInterceptor.class);

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
    throws Exception {
     logger.debug("********** INTERCEPTION SUCCESSFUL **********");
     return true;
}

}
And then, registered the interceptor by extending WebMvcConfig (as explained in Spring Web MVC Docs - Config Interceptors

@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {

@Autowired
DBEditorTenantInterceptor dbEditorTenantInterceptor;

@Override
public void addInterceptors(InterceptorRegistry registry) {
     registry.addInterceptor(dbEditorTenantInterceptor)
     .addPathPatterns("/**");
}

}
When I issue HTTP requests to any URL that is not used by Spring Data REST such as /helloworld the Interceptor works as expected, as I see the logger output

017-10-26 13:16:24.689 DEBUG 17012 --- [p-nio-80-exec-4] c.c.v.d.DBEditorTenantInterceptor : ********** INTERCEPTION SUCCESSFUL **********
However, when the URL is used by spring data rest, my interceptor is not called. This applies to all URLs like /api/{existing entity in model}

Why is my interceptor not called for Spring Data Rest URLs ? What can I do to make my interceptor work for all requests ?

Thanks a lot in advance.

PS: this question is also on stackoverflow in https://stackoverflow.com/questions/46953039/spring-interceptor-not-working-in-spring-data-rest-urls


No further details from DATAREST-1155

@peter-janssen
Copy link

Just encountered this same issue. Is this expected behaviour of Spring Data Rest?

@peter-janssen
Copy link

peter-janssen commented Mar 22, 2024

Took me a while but I understand now. Spring Data REST uses its own RepositoryRestDispatcherServlet. Which only knows about the RepositoryRestHandlerMapping. Which, like every HandlerMapping, picks up MappedInterceptors. That's why the solution presented on Stack Overflow works:

@Bean
public MappedInterceptor someMethodName() {
    return new MappedInterceptor(
        null,  // => maps to any repository
        new YourInterceptorImpl()
    );
}

Spring MVC has a the InterceptorRegistry to register interceptors to four HandlerMappers used by the "regular" DispatcherServlet.
So Spring Data REST never considers Interceptors from the InterceptorRegistry. I suppose this is an architectural decision since it has its own MVC implementation.

However, when trying to apply interceptors to all requests using both Spring Data REST and regular Spring MVC controllers it would require to only use the MappedInterceptors. This is somewhat unfortunate since the InterceptorRegistration provides some fine grained control over the order of Interceptors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug A general bug
Projects
None yet
Development

No branches or pull requests

3 participants