I want to configure spring interceptor so that every request is called.
- I use interceptor in API-GATEWAY (Spring-Boot)
- From API GATEWAY I call other microservices.
- Calling other microservices from API-GATEWAY is working fine.
- Other services that I call are Node.js Service, on the other hand, my API gateway is in spring boot.
- All services (Node.js + Spring-Boot) run on the Docker Console .
I am facing a problem in Interceptor. I want to configure it in such a way that at each request it would be necessary to name it preHandle()and perform the operations that I wrote in it.
I noticed one problem that I want to mention here.
If the services that I call are stopped (not working), Interceptor is working correctly and gives me an answer such as somename-service was not found. If the same services are started at this time, the Interceptor is not executed.
Here is my code snippet
@EnableEurekaClient
@SpringBootApplication
@EnableZuulProxy
@Configuration
public class Application extends WebMvcConfigurerAdapter {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Autowired
private TokenValidateInterceptor tokenValidateInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(tokenValidateInterceptor).addPathPatterns("/**");
}
Interceptor
@Component
public class TokenValidateInterceptor extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
LOG.info("#### Starting TokenValidateInterceptor.preHandle ####");
String apiKey = null;
try {
apiKey = request.getHeader("apikey");
LOG.info("The request come with apikey ======" + apiKey);
LOG.info("Actual apikey ======" + azureApikey);
}
Eddie source
share