Four ways to set up Cors cross-domain in springboot

Foreword: CorsFilter / WebMvcConfigurer / @CrossOrigin requires SpringMVC 4.2 or higher to support, corresponding to SpringBoot 1.3 or higher, these CORS features are supported. However, friends who use Spring MVC 4.2 and below do not have to panic. It is also possible to directly use method 4 to authorize CORS cross-domain access by manually adding response headers.

The first point: cross-domain problems, back-end solutions, there are four ways as follows .

Method 1: Return a new CorsFilter

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;


@Configuration
public class CorsConfig {
    private CorsConfiguration buildConfig() {
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.addAllowedOrigin("*");
        corsConfiguration.addAllowedHeader("*");
        corsConfiguration.addAllowedMethod("*");
        corsConfiguration.setMaxAge(3600L);
        corsConfiguration.setAllowCredentials(true);
        return corsConfiguration;
    }

    @Bean
    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", buildConfig());
        return new CorsFilter(source);
    }
}

Method 2: Rewrite WebMvcConfigurer

@Configuration
public class WebMvcConfg implements WebMvcConfigurer {

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        //设置允许跨域的路径
        registry.addMapping("/**")
                //设置允许跨域请求的域名
                //当**Credentials为true时,**Origin不能为星号,需为具体的ip地址【如果接口不带cookie,ip无需设成具体ip】
                .allowedOrigins("http://localhost:9527", "http://127.0.0.1:9527", "http://127.0.0.1:8082", "http://127.0.0.1:8083")
                //是否允许证书 不再默认开启
                .allowCredentials(true)
                //设置允许的方法
                .allowedMethods("*")
                //跨域允许时间
                .maxAge(3600);
    }
}

Method 3: Use annotations (@CrossOrigin)

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;


@Controller
@RequestMapping("/admin/sysLog")
@CrossOrigin
public class SysLogController {

}

Method 4: Manually set the response header (HttpServletResponse)

In this way, you can manually add it to the specific controller, inteceptor, filter and other logic.

@RequestMapping("/test")
@ResponseBody
public String test(){
response.addHeader("Access-Control-Allow-Origin", "http://localhost:8080");
return "success";
}

Summary : The above are four ways to set up the cors cross-domain backend solution. The essence is similar to the last one to set the response header, but each package implements different encapsulation logic.

Guess you like

Origin blog.csdn.net/Mint6/article/details/104726325