springboot CORS policy
javaspringbootIn a Spring Boot application, you can enable CORS (Cross-Origin Resource Sharing) by adding the @CrossOrigin annotation to your controller or by adding the following configuration to your application class:
@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**");
}
}
This will allow CORS for all paths and all HTTP methods. If you want to allow CORS for only specific paths or methods, you can specify them in the addMapping method:
registry.addMapping("/api/**")
.allowedOrigins("http://localhost:3000")
.allowedMethods("GET", "POST");
This will allow CORS for all paths that start with “/api/”, with only the “GET” and “POST” methods, and only for requests from the ”http://localhost:3000” origin.
You can also set other CORS options, such as allowed headers and exposed headers, using the methods of the CorsRegistration object returned by the addMapping method.
Other Article on Tag java
- - after and before in java date
- - am pm java date format
- - apk minecraft java edition
- - as been blocked by CORS policy Response to preflight request doesn't pass access control check It does not have HTTP ok status
- - bubble sort in java
- - Can I compile and run a spring boot jar from inside another spring boot application
- - convert java date to localdate
- - design patterns in java
- - encrypt by java and decrypt by node
- - exception handling in java
Other Article on Tag springboot
- - Can I compile and run a spring boot jar from inside another spring boot application
- - Response to preflight request doesnt pass access control check It does not have HTTP ok status.
- - springboot ComponentScan multiple excludeFilters example
- - springboot CORS policy
- - springboot No 'Access-Control-Allow-Origin' header is present on the requested resource