提问者:小点点

角形10和Spring防尘套1.5之间的CORS问题


我只是无法解决我的角度10应用程序和Spring引导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;
    }
}

但还是不起作用


共1个答案

匿名用户

添加交叉原点(原点=”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,那么应该添加配置。setAllowCredentials(真)也在这里,不要忘记添加http。在Spring Security配置中的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,调用应该会成功。

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