3 Ağustos 2021 Salı

SpringMVC WebMvcConfigurer Sınıfı

addCorsMappings metodu
Örnek
Şöyle yaparız
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
 
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
 
 @Override
 public void addCorsMappings(CorsRegistry registry) {
   registry.addMapping(“/**”)
     .allowedMethods(“*”)
     .allowedHeaders(“*”)
     .allowedOrigins(“http://192.168.1.9:3000”)
     .allowCredentials(false)
     .maxAge(-1);
  }
}
Açıklaması şöyle
- “*” means it will accept any allowable value for that field. For example, allowedMethods will accept GET, POST, PUT, DELETE, OPTIONS, etc. @see org.springframework.http.HttpMethod.
- allowedOrigins, tells Spring to only accept Ajax requests from these URL patterns.
- maxAge — indicates how long to cache the result of a CORS preflight request.

Hiç yorum yok:

Yorum Gönder