Я пробовал различные варианты, позволяющие моему интерфейсу взаимодействовать с конечной точкой весной, избегая при этом проблемы с Cors. Здесь я предлагаю вам одно из решений, которые я пробовал, но все равно не сработало. Конечная точка – /api/v1/test
Класс конфигурации безопасности:
Код: Выделить всё
@Bean
CorsConfigurationSource corsConfigurationSource() {
CorsConfiguration configuration = new CorsConfiguration();
configuration.setAllowedOrigins(Arrays.asList("http://localhost:5123"));
configuration.setAllowedMethods(Arrays.asList("GET","POST","DELETE","OPTIONS"));
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", configuration);
return source;
}
@Bean
public SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Exception {
return httpSecurity
.cors(cors -> cors.disable())
.csrf(csrf -> csrf.disable())
.authorizeHttpRequests(auth -> auth
.requestMatchers(
AntPathRequestMatcher.antMatcher("/api/v1/test")
)
.permitAll()
.requestMatchers("/api/**")
.authenticated()
)
.sessionManagement(sess -> sess.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
.build();
} }
Код: Выделить всё
@RestController
@RequestMapping(value = "/api/v1/")
@Slf4j
public class TestController {
@GetMapping(value = "test", produces = "application/json")
@Operation(method = "GET")
public ResponseEntity getTest() {
...
Доступ к XMLHttpRequest по адресу «https://» имя_сервера/api/v1/test' из
источника 'http://localhost:5123' заблокировано политикой CORS:
Ответ на предполетный запрос не проходит проверку контроля доступа: Нет
Заголовок Access-Control-Allow-Origin присутствует в запрошенном
ресурсе.
Подробнее здесь: https://stackoverflow.com/questions/790 ... -http-loca