im trying to make a filter for my requests (spring) but is not doing anything

39 Views Asked by At

I made some research and by adding the following lines every request that happens should enter here

@Slf4j
@Component
@Order(1)
public class Filtro implements Filter

the problem is not even the log.info is working

@Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        log.info("ENTRY::-> doFilter");
    }

I dont know what to do so the filter works for every request, i have tried different request on different controllers but nothing works, im doing this because im implementing JWT

1

There are 1 best solutions below

0
Filipe Cavalcanti On

The way you built this filter will run on all requests.

If you want it to execute under some criteria you need to register the filter.

@Bean
public FilterREgistrationBean<Filtro> exampleRegistrationFilterBean() {
FilterRegistrationBean<Filtro> exampleRegistrationFilterBean = new FilterRegistrationBean();
exampleRegistrationFilterBean.setFilter(new Filtro());
exampleRegistrationFilterBean.addUrlPatterns("examples/**");
exampleRegistrationFilterBean.setOrder(1);
return exampleRegistrationFilterBean;
}

In the example above, the filter will only run for URLs in "examples/**"

you can see more in How to Define a Spring Boot Filter?