0

I have added a filter in my spring backend for JWT authentication alongside a CORSFilter, however it seems that my JWTAuthFilter is not getting executed at all(CORSFilter has no problems and gets executed).

I tried adding print statements in init and doFilter and also adding @ComponentScan in my main application, but it still does not work. How can I fix it?
My CORS Filter :

//@Configuration
@Component
@Order(1)
public class CORSFilter {

    @Bean
    public WebMvcConfigurer getCorsConfiguration(){
        return new WebMvcConfigurer() {
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/**")
                        .allowedOrigins("*")
                        .allowedMethods("*")
                        .allowedHeaders("*");
            }
        };
    }
}

My JWT Filter :

@Component
@Order(2)
@WebFilter("/*")
public class JWTAuthFilter implements Filter {

    @Autowired
    private BalootService balootService;

    public void init(FilterConfig config) throws ServletException {
        System.out.println("init"); //not getting printed !
    }

    public void destroy() {
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        System.out.println("jwt filter"); //not getting printed
        //processing the request

My main application :

@SpringBootApplication
//@RestController
@ServletComponentScan
@ComponentScan(basePackages = {"com.baloot.baloot", "com.baloot.baloot.filters"})
public class BalootApplication {

    public static void main(String[] args) {
        SpringApplication.run(BalootApplication.class, args);
    }

}
alimrn
  • 33
  • 6
  • Hello, You can look: https://stackoverflow.com/questions/19825946/how-can-i-add-a-filter-class-in-spring-boot it is explaining how to add a filter. – Andreea Frincu Jun 12 '23 at 17:34

0 Answers0