I have Spring Boot rest controller which exposes certain APIs which I want to consume in Angular 12. On the controller I have added @CrossOrigin, but spring-boot does not seem to pick it up because even after adding the annotation I am getting the Cors policy issue.
@CrossOrigin(origins = "*")
@RestController
@Scope(WebApplicationContext.SCOPE_REQUEST)
@RequestMapping("api/rest")
public class MyController {
I think you've enabled the cross-origin for the particular controller. Either you can enable the same by adding the @CrossOrigin(origins = "*")
on other controllers or you can configured the cross-origin globally by creating the bean as following
@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurer() {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**").allowedOrigins("*");
}
};
}
Have you tried adding it as @CrossOrigin(origins = "http://localhost:<your_port>")
Where your_port
is the port your angular app is using?
尝试以下代码:
@CrossOrigin(allowCredentials = "true")
Add a CORS-Origin extension to your browser if you are sending request from the browser. If @CrossOrigin doesn't work then thats your problem. There you go:
https://chrome.google.com/webstore/detail/cors-unblock/lfhmikememgdcahcdlaciloancbhjino
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.