1. Problem Description: cross domain problem in front end call interface
2. Solution, add the following classes
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class CorsConfiguration {
@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurer() {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowCredentials(true)
.allowedMethods("POST", "GET", "PUT", "OPTIONS", "DELETE")
.allowedOrigins("*");
}
};
}
}
The
allowedheads string array class or interface no access control request heads
exposed heads string array class or interface no access control expose heads
Note:
1) Attribute value, origins: configure the sources that can be accessed, for example: 0 https://adong.blog.csdn.net/article/details/113126033 *
indicates that all domain names are allowed.
2) Property methods: configure the methods of cross domain request support, such as get, post, delete, put, and return all supported methods at one time.
3) Attribute maxage: configures the valid time of the pre check request. The unit is seconds. It indicates how long the second pre check request does not need to be issued.
4) Attribute allowcredentials: configure whether to allow sending cookies for credential requests. Cookies are not sent by default.
5) Attribute allowedheaders: configure the allowed custom request headers for pre checking requests.
6) Attribute exposedheaders: configure the header information of the response, in which other header information can be set. Without configuration, cache control, content language, content type, expires, last modified and pragma fields can be obtained by default.
Read More:
- How to Solve Vue & Django Cross-domain Issue
- Two implementation methods of spring boot scan mapper interface class
- [GO]Solve request origin not allowed by Upgrader.CheckOrigin websocket cross-domain
- Spring-boot Use dubbo Error: ERROR 8404 o.s.boot.SpringApplication:Application run failed
- Error received: error: xhr’get’to URL cross domain [How to Solve]
- Dyf Spring boot startup error: NoSuchBeanDefinitionException
- Causes of error in data transmission from spring boot to JSON
- Error running ‘PigSellCabinetApplication’: Command line is too long. Shorten command line for PigSellCabinetApplication or also for Spring Boot default configuration
- Some problems encountered in the initial construction of Ant Design Pro project, such as cross Env, webpack and so on
- [Go] Solve the empty interface interface{} cannot use (type []string) as type []interface {}
- New Spring boot startup error Failed to auto-configure a DataSource
- How to Solve Spring MVC upload file error
- How to Solve Spring Cloud Error context has been closed already
- [How to Fix] Spring boot startup error: could not resolve placeholder
- Spring Boot Druid Error: discard long time none received connection
- How to Solve Nginx cross compilation Error
- [Solved] Logging system failed to initialize using configuration from ‘classpathlogbacklogback-spring.xml‘
- [Solved] spring boot – JPA–H2 Error: H2 error: “Syntax error in SQL statement … expected identifier“
- [neo4j] error report when visiting neo4j in spring boot project
- How to Solve Error: Missing type map configuration or unsupported mapping