Spring Boot中如何解决跨域问题?

在Spring Boot中解决跨域问题(CORS,Cross-Origin Resource Sharing)有多种方法。以下是几种常见的解决方案:
# 1. 使用 @CrossOrigin 注解
这是最简单的方法之一,适用于特定的控制器或方法级别。
## 示例:
java
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@CrossOrigin(origins = "http://example.com") // 允许来自 example.com 的请求
public class MyController {
@GetMapping("/hello")
public String hello() {
return "Hello, World!";
}
}
你也可以在方法级别使用 @CrossOrigin,以便更细粒度地控制哪些端点允许跨域请求。
java
@RestController
public class MyController {
@CrossOrigin(origins = "http://example.com")
@GetMapping("/hello")
public String hello() {
return "Hello, World!";
}
}
# 2. 配置全局 CORS 过滤器
如果你希望为整个应用程序配置统一的跨域策略,可以使用 WebMvcConfigurer 或者直接配置一个全局的 CorsFilter。
## 方法一:通过 WebMvcConfigurer 配置全局 CORS 策略
java
import org.springframework.context.annotation.Bean;
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 WebConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/") // 匹配所有路径
.allowedOrigins("http://example.com") // 允许的源
.allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS") // 允许的HTTP方法
.allowedHeaders("*") // 允许的HTTP头
.allowCredentials(true); // 是否允许发送Cookie
}
}
## 方法二:通过 CorsFilter 配置全局 CORS 策略
java
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
@Configuration
public class CorsConfig {
@Bean
public CorsFilter corsFilter() {
CorsConfiguration corsConfiguration = new CorsConfiguration();
corsConfiguration.setAllowCredentials(true);
corsConfiguration.addAllowedOrigin("http://example.com");
corsConfiguration.addAllowedHeader("*");
corsConfiguration.addAllowedMethod("*");
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/", corsConfiguration);
return new CorsFilter(source);
}
}
# 3. 使用 Spring Security 配置 CORS
如果你的应用程序使用了 Spring Security,你需要确保 CORS 配置与安全配置兼容。可以通过 HttpSecurity 来配置 CORS。
java
import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.cors().and().csrf().disable(); // 如果不需要CSRF保护,可以禁用
}
@Bean
CorsConfigurationSource corsConfigurationSource() {
CorsConfiguration configuration = new CorsConfiguration();
configuration.setAllowedOrigins(Arrays.asList("http://example.com"));
configuration.setAllowedMethods(Arrays.asList("GET", "POST", "PUT", "DELETE", "OPTIONS"));
configuration.setAllowedHeaders(Arrays.asList("*"));
configuration.setAllowCredentials(true);
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/", configuration);
return source;
}
}
# 总结
- @CrossOrigin 注解 适合对单个控制器或方法进行跨域配置。
- 全局 CORS 配置 适合为整个应用程序设置统一的跨域策略。
- Spring Security 中的 CORS 配置 适合在使用 Spring Security 的情况下处理跨域问题。
选择哪种方式取决于你的具体需求和应用场景。

你可能感兴趣的:(springboot,spring,boot,后端,java)