CORS(跨源资源共享)是一种Web标准,允许来自不同源的Web页面共享资源。在Spring Boot应用程序中,CORS问题可能会出现,因为浏览器会阻止来自不同源的请求。默认情况下,Spring Boot允许来自同一源的请求,但会阻止来自不同源的请求。
要解决CORS问题,您可以使用Spring Boot提供的CORS支持。以下是一些可能的解决方案:
使用全局CORS配置
您可以在Spring Boot应用程序的主类上添加@CrossOrigin注解,以允许来自所有源的请求。例如:
@SpringBootApplication public class MyApplication { public static void main(String[] args) { SpringApplication.run(MyApplication.class, args); } @Bean public WebMvcConfigurer corsConfigurer() { return new WebMvcConfigurer() { @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**") .allowedOrigins("*") .allowedMethods("*") .allowedHeaders("*"); } }; } }
在上面的示例中,我们创建了一个WebMvcConfigurer bean,并覆盖了addCorsMappings方法。我们使用CorsRegistry对象来定义CORS规则。在这个例子中,我们允许来自所有源的请求,并允许所有方法和头部。
使用局部CORS配置
如果您只想为特定的控制器或请求方法启用CORS,您可以在控制器类或请求方法上添加@CrossOrigin注解。例如:
@RestController @RequestMapping("/api") public class MyController { @CrossOrigin(origins = "*", methods = "*", headers = "*") @GetMapping("/data") public ResponseEntitygetData() { // ... } }
在上面的示例中,我们只在getData方法上启用了CORS。我们允许来自所有源的请求,并允许所有方法和头部。
使用自定义CORS配置
如果您需要更细粒度的CORS配置,您可以创建自定义的CorsConfiguration对象,并将其添加到CorsRegistry对象中。例如:
@Bean public WebMvcConfigurer corsConfigurer() { return new WebMvcConfigurer() { @Override public void addCorsMappings(CorsRegistry registry) { CorsConfiguration config = new CorsConfiguration(); config.setAllowedOrigins(Arrays.asList("http://example.com", "https://example.org")); config.setAllowedMethods(Arrays.asList("GET", "POST", "PUT", "DELETE")); config.setAllowedHeaders(Arrays.asList("X-Requested-With", "Content-Type", "Authorization")); config.setAllowCredentials(true); registry.addMapping("/**").withConfig(config); } }; }
在上面的示例中,我们创建了一个自定义的CorsConfiguration对象,并设置了允许的源、方法、头部和凭证。然后,我们将该配置添加到CorsRegistry对象中,以应用于所有的请求路径。
除了上述方法,还有一些其他的解决方案可以用来解决Spring Boot中的CORS问题。例如:
使用Spring Security的CORS支持
如果您正在使用Spring Security,您可以使用其提供的CORS支持来解决CORS问题。以下是一个示例配置:
@Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.cors().and(). ... } @Bean public CorsConfigurationSource corsConfigurationSource() { CorsConfiguration configuration = new CorsConfiguration(); configuration.setAllowedOrigins(Arrays.asList("http://example.com", "https://example.org")); configuration.setAllowedMethods(Arrays.asList("GET", "POST", "PUT", "DELETE")); configuration.setAllowedHeaders(Arrays.asList("X-Requested-With", "Content-Type", "Authorization")); configuration.setAllowCredentials(true); UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); source.registerCorsConfiguration("/**", configuration); return source; } }
在上面的示例中,我们创建了一个CorsConfigurationSource bean,并设置了允许的源、方法、头部和凭证。然后,我们在HttpSecurity对象上调用cors()方法来启用CORS支持,并将CorsConfigurationSource对象传递给该方法。
使用过滤器解决CORS问题
您还可以创建一个自定义的过滤器来解决CORS问题。以下是一个示例配置:
@Component public class CorsFilter extends OncePerRequestFilter { @Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { CorsConfiguration config = new CorsConfiguration(); config.setAllowedOrigins(Arrays.asList("http://example.com", "https://example.org")); config.setAllowedMethods(Arrays.asList("GET", "POST", "PUT", "DELETE")); config.setAllowedHeaders(Arrays.asList("X-Requested-With", "Content-Type", "Authorization")); config.setAllowCredentials(true); CorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); source.registerCorsConfiguration("/**", config); CorsFilter corsFilter = new CorsFilter(source); corsFilter.doFilter(request, response, filterChain); } }
在上面的示例中,我们创建了一个自定义的CorsFilter类,并覆盖了doFilterInternal方法。在这个方法中,我们创建了一个CorsConfiguration对象,并设置了允许的源、方法、头部和凭证。然后,我们创建了一个UrlBasedCorsConfigurationSource对象,并将CorsConfiguration对象注册到该对象中。最后,我们创建了一个CorsFilter对象,并将其应用到请求/响应链中。
原文地址:https://www.toutiao.com/article/7284815106486420023/