Spring 跨源请求被阻止:同源策略不允许读取远程资源(访问控制允许源不匹配';(null)';)

Spring 跨源请求被阻止:同源策略不允许读取远程资源(访问控制允许源不匹配';(null)';),spring,spring-mvc,spring-security,cors,Spring,Spring Mvc,Spring Security,Cors,Angular 2.0应用程序正在尝试与Rest服务器交互,以解决CORS问题 下面是我的CORSFilter实现 response.setHeader("Access-Control-Allow-Origin", "http://localhost:3000"); response.setHeader("Access-Control-Allow-Methods", request.getMethod()); response.setHeader("Access

Angular 2.0应用程序正在尝试与Rest服务器交互,以解决CORS问题

下面是我的CORSFilter实现

response.setHeader("Access-Control-Allow-Origin", "http://localhost:3000");
        response.setHeader("Access-Control-Allow-Methods", request.getMethod());
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Control-Allow-Headers", request.getHeader("Access-Control-Request-Headers"));

        if ("OPTIONS".equalsIgnoreCase(request.getMethod())) {
            response.setStatus(HttpServletResponse.SC_OK);
        } else {
            chain.doFilter(req, res);
        }
令人惊讶的是,如果我在本地部署相同的war文件(客户端在3000端口,服务器在8080端口),它工作正常,在本地,我没有得到访问控制允许凭据访问控制公开头文件,如果我们在服务器部署它,我得到这些头文件,CORS问题就来了


更新:我们部署在不同的服务器上,该服务器是http://应用程序工作正常,因为它在https上抛出错误://

修改访问控制允许源

 response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));
并且,允许所有HttpMethod.OPTIONS请求

    @Override
protected void configure(HttpSecurity http) throws Exception {
    //http.csrf().disable();
    http
        .csrf().disable()
        .httpBasic().and()
        .authorizeRequests()
            .antMatchers(HttpMethod.OPTIONS, "/**").permitAll()
            .anyRequest().authenticated()
            //.and().requiresChannel().anyRequest().requiresSecure()
            .and().httpBasic().authenticationEntryPoint(customBasicAuthenticationEntryPoint)

            .and().addFilter(basicAuthenticationFilter(super.authenticationManagerBean()));
}

@ramesh kotha,有关更多信息,请查看修改访问控制允许原点

 response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));
并且,允许所有HttpMethod.OPTIONS请求

    @Override
protected void configure(HttpSecurity http) throws Exception {
    //http.csrf().disable();
    http
        .csrf().disable()
        .httpBasic().and()
        .authorizeRequests()
            .antMatchers(HttpMethod.OPTIONS, "/**").permitAll()
            .anyRequest().authenticated()
            //.and().requiresChannel().anyRequest().requiresSecure()
            .and().httpBasic().authenticationEntryPoint(customBasicAuthenticationEntryPoint)

            .and().addFilter(basicAuthenticationFilter(super.authenticationManagerBean()));
}

@ramesh kotha,有关更多信息,请查看

response.setHeader(“访问控制允许源代码”)http://localhost:3000“”
在服务器中也是这样吗?主机:3000是我的客户端应用程序部署的地方,服务器在运行,你解决了吗?我也面临同样的问题。还没有@Thirumal。!这对我来说很有效,还添加了答案look@RameshKotha
response.setHeader(“访问控制允许源站”http://localhost:3000“”
在服务器中也是这样吗?主机:3000是我的客户端应用程序部署的地方,服务器在运行,你解决了吗?我也面临同样的问题。还没有@Thirumal。!这对我来说很有效,还添加了答案look@RameshKothaThanks,将检查并让您知道。!谢谢,我会检查并让你知道。!