Здравствуйте,
С помощью аннотации @Order(…) вы задаете очередность для компонента, то есть при добавлении аннотации на метод public SecurityFilterChain securityFilterChain(HttpSecurity http)
вы задаёте приоритет вашей конфигурации над уже ранее созданными securityFilterChain
.
Для добавления кастомного фильтра в цепочке фильтров вы можете воспользоваться одним из методов addFilter()
(подробнее по ссылке ниже).
Пример добавления кастомного фильтра, который будет выполняться до AnonymousAuthenticationFilter фильтра:
@Bean
@Order(JmixOrder.HIGHEST_PRECEDENCE + 200)
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
....
http.addFilterBefore(
new CustomAuthenticationProcessingFilter(getRequestMatchers(),
authenticationConfiguration.getAuthenticationManager()),
AnonymousAuthenticationFilter.class
);
...
return http.build();
}
private RequestMatcher getRequestMatchers() {
return new OrRequestMatcher(new AntPathRequestMatcher("/api/**"));
}
CustomAuthenticationProcessingFilter:
public class CustomAuthenticationProcessingFilter extends AbstractAuthenticationProcessingFilter {
...
@Override
public Authentication attemptAuthentication(HttpServletRequest request,
HttpServletResponse response) throws AuthenticationException {
//todo
return new UsernamePasswordAuthenticationToken(user, password, authorities);
}
@Override
protected void successfulAuthentication(HttpServletRequest request,
HttpServletResponse response,
FilterChain chain,
Authentication authResult) throws IOException, ServletException {
SecurityContextHolder.getContext().setAuthentication(authResult);
chain.doFilter(request, response);
}
}
Пример:
С Уважением,
Никита