当前位置: 首页 > 知识库问答 >
问题:

'http://localhost:4200'已被CORS策略阻止:请求的资源上没有'access-control-allog-origin'标头

谭安翔
2023-03-14

我使用angular为客户机和Java作为后端服务面临以下问题。我浏览了所有可用的在线资源,但没有运气,任何帮助都将被感谢。“CORS策略阻止从来源'http://localhost:4200'访问位于'http://localhost:8081/demo/customer'的XMLHttpRequest:请求的资源上没有'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);
    }
}

共有1个答案

赵晟睿
2023-03-14
 @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;
}
 类似资料:
  • 我正在尝试使用springboot和angular更新数据,但每当我尝试更新数据时,我都会遇到这个错误http://localhost:4200“”已被CORS策略阻止:对预飞请求的响应未通过访问控制检查:请求的资源上不存在“access control Allow Origin”标头。 这是我的Spring控制器和角度服务。 我尝试了stackoverflow的其他解决方案,但它不起作用,请告诉

  • 我正在开发一个web应用程序,我使用angular为前端和keycloak为安全。 我在angular Side上使用以下软件包: keycloak-js@10.0.1 在前端(angular应用程序)上,我试图使用以下服务检索用户详细信息: 正如我所想,对loadUserProfile()的调用会向keycloak服务器的客户机(帐户)发起GET请求。 我得到一个CORS错误消息作为响应,请参见

  • 有一个应用程序在角7也有一个. net web api解决方案,当我调用一个动作CORS问题发生,错误如下 CORS策略阻止从http://localhost:57467/UserDetails/GetUserDetailshttp://localhost:4200访问XMLHttpRequest:对预试请求的响应不通过权限改造检查:请求的资源上不存在访问控制允许起源标头。 我试图使用Web ap

  • 请求标头如下所示: 临时头显示为access-control-request-headers:content-type access-control-request-method:POST origin:http://localhost:4200 referer:http://localhost:4200/login user-agent:mozilla/5.0(Macintosh;Intel M

  • 交叉起源没有发生,被CORS策略阻止。 我使用的是Windows系统。 在文件中: 在文件中: 我在浏览器控制台中遇到此错误: CORS策略阻止了从http://localhost:1111/api/v1/employees/createhttp://localhost:4200对XMLHttpRequest的访问:对预飞行请求的响应没有通过权限改造检查:请求的资源上没有访问控制允许起源标头。<-

  • 当我使用HTML2Canvas评估图像时,我得到了这个错误。 CORS策略阻止从来源“http://localhost:8080”访问位于“http://testdomain.com/storage/images/products/user_front.png”的映像:请求的资源上没有“Access-Control-Allow-Origin”标头。 下面是我的cors配置。 有人能帮我吗?

  • 问题内容: 我想访问来自同一域但端口号不同的信息,为此,我添加了响应头。 Servlet代码:( 显示在www.example.com:PORT_NUMBER上) jQuery代码:( 显示在www.example.com上) 几次我收到此错误(在控制台中): 该错误通常在执行时首次发生。第二次允许。 我的问题是代码中或代码中缺少什么? 任何建议将不胜感激。 更新1 我变了: 至: 然后我在控制台