简体   繁体   中英

CORS origin error in Angular while calling Eureka API

I have Java Spring Boot applications which has Eureka Server Application (Java project) and other Eureka Client Applications (Java projects). In Eureka Server Application project I have some of my Rest APIs exposing through my own Controller class and those APIs (localhost:8100/someApi) are accessible without any issue from Angular (localhost:4200) after configuring CORS setup in Spring Boot Application.

But some Rest APIs which are in library class of my Java Application are not accessible from Angular due to CORS error. But other Rest APIs are accessible which are defined in my Controller class created by me.

These are the APIs I am trying to access. https://github.com/Netflix/eureka/wiki/Eureka-REST-operations

These APIs are in library code which gets added if we mention the dependency in pom.ml.

<dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

I am getting CORS blocked error while trying to access these Eureka APIs from Angular but through Postman it is working fine. Please help.

Did you eable cross origin things in SpringBoot Application. Can you enable corss origin at controller level by adding @CrossOrgina annotation by default It will allow all origin including all http methods or can add customized global configuration like below.

@Bean
Public WebMvcConfigurer corsConfigurer(){
return new WebMvcConfigurer(){
@Override
public void addCorsMapping(CoreRegistry registry){
registry.addMapping("/**").allowdOrgin("*");
}
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM