Java ';http://localhost:4200' 已被CORS策略阻止:否';访问控制允许原点';请求的资源上存在标头

Java ';http://localhost:4200' 已被CORS策略阻止:否';访问控制允许原点';请求的资源上存在标头,java,angular,web-services,Java,Angular,Web Services,我使用angular for client和Java作为后端服务来解决以下问题。我浏览了所有可用的在线资源,但没有任何帮助将被感谢。 “访问位于的XMLHttpRequest”http://localhost:8081/demo/customer“起源”http://localhost:4200'已被CORS策略阻止:请求的资源上不存在'Access Control Allow Origin'标头。 控制器代码: @CrossOrigin(origins = "http://local

我使用angular for client和Java作为后端服务来解决以下问题。我浏览了所有可用的在线资源,但没有任何帮助将被感谢。 “访问位于的XMLHttpRequest”http://localhost:8081/demo/customer“起源”http://localhost:4200'已被CORS策略阻止:请求的资源上不存在'Access Control Allow Origin'标头。

控制器代码:

@CrossOrigin(origins = "http://localhost:4200")
@RestController
@RequestMapping("/demo")
public class CustomerController {
    
    @Autowired
    private CustomerService customerService;
    
    @CrossOrigin(origins = "http://localhost:4200")
    @GetMapping("/customer")
    public List<Customer> getCustomerList() {
        
        return customerService.get();
        
    }
    
}
@Configuration
public class CorsConfig {
    @Bean
    public CORSFilter corsFilter() {
        CorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration config = new CorsConfiguration();
        config.addAllowedOrigin("http://localhost:4200");
        config.addAllowedMethod(HttpMethod.DELETE);
        config.addAllowedMethod(HttpMethod.GET);
        config.addAllowedMethod(HttpMethod.OPTIONS);
        config.addAllowedMethod(HttpMethod.PUT);
        config.addAllowedMethod(HttpMethod.POST);
        ((UrlBasedCorsConfigurationSource) source).registerCorsConfiguration("/**", config);
        return new CORSFilter(source);
    }
}
试试这个:

 @Bean
public CorsConfigurationSource corsConfigurationSource() {
    String localURI = "http://localhost:4200";

    List<String> allowedOrigins = List.of(localURI);
    final CorsConfiguration configuration = new CorsConfiguration();
    configuration.setAllowedOrigins(allowedOrigins);
    configuration.setAllowedMethods(java.util.List.of("HEAD", "GET", "POST", "PUT", "DELETE", "PATCH"));
    // setAllowCredentials(true) is important, otherwise:
    // The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'.
    configuration.setAllowCredentials(true);
    // setAllowedHeaders is important! Without it, OPTIONS preflight request
    // will fail with 403 Invalid CORS request
    configuration.setAllowedHeaders(java.util.List.of("Authorization", "Cache-Control", "Content-Type", "Access-Control-Allow-Origin"));
    final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    source.registerCorsConfiguration("/**", configuration);
    return source;
}
@Bean
公共公司配置源公司配置源(){
字符串localURI=”http://localhost:4200";
List allowedOrigins=List.of(localURI);
最终公司配置配置=新公司配置();
配置.setAllowedOrigins(allowedOrigins);
setAllowedMethods(java.util.List.of(“HEAD”、“GET”、“POST”、“PUT”、“DELETE”、“PATCH”));
//setAllowCredentials(true)很重要,否则:
//当请求的凭据模式为“包括”时,响应中的“访问控制允许来源”标头的值不得为通配符“*”。
配置.setAllowCredentials(true);
//setAllowedHeaders很重要!没有它,选项飞行前请求
//将因403无效CORS请求而失败
setAllowedHeaders(java.util.List.of(“授权”、“缓存控制”、“内容类型”、“访问控制允许源”);
最终UrlBasedCorsConfigurationSource=新UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration(“/**”,配置);
返回源;
}

当在Spring中启用CORS时,所有端点访问错误在客户端中默认显示为CORS错误,因为发生错误时(身份验证或其他)未设置标题。请启用完整服务器日志记录,并粘贴相关日志。您可以在Spring属性文件中对其使用
logging.level.org.springframework=TRACE
。在那里,您可以查看是否是由于缺少权限、错误URL或其他原因。禁用默认配置加载项。属性文件Spring.autoconfigure.exclude[0]=org.springframework.boot.autoconfigure.security.servlet.securityautoconfiguration您应该检查浏览器中的
网络
选项卡,并检查响应中是否存在此标头。后端的
5xx
(内部服务器错误)将作为chrome中的
访问控制允许源站
抛出(未尝试使用其他浏览器)浏览器。