Dermot O'Connor
Dermot O'Connor

Reputation: 23

Spring Boot HandlerInterceptor not firing when Jersey Configured?

I have a Spring Boot v1.4.0 application configured with Jersey for delivery of RESTful services.

I have a working app, but I'm now trying to enhance it with a multi-tenancy SCHEMA awareness strategy. I was hoping to set a TenantContext based on client auth headers using a Spring's HandlerInterceptor framework...

BUT, there seems to be an issue with the Interceptors being fired with Jersey. I can hit the APIs fine, ( i.e. curl -i -H "Accept: application/json" -X GET http://localhost:8080/api/products ), but the interceptors just won't fire. If I wire up a more basic app without Jersey for resource management, they fire fine?

Here is the current application set-up:

@SpringBootApplication
@EnableAutoConfiguration
public class Application {
    public static void main(String[] args) {
        new SpringApplicationBuilder(Application.class).run(args);
    }
}

Registering the Interceptor

@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {

    @Autowired
    HandlerInterceptor tenantInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(tenantInterceptor);
    }
}

The Interceptor itself

@Component
public class TenantInterceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest req, HttpServletResponse res, Object handler) throws Exception {
        // FIXME: Put in a Logger impl
        System.out.println("++++++++++++=======+++++++++ TenantInterceptor.preHandle() Checking for Tenant Routing");

        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        TenantContext.clear();
        // FIXME: Put in a Logger impl
        System.out.println("++++++++++++=======+++++++++ TenantInterceptor.postHandle() Reset Tenant to " + TenantContext.getCurrentTenant());
    }
}

The JerseyConfig

@Component
@ApplicationPath("api")
public class JerseyConfig extends ResourceConfig {

    @PostConstruct
    private void init() {
        registerClasses(TenantsResource.class);
        registerClasses(UsersResource.class);
        registerClasses(ProductsResource.class);
    }

}

I played around with the JerseyConfig @ApplicationPath("api") and the WebMvcConfig registry.addInterceptor(tenantInterceptor).addPathPatterns("patterns");. Tried the following one after the other, but no joy.

registry.addInterceptor(tenantInterceptor).addPathPatterns("/*");

registry.addInterceptor(tenantInterceptor).addPathPatterns("/**");

registry.addInterceptor(tenantInterceptor).addPathPatterns("/api/**");

registry.addInterceptor(tenantInterceptor).addPathPatterns("/api/*");

registry.addInterceptor(tenantInterceptor).addPathPatterns("/api/products");

registry.addInterceptor(tenantInterceptor).addPathPatterns("/api/products/");

Any help - much appreciated, or else I'll be resorting to hacking the Resource Controllers with smelly code :(.

Thanks - Derm

Upvotes: 2

Views: 1505

Answers (1)

Paul Samsotha
Paul Samsotha

Reputation: 208944

As mentioned by M.Deinum, HandlerInterceptor is not for Jersey, and it not some "underversal" interceptor. It is only for Spring MVC. For Jersey, you can use a ContainerRequestFilter. You would register it with you ResourceConfig.

See also:

Upvotes: 1

Related Questions