我有一个想启用CORS的小型应用程序。因此,我根据此指南进行了所有操作:https://spring.io/guides/gs/rest-service-cors/。这是我的代码:
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**").allowedOrigins("http://google.com").allowedMethods("GET,POST");
}
}
因此,它应该拒绝除http://google.com的请求以外的所有请求。这是我的控制器:
@RestController
@RequestMapping("/api/")
public class Controller {
@Autowired
private UniversitiesService universitiesService;
@Autowired
private RatingLadderService ratingLadderService;
@GetMapping("/peopleAbove")
public String getPeopleAbove(@RequestParam("facId")int fac, @RequestParam("ball") double ball,@RequestParam("prior") int prior,@RequestParam("surname") String surname) throws Exception{
surname = surname.replaceAll(" ","");
return ratingLadderService.getPeopleAbove(fac,ball,prior,surname);
}
@GetMapping("/getAllUniversities")
public List<University> getAllUnibersities() throws Exception{
return universitiesService.getAllUniversities();
}
}
但是,当我尝试获取http://localhost:9000/getAllUniversities时,它可以工作。当我通过邮递员发送请求时,它也有效。为什么呢以及如何解决?