Проблема с ошибкой происхождения CORS для приложения Springboot 3
При попытке вызвать API из Swagger с добавлением Aad OAuth SecurityConfig, который я использую, поскольку все API получают доступ на основе роли приложения, установленной в корпоративном приложении, с использованием единого входа.
@Configuration(proxyBeanMethods = false)
@EnableWebSecurity
@EnableMethodSecurity
public class AadOAuth2LoginSecurityConfig {
/**
* Add configuration logic as needed.
*/
@Bean
SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http.cors(cors -> cors.disable()).csrf(csrf -> csrf.disable()).apply(AadWebApplicationHttpSecurityConfigurer.aadWebApplication()).and().authorizeHttpRequests().requestMatchers("swagger-ui/**").permitAll().requestMatchers("/swagger-ui/**").permitAll().requestMatchers("api-docs/**").permitAll().requestMatchers("/api-docs/**").permitAll().requestMatchers("/api/v1/**").authenticated();
return http.build();
}
@Bean
public CorsConfigurationSource corsConfigurationSource() {
CorsConfiguration configuration = new CorsConfiguration();
configuration.setAllowedOrigins(List.of("*"));
configuration.setAllowedHeaders(Arrays.asList("Origin", "Authorization"));
configuration.setExposedHeaders(Arrays.asList("Access-Control-Allow-Origin", "Access-Control-Allow-Credentials"));
configuration.setAllowedMethods(Arrays.asList("HEAD", "GET", "POST", "PUT", "DELETE", "PATCH", "OPTION"));
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", configuration);
return source;
}
}
Используемые версии: Java -17 Springboot - 3.1.2.