1

I've looked at similar problems:

how to intercept all requests in spring REST controllers?

spring boot adding http request interceptors

And still can't figure out the solution. I have a Spring Boot app that runs a bunch of Rest Controllers and want to intercept calls and perform some business logic before the request is passed to the controllers. I have this:

My Application class for Sprint Boot:

package com.amazonaws.lambda.keefinty.application;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

@SpringBootApplication(scanBasePackages="com.amazonaws.lambda.keefinty.controllers")
public class Application extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

My Configuration class in same package a Application:

package com.amazonaws.lambda.keefinty.application;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import com.amazonaws.lambda.keefinty.interceptors.MyInterceptor;

@Configuration
public class MyConfiguration extends WebMvcConfigurerAdapter  {

    @Bean
    public MyInterceptor getInterceptor() {
        return new MyInterceptor();
    }

    @Override
    public void addInterceptors (InterceptorRegistry registry) {
        System.out.println("\n\nAdding interceptors\n\n");
        registry.addInterceptor(getInterceptor()).addPathPatterns("/**");
    }
}

And finally the interceptor class:

package com.amazonaws.lambda.keefinty.interceptors;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

@Component
public class MyInterceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {

        System.out.print("\\n\\nIn MyInterceptor.preHandle\\n\\n");
        String token = request.getParameter("token");

        if (StringUtils.isBlank(token)) {
            throw new Exception("Invalid User Id or Password. Please try again.");
        }

        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler,
            Exception exception) throws Exception {
        // TODO Auto-generated method stub
        System.out.print("\n\nIn MyInterceptor.afterCompletion\\n\\n");
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
            ModelAndView modelAndView) throws Exception {
        // TODO Auto-generated method stub
        System.out.print("\\n\\nIn MyInterceptor.postHandle\\n\\n");
    }
}

The Configuration class never seems to get called to register the interceptors nor does MyInterceptor.

Michael Petch
  • 46,082
  • 8
  • 107
  • 198

1 Answers1

3

By declaring @SpringBootApplication(scanBasePackages="com.amazonaws.lambda.keefinty.controllers") only annotated components from com.amazonaws.lambda.keefinty.controllers package and it's sub packages will be discovered.

You MyConfiguration class is in com.amazonaws.lambda.keefinty.application package which is not part of the declared component scan package.

One way to resolve this is to remove scanBasePackages argument from your @SpringBootApplication declaration. This will allow MyConfiguration to be component scanned as by default the package in which @SpringBootApplication is declared is component scanned.

Karol Dowbecki
  • 43,645
  • 9
  • 78
  • 111
  • I've added the package name in the scan and that worked. I thought that Spring Boot would automatically look in the same package as the Application. Thanks. –  May 19 '18 at 11:39
  • Of note is that it is actually a good idea to indicate the packages to be scanned so as to speed up start up of the application. For example, various packages with simple non-Spring managed POJOs do not need to be scanned. – filpa May 19 '18 at 12:34