【问题标题】:POST blocked by CORS policy, GET, DELETE normally workingPOST 被 CORS 策略阻止,GET、DELETE 正常工作
【发布时间】:2022-01-11 00:07:29
【问题描述】:

我的应用有奇怪的问题。 我的应用程序是这样构建的 ts(angular) -> java(spring)。我随便添加了一些从角度到java的Get,也删除了请求,但是当我要添加发布请求时遇到了问题。我的应用程序正在使用 httpbasic 身份验证。所以让我给你看一些代码: 这是来自我的数据服务的代码

getCurrentCars(id:number){
    const headers = new HttpHeaders({ 'Content-Type':'application/json',
      'Authorization': 'Basic ' + btoa(sessionStorage.getItem('username') + ':' + sessionStorage.getItem('password')) });
    return this.http.get("http://localhost:8080/api/cars/getcurrentcars/"+id.toString(),{headers});
  }
  postNewRepair(name:string, description:string, date:string, idCar:number){
    const headers = new HttpHeaders({ 'Content-Type':'application/json',
      'Authorization': 'Basic ' + btoa(sessionStorage.getItem('username') + ':' + sessionStorage.getItem('password')) });
    let newrepair:Repair= {name: name, date:date, description:description, idCar:idCar}
    this.http.post("localhost:8080/api/repairs/postRepair",newrepair,{headers}).subscribe(resp => console.log(resp));
  }

获得作品,发布不想工作,但来自 POSTMAN POST WORKS FINE

ofc 我禁用了一些 Cors 策略,以下是一些示例:

@CrossOrigin(origins = "*", allowedHeaders = "*")
@RestController
@RequestMapping("/api/repairs")
public class ControllerRepair {
@RequestMapping(method = RequestMethod.POST, value = "/postRepair")

    public void postRepair(@RequestParam String name, @RequestParam String date, @RequestParam String description, @RequestParam Long idCar){
        LocalDate date1 = LocalDate.parse(date);
        System.out.println("aaa");
        iRepairService.postRepair(name, date1, description, idCar);
    }
}

这里来自 http 配置

http
                .cors().and()
                .csrf().disable()
                .authorizeRequests()
                .antMatchers("/api/repairshops/**").hasAuthority("REPAIR_SHOP")
                .antMatchers("/api/clients/**").hasAnyAuthority("CLIENT","REPAIR_SHOP")
                .antMatchers("/login/**").fullyAuthenticated()
                .anyRequest()
                .authenticated()
                .and()
                .httpBasic();

我还尝试了我在堆栈中某处找到的这个 cors 配置类,但它根本不起作用,而且我的其他获取和删除请求也不起作用

Access to XMLHttpRequest at 'localhost:8080/api/repairs/postRepair' from origin 'http://localhost:4200' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, chrome-untrusted, https.

也许我没有在我的 http 帖子中使用某些标头来使其正常工作?

【问题讨论】:

    标签: java typescript http cors


    【解决方案1】:

    我看到您在帖子请求的网址中错过了“http”?让我们把它和之前的 GET 一样,再试一次。

    this.http.post("http://localhost:8080/api/repairs/postRepair", newrepair, { headers })
        .subscribe(resp => console.log(resp));
    

    【讨论】:

    • 我也试过了,但还是有同样的问题,所以我把 url 改成了这个,没有 http,忘了改回来
    • @CyprianGdowski 问题不可能相同。错误信息应该不同。
    【解决方案2】:

    使用您当前的设置 (http.cors()),您使用的是 spring (https://docs.spring.io/spring-security/site/docs/4.2.x/reference/html/cors.html) 提供的默认 cors 配置。您可以使用指南 (https://spring.io/blog/2015/06/08/cors-support-in-spring-framework) 针对您的用例进行配置。

    如果您只想尝试一下,可以将其用作临时解决方案:

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**").allowedMethods("*");
    }
    

    【讨论】:

      猜你喜欢
      • 2020-08-23
      • 2021-02-07
      • 2021-11-19
      • 2020-05-28
      • 1970-01-01
      • 2019-10-23
      • 2020-09-11
      • 2021-04-16
      • 2018-02-26
      相关资源
      最近更新 更多