【问题标题】:CORS issue between Angular 10 and Spring Boot 1.5Angular 10 和 Spring Boot 1.5 之间的 CORS 问题
【发布时间】:2026-02-24 19:00:01
【问题描述】:

我只是无法解决我的 angular 10 应用程序和 spring boot 1.5 之间的 CORS 问题。

我已经尽我所能,但仍然无法正常工作。 我正在尝试设置全局 CORS 配置以允许所有内容

我目前的实现:

@Configuration
public class DevConfig {
    
    @Bean
    public WebMvcConfigurer corsConfigurer() {
        return new WebMvcConfigurerAdapter() {
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/**")
                    .allowedOrigins("http://localhost:4200")
                    .allowedHeaders("*");
            }
        };
    }
    
}

我在前端得到这个:

Access to XMLHttpRequest at 'http://localhost:8080/test' from origin 'http://localhost:4200' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource

版本

<properties>
    <java.version>1.8</java.version>
    <commons-io.version>2.7</commons-io.version>
    <spring.version>1.5.9.RELEASE</spring.version>
</properties>

[编辑]

我已将其更改为:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
    
import java.util.Arrays;
    
@Configuration
public class AppConfig {
    @Bean
    CorsConfigurationSource corsConfigurationSource()
    {
        CorsConfiguration configuration = new CorsConfiguration();
            configuration.setAllowedOrigins(Arrays.asList("http://localhost:4200"));
        configuration.setAllowedMethods(Arrays.asList("GET", "POST", "OPTIONS", "DELETE", "PUT", "PATCH"));
        configuration.setAllowedHeaders(Arrays.asList("X-Requested-With", "Origin", "Content-Type", "Accept", "Authorization"));
        configuration.setAllowCredentials(true);
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", configuration);
        return source;
    }
}

还是不行

【问题讨论】:

  • 你检查过这个吗:spring.io/guides/gs/rest-service-cors
  • 基本上是标题“全局 CORS 配置”下的描述,这就是我正在做的事情
  • 你能分享你的控制器,你有“测试”映射吗?

标签: spring-boot cors


【解决方案1】:

在 RestController 的方法或控制器级别添加 @CrossOrigin(origins="http://localhost:4200")。或者,

如果要添加全局配置,请在配置文件中配置此 bean:

@Bean
CorsConfigurationSource corsConfigurationSource() 
{
    CorsConfiguration configuration = new CorsConfiguration();
    configuration.setAllowedOrigins(Arrays.asList("http://localhost:4200"));
    configuration.setAllowedMethods(Arrays.asList("GET", "POST", "OPTIONS", "DELETE", "PUT", "PATCH"));
    configuration.setAllowedHeaders(Arrays.asList("X-Requested-With", "Origin", "Content-Type", "Accept", "Authorization"));
    configuration.setAllowCredentials(true);
    UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    source.registerCorsConfiguration("/**", configuration);
    return source;
}

如果你使用的是spring security,你也应该在这里添加configuration.setAllowCredentials(true);,不要忘记在spring security配置中添加http.cors()

编辑:

当浏览器发出跨域请求时,浏览器会添加一个带有当前来源(方案、主机和端口)的 Origin 标头。在服务器端,当服务器看到此标头并希望允许访问时,它需要将 Access-Control-Allow-Origin 标头添加到指定请求来源的响应中(或 * 以允许任何来源。)。当浏览器看到此响应带有适当的 Access-Control-Allow-Origin 标头时,浏览器允许与客户端站点共享响应数据。

如果您的后端正在发送标头,这可能是 Chrome 不支持 localhost 通过 Access-Control-Allow-Origin。要让 Chrome 在标头中发送 Access-Control-Allow-Origin,只需将 /etc/hosts 文件中的 localhost 别名为其他域,例如:

127.0.0.1   localhost yourdomain.com

然后,如果您使用 yourdomain.com 而不是 localhost 访问您的 api,调用应该会成功。

如果问题仍然存在,则需要检查后端是否发送了任何标头。如果没有,请将其添加到响应标头(这很讨厌)。让我知道实际发生了什么。

【讨论】:

  • 感谢您的帮助。但它不工作
  • 您在控制器上添加了@CrossOrigin 吗? @安娜
  • 是否应该将其添加到那里,即使它是全局配置的?但即使我添加它也不起作用,我不知道到底发生了什么
  • @Anna,你是只从浏览器还是从邮递员那里收到这个 cors 错误?
  • 仅浏览器。 Access to XMLHttpRequest at 'http://localhost:8080/test/' from origin 'http://localhost:4200' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. zone-evergreen.js:2845 GET http://localhost:8080/test/ net::ERR_FAILED