【发布时间】: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