I face an issue when I try to call repositories, and services classes inside AuthorizationFilter class
this is the repo interface:
@Repository
public interface CarRepository extends JpaRepository<Car, Integer> { }
and this is my custom filter:
public class CustomAuthorizationFilter extends OncePerRequestFilter {
@Autowired
private CarRepository repo;
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
FilterChain filterChain) throws ServletException, IOException {
List<Car> cars = repo.findAll(); <---- here the repo will be null and it will return null pointer exception
// rest of code
}
we initialize the filter in SecurityConfig class
@Configuration @EnableWebSecurity @AllArgsConstructor
@Slf4j @EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws
Exception{
return super.authenticationManagerBean();
}
@Override
protected void configure(HttpSecurity http) throws Exception{
CustomAuthenticationFilter customAuthenticationFilter = new
CustomAuthenticationFilter(authenticationManagerBean());
http.authorizeRequests().anyRequest().authenticated();
http.addFilter(customAuthenticationFilter);
http.addFilterBefore(new CustomAuthorizationFilter(), UsernamePasswordAuthenticationFilter.class);
}
}
I tried to add @component to the filter class but I faced tomcat issue because the build is failed
so how can we solve this issue ?