【发布时间】:2019-04-17 17:20:24
【问题描述】:
所以我尝试在我的 Spring Boot 应用程序中添加一个全局 CORS 配置。当我对所有来源进行自动处理时(通过在 CorsRegistry.allowedOrigins 中添加“*”),我可以让它工作,但是当我尝试指定 http://localhost:8080 时,它不起作用。
我的控制器
@RestController
@RequestMapping("/api")
public class Controller {
@GetMapping("/svc")
public ResponseEntity<String> method(@RequestParam(required = false) String id) {
//some code
}
我的配置:
@Configuration
public class Configuration {
@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurer() {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**").allowedOrigins("http://localhost:8080")
.allowedMethods("POST", "GET", "PUT", "OPTIONS", "DELETE")
;
}
};
}
所以这不能按预期工作。但是当我把
registry.addMapping("/**").allowedOrigins("*")
当我从客户端调用 http://localhost:8080/api/svc?id=999 时,它可以工作。
知道我在这里缺少什么吗?
谢谢!
【问题讨论】:
标签: java spring spring-boot cors