後端解決跨域的一種方案

後端解決跨域的一種方案

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 CorsConfig implements WebMvcConfigurer {

    @Override
    public void addCorsMappings(CorsRegistry corsRegistry){
        corsRegistry.addMapping("/**")//允許跨域的訪問路徑
                    .allowedOrigins("http://localhost:8081")//允許跨域訪問的源
                    .allowedMethods("POST","GET","PUT","OPTIONS","DELETE")//允許請求方法
                    .allowCredentials(true)//是否允許發送cookie
                    .maxAge(3600)//預檢的間隔時間
                    .allowedHeaders("*");//允許跨域訪問的header
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章