【发布时间】:2018-05-23 01:25:49
【问题描述】:
有一个 Spring Boot 应用程序在后端运行。我已经实现了几个 GET/POST/PUT/DELETE API。
前端(AngularJS)可以做GET和POST,但是请求PUT和DELETE时会得到403 ERROR。 我添加了允许的来源(没有它 POST 将无法工作)来解决 CORS 问题。
CORS 设置:
@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurerAdapter() {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**").allowedOrigins("https://xxx1.com", "https://xxx2.com");
}
};
}
CSRF 设置:
http.csrf().disable();
暂时禁用
下面是我的 DELETE 实现:
@DeleteMapping("/listings/{id}")
public ResponseEntity<Void> deleteListings(@PathVariable Long id) {
log.debug("REST request to delete listings : {}", id);
this.listingsService.delete(id);
return ResponseEntity.status(HttpStatus.OK).body(null);
}
有人可以帮忙吗?非常感谢!
【问题讨论】:
-
你能分享或展示你的代码吗,也许在 github 上?
标签: angularjs http spring-boot cors