Можно ли сделать отдельный фильтр на каждый antMatchers в Spring Security?
коллеги!
У меня есть небольшое приложение с некоторой конфигурацией spring security, но я в ней пока не очень силен и столкнулся с проблемой. Вот моя конфигурация
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.requestMatchers()
.antMatchers("/txn")
.antMatchers("/profiles")
.and()
.addFilterBefore(new AuthenticationFilter(), BasicAuthenticationFilter.class)
.anyRequest().permitAll()
.and()
.exceptionHandling().accessDeniedHandler(authAccessDeniedHandler);
}
При вызове апи /txn или /profiles отладчик проваливается в метод protected void doFilterInternal из класса AuthenticationFilter. Внутри этого метода я получаю токен из хедеров и валидирую его по своим правилам. выглядит это примерно вот так:
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
String authorization = получение токена из запроса;
if (isEmpty(authorization)) {
this.setUnauthorizedResponse(response); //заполняем неавторизованный ответ чтобы вернуть 401
return;
}
Authentication auth = new TokenAuthentication(authorization);
SecurityContextHolder.getContext().setAuthentication(auth);
filterChain.doFilter(request, response);
}
А теперь появились новые требования и проблема заключается в следующем, для /txn и /profiles будет разная логика получения токена из-запроса. И я мечтаю, чтобы можно было для разных апи вызывать разные .addFilterBefore(new AuthenticationFilter(), BasicAuthenticationFilter.class) как можно это осуществить?
Заранее спасибо за ответы и предложения!