ARTICLE DETAIL

资讯详情

深耕网站视觉设计与运营推广的一线实战洞察。

JAVA 跨域设置

JAVA 跨域设置 java 跨域请求设置importorg.springframework.context.annotation.Configuration;importorg.springframework.web.servlet.config.annotation.CorsRegistry;importorg.springframework.web.servlet.config.annotation.WebMvcConfigurer;ConfigurationpublicclassConfigimplementsWebMvcConfigurer{OverridepublicvoidaddCorsMappings(CorsRegistry registry){registry.addMapping(/**)// 允许所有路径.allowedOriginPatterns(*)// 允许所有来源使用 Pattern// .allowedOrigins(http://localhost:5173) // 允许的前端地址.allowedMethods(GET,POST,PUT,DELETE,OPTIONS)// 允许的方法.allowedHeaders(*)// 允许的请求头.allowCredentials(true)// 允许携带cookie.maxAge(3600);// 预检请求缓存时间秒}}
返回列表