Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interceptor not working in Spring Boot GraphQL

Im using graphql-java-kickstart/graphql-spring-boot and I'd like to create an interceptor to add an HTTP header after processing the request.

When I'm sending a graphql request to the backend the the interceptor is not triggered. But some calls trigger the interceptor. For example when I'm opening /graphiql in my browser I see that the interceptor is triggered but when I send a graphql request from graphiql client it is not. Any idea why? Anybody got experience with this?

My config looks like this:

@Configuration
public class InterceptorConfig implements WebMvcConfigurer {

    @Autowired
    private TestInterceptor testInterceptor;

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

Also my Interceptor:

@Slf4j
@Component
public class TestInterceptor extends HandlerInterceptorAdapter {

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView)
            throws Exception {
        // post processing
        log.info("hello there");
    }

}
like image 255
LagSurfer Avatar asked May 14 '26 17:05

LagSurfer


1 Answers

I have following interceptor that successfully works in my project:

  1. My WebConfig:
@Configuration
public class WebConfig implements WebMvcConfigurer {
    
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(requestInterceptor);
    }
    
    @Autowired
    private ControllerExecInterceptor requestInterceptor;
}

Where ControllerExecInterceptor defined as:

@Component
public class ControllerExecInterceptor extends HandlerInterceptorAdapter {
    
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws AccessDeniedException, Exception {
        // ...
    }

    @Override
    public boolean postHandle(HttpServletRequest request, HttpServletResponse 
                            response, Object handler) throws {
        // ...
    }
}
like image 142
Michael Ushakov Avatar answered May 17 '26 07:05

Michael Ushakov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!