I have a @RestController
and I want to add to it a Spring Filter to add a check before processing request that extends OncePerRequestFilter
Also I don't want to add it to all controllers, my last resort is to add it to general WebApplicationInitializer
's onStartup
on specific path:
public void onStartup(ServletContext container) throws ServletException {
FilterRegistration corsFilterReg = servletContext.addFilter("CorsFilter", CorsFilter.class);
corsFilterReg.addMappingForUrlPatterns(null, false, "/servlet/cors/*");
Is there other way to connect Spring Filter to specific Spring RestController?
Is it wrong using filter over interceptor in Spring REST?
Should filter be connected to URLs only?
You can use @WebFilter annotation
.
Example:
@WebFilter(filterName = "Filter", urlPatterns = { "/filter/*" })
public class WebFilter implements Filter {
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
.......
}
.......
}