POST 被 CORS 策略阻止,GET、DELETE 正常工作
Posted
技术标签:
【中文标题】POST 被 CORS 策略阻止,GET、DELETE 正常工作【英文标题】:POST blocked by CORS policy, GET, DELETE normally working 【发布时间】: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 帖子中使用某些标头来使其正常工作?
【问题讨论】:
【参考方案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("*");
【讨论】:
以上是关于POST 被 CORS 策略阻止,GET、DELETE 正常工作的主要内容,如果未能解决你的问题,请参考以下文章
AJAX POST、PUT 和 DELETE 工作正常,但 GET 被 CORS 阻止
从 Angular 前端到 json-server 的 GET 调用访问被 CORS 策略阻止的 XMLHttpRequest