Mustahsan
Mustahsan

Reputation: 3862

Configure WebRequestInterceptor in Spring Configuration

I am trying to use WebRequestInterceptor but i don't know how can i configure it in spring boot, as if I implement WebMvcConfigurer interface it requires a HandlerInterceptor object so i cannot assign my interceptor to it. Any help would be highly appreciated.

Interceptor class:

public class CustomerStateInterceptor implements WebRequestInterceptor {

    @Resource(name = "customerStateRequestProcessor")
    private CustomerStateRequestProcessor customerStateRequestProcessor;


    @Override
    public void preHandle(WebRequest webRequest) {
        customerStateRequestProcessor.process(webRequest);
    }

    @Override
    public void postHandle(WebRequest webRequest, ModelMap modelMap) {
        //unimplemented
    }

    @Override
    public void afterCompletion(WebRequest webRequest, Exception e) {
        //unimplemented
    }
}

and config class:

@Configuration
public class InterceptorConfig  implements WebMvcConfigurer {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new CustomerStateInterceptor()); // <-- Error here.
    }

}

Upvotes: 2

Views: 3833

Answers (2)

Bheem  Singh
Bheem Singh

Reputation: 707

Add filter class to your package and please try the code below -

public class RequestValidateFilter extends GenericFilterBean {
 @Override
 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {
    HttpServletRequest httpServletRequest = (HttpServletRequest) request;
    try {
        request = new RequestWrapper(httpServletRequest);
        chain.doFilter(request, response);
    } catch (Exception e) {
            throw new ServletException();
    }
 }
}

FilterClass : 

@Configuration
public class CustomWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
    http.csrf().disable();
    http.addFilterBefore(requestValidateFilter(), BasicAuthenticationFilter.class);
    http.authorizeRequests().antMatchers("/projectname/**").authenticated();
    http.addFilterAfter(responseValidateFilter(), BasicAuthenticationFilter.class);
}

private RequestValidateFilter requestValidateFilter() {
    return new RequestValidateFilter();
}

private ReponseValidateFilter responseValidateFilter() {
    return new ReponseValidateFilter();
}

}

Upvotes: 1

mrkurtan
mrkurtan

Reputation: 591

You supposed to implement HandlerInterceptor from org.springframework.web.servlet package and not WebRequestInterceptor.

Update

You can just wrap with WebRequestHandlerInterceptorAdapter:

@Configuration
public class InterceptorConfig implements WebMvcConfigurer {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(
                new WebRequestHandlerInterceptorAdapter(
                        new CustomerStateInterceptor()));
    }

}

Upvotes: 3

Related Questions