У меня есть приложение с весенней загрузкой, которое использует аутентификацию OAuth. Помимо аутентификации, мне нужно авторизовать пользователей, прежде чем они смогут получить доступ к системе. Я создал собственный фильтр, который авторизует пользователя. Я просто хочу запустить этот фильтр только после BasicAuthenticationFilter
. Если BasicAuthenticationFilter
не запускается, мой фильтр также не должен запускаться.
AuthorizationFilter.java
@Component
public class AuthorizationFilter extends OncePerRequestFilter {
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
boolean isValidUser = true;
// we get the authenticated user from the context
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
String id = (authentication == null)? "" : authentication.getName();
..
// code to get the user data from database using 'id' and set isValidUser flag
..
if(isValidUser) {
filterChain.doFilter(request, response);
}
else {
...
// handle UNAUTHORIZED
...
}
}
}
SecurityConfiguration.java
@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
public void configure(final HttpSecurity security) throws Exception {
security.requestMatchers()
.antMatchers("/actuator/health")
.and()
.authorizeRequests()
.antMatchers("/actuator/health").permitAll()
.and()
.csrf().disable();
security.cors();
// Custom filter to validate if user is authorized and active to access the system
security.addFilterAfter(new AuthorizationFilter(), BasicAuthenticationFilter.class);
}
}
Вопросы:
Даже если я разрешил конечную точку «/ привод / здоровье», мой пользовательский фильтр все еще работает для этой конечной точки. Как я могу исключить мой фильтр из режима «/ привод / здоровье»?
Идеальным решением было бы запустить мой фильтр, только если работает BasicAuthenticationFilter
. Это возможно? Как?