Enable CORS In Spring MongoDB Data Access

I am trying to create a RestFul web service using Spring, which retrieves data from the mongoDB collection and serves it for the client. To build the service, I followed this spring.io tutorial . Everything went well, I can access the data from mongoDB and find it for the name of the data structure. The problems started when I tried to manage requests from my client, I get a classic error with violations in the same area.

No header "Access-Control-Allow-Origin" is present in the requested resource.

The project is extremely simple, consists of three classes:

Frames.java

@Id
private String Id;

private String frameName;
private ArrayList<String> frameElements;

public String getId() {
    return Id;
}

public String getFrameName() {
    return frameName;
}

public ArrayList<String> getFrameElements() {
    return frameElements;
}

FrameRestFulServiceApplication.java

@SpringBootApplication
public class FrameRestFulServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(FrameRestFulServiceApplication.class, args);
    }
}

FramesRepository.java

 @RepositoryRestResource(collectionResourceRel = "frames", path = "frames")
    public interface FramesRepository extends MongoRepository<Frames, String>{

    List<Frames> findByFrameNameLike(@Param("frameName") String frameName);
    List<Frames> findByFrameName(@Param("frameName") String frameName);

}

, . , ...

+4
2

Spring Data Rest and Cors

: Spring Boot ( Filter beans), - :

@Configuration
public class RestConfiguration {

    @Bean
    public CorsFilter corsFilter() {

        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration config = new CorsConfiguration();
        config.setAllowCredentials(true); // you USUALLY want this
        config.addAllowedOrigin("*");
        config.addAllowedHeader("*");
        config.addAllowedMethod("GET");
        config.addAllowedMethod("PUT");
        source.registerCorsConfiguration("/**", config);
        return new CorsFilter(source);
    }
}
+2

?

@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**");
    }
}

0

All Articles