spring-projects / spring-projects/spring-data-rest

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

Open
#1,522 2 comments 1 reaction 1 assignee View on GitHub

@odrotbohm is already working on this.

Since Dec 31, 2020.

type: bug
Dominant language
Java
Stars
958
Forks
568
PR merge metrics
No merged PRs in 30d

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.