SpringBoot解决浏览器跨域问题

package 类路径;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**
 * 解决浏览器跨域问题
 */
@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {

   @Override
    public void addCorsMappings(CorsRegistry registry) {
      registry.addMapping("/**")
         .allowedOrigins("*")
         .allowedMethods("*")
         //.allowedMethods("PUT", "DELETE","POST","GET","OPTIONS")
         .allowedHeaders("*")
         //.exposedHeaders("header1", "header2")
         .allowCredentials(true).maxAge(3600);
   }
}

你可能感兴趣的:(java,springBoot,前端,跨域,后端)