0

I have written a preHandle method that will check the URL for a token and it works fine.

But will the same work for ajax calls?

 public boolean preHandle(final HttpServletRequest request, final HttpServletResponse response,final Object handler) throws Exception {
            String requesttoken = request.getParameter("token");
            if (requesttoken != null && validatetoken(requesttoken)) {
                return true;
            } else {
                response.sendRedirect("/404.html");
                return false;
            }
        }
superuser33333
  • 113
  • 1
  • 11

1 Answers1

0

Yes the interceptor will work for ajax calls as long as the pattern matches. Check out this SO answer.

If you want to exclude ajax calls you can exclude the patterns like :

@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new ThemeInterceptor()).addPathPatterns("/**").excludePathPatterns("/ajax/**");
    }

}
Community
  • 1
  • 1
Abdullah Khan
  • 12,010
  • 6
  • 65
  • 78