1. 程式人生 > >springboot前後端分離之跨域

springboot前後端分離之跨域

springmvc有多種處理跨域的方法,介紹最簡單的一種:

@Configuration
public class WebMvcConfig  extends WebMvcConfigurerAdapter {
	@Override
	public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
        .allowedOrigins("*")
        .allowedMethods("GET", "POST", "PUT", "OPTIONS", "DELETE", "PATCH")
        .allowCredentials(false).maxAge(3600);
	}
}

還有使用spring security時需在@EnableWebSecurity配置類裡

@Override
protected void configure(HttpSecurity http) throws Exception {
		http.cors().and().crsf().disable();
}