spring怎么配置跨域请求
-
要在Spring中配置跨域请求,你可以按照以下几个步骤进行操作:
步骤一:在你的Spring项目中的配置文件(如application.properties或application.yaml)中添加以下配置:
对于XML配置方式:
<mvc:cors> <mvc:mapping path="/**" allowed-origins="*" allowed-methods="GET,PUT,POST,DELETE,OPTIONS" /> </mvc:cors>对于Java配置方式:
@Configuration @EnableWebMvc public class WebConfig implements WebMvcConfigurer { @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**") .allowedOrigins("*") .allowedMethods("GET", "PUT", "POST", "DELETE", "OPTIONS"); } }步骤二:在你的Controller类或方法上添加
@CrossOrigin注解来明确允许跨域请求。@CrossOrigin(origins = "*", allowedHeaders = "*", methods = {RequestMethod.GET, RequestMethod.PUT, RequestMethod.POST, RequestMethod.DELETE, RequestMethod.OPTIONS}) @RestController @RequestMapping("/api") public class MyController { // your controller methods }使用
@CrossOrigin注解,你可以定义允许的原始域、请求头和请求方法。步骤三:确保你的跨域请求正确发送的时候,服务器会正确地响应跨域请求。
通过以上步骤配置完之后,Spring应用程序将能够处理来自任意域的跨域请求。
1年前 -
在Spring框架中配置跨域请求需要以下步骤:
- 在Spring Boot中,可以通过使用
@CrossOrigin注解来实现跨域请求的配置。在Controller类或者Controller方法上添加@CrossOrigin注解即可启用跨域请求。
@RestController public class ExampleController { @CrossOrigin(origins = "http://localhost:8080") @GetMapping("/example") public String example() { return "Hello, Cross Origin!"; } }在上述示例中,
@CrossOrigin注解指定了允许的来源(origin)为http://localhost:8080,其他的请求来源都将被拒绝。- 如果需要配置更多的跨域选项,可以使用
CorsConfiguration类进行自定义配置。可以创建一个全局的WebMvcConfigurer配置类,在其中重写addCorsMappings方法来进行跨域配置。
@Configuration public class CorsConfig implements WebMvcConfigurer { @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/api/**") .allowedOrigins("http://localhost:8080") .allowedMethods("GET", "POST", "PUT", "DELETE") .allowedHeaders("*") .allowCredentials(true); } }在上述示例中,
addCorsMappings方法添加了一个跨域映射路径为/api/**,允许的来源是http://localhost:8080,允许的方法是GET、POST、PUT和DELETE,允许所有的请求头,同时允许携带身份验证信息。- 如果使用Spring MVC,可以通过注册
CorsFilter过滤器来实现跨域请求的配置。在Web应用的配置类中添加以下代码:
@Bean public FilterRegistrationBean<CorsFilter> corsFilter() { final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); final CorsConfiguration config = new CorsConfiguration(); config.setAllowCredentials(true); config.addAllowedOrigin("http://localhost:8080"); config.addAllowedHeader("*"); config.addAllowedMethod("*"); source.registerCorsConfiguration("/**", config); final FilterRegistrationBean<CorsFilter> bean = new FilterRegistrationBean<>(new CorsFilter(source)); bean.setOrder(Ordered.HIGHEST_PRECEDENCE); return bean; }在上述示例中,我们创建了一个
CorsConfiguration对象,并设置了允许携带身份验证信息、允许的来源、允许的请求头和允许的方法。然后,我们使用UrlBasedCorsConfigurationSource将配置和路径匹配起来,并在FilterRegistrationBean中注册CorsFilter过滤器。- 如果使用Spring Security,需要配置Spring Security的跨域请求。可以通过重写
WebSecurityConfigurerAdapter的configure方法来进行跨域配置。
@Configuration @EnableWebSecurity public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.cors().and().csrf().disable(); } }在上述示例中,
configure方法使用http.cors()启用跨域请求,使用http.csrf().disable()禁用CSRF跨站请求伪造防护。- 如果某些请求需要特定的跨域配置,可以使用
CorsConfigurationSource接口进行自定义。可以创建一个实现CorsConfigurationSource接口的类,并在其中进行自定义跨域配置。
@Configuration public class CustomCorsConfiguration implements CorsConfigurationSource { @Override public CorsConfiguration getCorsConfiguration(HttpRequest request) { CorsConfiguration config = new CorsConfiguration(); config.setAllowedOrigins(Collections.singletonList("http://localhost:8080")); config.setAllowedMethods(Arrays.asList("GET", "POST", "PUT", "DELETE")); config.setAllowedHeaders(Collections.singletonList("*")); config.setAllowCredentials(true); return config; } }在上述示例中,我们创建了一个名为
CustomCorsConfiguration的类,实现了CorsConfigurationSource接口,并重写了getCorsConfiguration方法来自定义跨域配置。通过以上的配置,Spring框架就可以支持跨域请求了。
1年前 - 在Spring Boot中,可以通过使用
-
Spring配置跨域请求的方法如下:
- 使用Java配置方式(JavaConfig):
首先,在你的Spring配置文件中导入相关的类:
import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.CorsRegistry; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration @EnableWebMvc public class WebConfig implements WebMvcConfigurer { @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**") .allowedOrigins("*") .allowedMethods("*") .allowedHeaders("*") .allowCredentials(true) .maxAge(3600); } }这段代码配置了允许来自所有域的跨域请求,并允许所有的HTTP方法和HTTP头部。
- 使用XML配置方式:
在Spring配置文件里添加以下配置:
<bean id="corsConfigurationSource" class="org.springframework.web.cors.UrlBasedCorsConfigurationSource"> <property name="corsConfigurations"> <map> <entry key="/**"> <bean class="org.springframework.web.cors.CorsConfiguration"> <property name="allowedOrigins" value="*"/> <property name="allowedMethods" value="*"/> <property name="allowedHeaders" value="*"/> <property name="allowCredentials" value="true"/> <property name="maxAge" value="3600"/> </bean> </entry> </map> </property> </bean> <bean id="corsFilter" class="org.springframework.web.filter.CorsFilter"> <constructor-arg ref="corsConfigurationSource" /> </bean>这段代码配置了允许来自所有域的跨域请求,并允许所有的HTTP方法和HTTP头部。
- 使用注解方式:
在你的Controller类或者Controller的方法上添加
@CrossOrigin注解,如下所示:@RestController @RequestMapping("/api") @CrossOrigin(origins = "*", allowedHeaders = "*", methods = { RequestMethod.GET, RequestMethod.POST, RequestMethod.PUT, RequestMethod.DELETE }) public class ApiController { // ... }这段代码配置了允许来自所有域的跨域请求,并允许所有的HTTP方法和HTTP头部。
以上是三种常见的Spring跨域请求配置方法,选择其中一种即可根据你的具体需求进行配置。
1年前