У меня есть список общедоступных URL-адресов (например, /login, /register и т. д.) и фильтр для аутентификации. Моя проблема в том, что когда я вызываю один из этих общедоступных URL-адресов, запрос также фильтруется (но не должен быть), и я получаю статус 401. Для остальных конечных точек все работает нормально.
Мой SecurityConfis.class:
Код: Выделить всё
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig {
@Autowired
private CustomAuthenticationFilter customAuthenticationFilter;
/**
* No authentication endpoints
*/
public static final RequestMatcher PUBLIC_URLS = new OrRequestMatcher(
new AntPathRequestMatcher("/api/auth/login"),
new AntPathRequestMatcher("/api/auth/register")
);
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
var protectedUrls = new NegatedRequestMatcher(PUBLIC_URLS);
http
.securityMatcher(protectedUrls)
.cors(cors -> cors.configurationSource(corsConfigurationSource()))
.csrf(csrf -> csrf.disable())
.sessionManagement(sessionManagement ->
sessionManagement.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
.exceptionHandling(exceptionHandling ->
exceptionHandling.authenticationEntryPoint(new HttpStatusEntryPoint(HttpStatus.FORBIDDEN)))
.authorizeHttpRequests(authorize -> authorize
.requestMatchers("/api/data-transfer/**").hasAnyAuthority("ADMIN")
.requestMatchers("/api/discount-code/**").hasAnyAuthority("USER")
.anyRequest().authenticated())
.addFilterAfter(customAuthenticationFilter, BasicAuthenticationFilter.class);
return http.build();
}
@Bean
public CorsConfigurationSource corsConfigurationSource() {
[...]
}
}
Код: Выделить всё
@Component
public class CustomAuthenticationFilter extends OncePerRequestFilter {
private static final String BEARER_PREFIX = "Bearer ";
@Autowired
private UserCrudService userCrudService;
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
[...]
}
}
Подробнее здесь: https://stackoverflow.com/questions/792 ... ringboot-3
Мобильная версия