3

I have written an interceptor for my spring-boot app. But when I hit the endpoint, its executing fine.The interceptor is not able to intercept my request. Where I am going wrong or missing something?

Below is the Code

@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {

    private static final String PATHS = "/services/api/**";

    @Autowired
    private AuthorizationInterceptor authInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(authInterceptor).addPathPatterns(PATHS);
    }



}

Here is the code for Interceptor:::

@Component
public class AuthorizationInterceptor implements HandlerInterceptor {
    private static final Logger LOGGER = LoggerFactory.getLogger(AuthorizationInterceptor.class);
    private static final String MSG_BAD_INPUT = "Very Bad Input";
    private static final int MAX_URI_LENGTH = 4;

    @Override
    public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
            throws Exception {
        // TODO Auto-generated method stub

    }

    @Override
    public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
            throws Exception {
        // TODO Auto-generated method stub

    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        System.out.println("Inside Prehandle:::::::------->");
        this.checkURILength(request);
        System.out.println("After checking:::::::::::---->");
        return true;

    }

    private void checkURILength(HttpServletRequest request) {
        if (request.getRequestURI().length() > MAX_URI_LENGTH) {
            LOGGER.error("Request URI is too long");
            throw new InvalidInputException(MSG_BAD_INPUT);
        }
    }


}

Now when I hit the endpoint for my spring-boot app say, its working fine

http://localhost:8181/services/api/companies

Basically its not at all calling the prehandle. What am I missing????

sromit
  • 900
  • 3
  • 16
  • 43
  • But when I changed the` PATHS = "/services/api/**"` to `PATHS = "/**"` it is able to intercept the request, but I am not understanding why its failing for the above – sromit Mar 01 '18 at 21:04
  • You should list your dependencies. May something others goes wrong – Jess Chen Apr 08 '21 at 10:06

2 Answers2

1

Did you used @EnableWebMvc as

@EnableWebMvc
@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {
  ...
}
Hexiler
  • 213
  • 2
  • 12
  • Well interesting, is it worked without `addPathPatterns()`? – Hexiler Mar 01 '18 at 21:03
  • I know I just was curious maybe that cause some problem, but that shouldn't. Btw did you create a bean from your interceptor in the xml? Please try the following instead of `@Autowired` ` @Bean AuthorizationInterceptor authorizationInterceptor () { return new AuthorizationInterceptor (); } registry.addInterceptor(authorizationInterceptor()).addPathPatterns(PATHS);` EDIT: Sorry for code formatting... :D – Hexiler Mar 01 '18 at 21:14
  • If I changed the` PATHS = "/services/api/**"` to PATHS = "/**" it is able to intercept the request, but I am not understanding why its failing for the above – sromit Mar 01 '18 at 21:32
  • Do you have a controller which should handle that request? e.g. `@RequestMapping(value = "/services/api/something")` – Hexiler Mar 02 '18 at 10:18
0

In my case the use of a MappedInterceptor as described in the answer below worked.

https://stackoverflow.com/a/35948730/1705048

NDAclan
  • 43
  • 8